File indexing completed on 2024-05-12 11:57:22

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2014 Miquel Sabaté Solà <mikisabate@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef FAKE_CODE_COMPLETION_TEST_MODEL_H
0009 #define FAKE_CODE_COMPLETION_TEST_MODEL_H
0010 
0011 #include "base.h"
0012 
0013 #include <ktexteditor/codecompletionmodel.h>
0014 
0015 namespace KTextEditor
0016 {
0017 class CodeCompletionInterface;
0018 }
0019 
0020 /**
0021  * Helper class that mimics some of the behaviour of KDevelop's code completion, in particular
0022  * whether it performs "bracket merging" on completed function calls e.g. if we complete a call
0023  * to "functionCall(int a)" at the end of the -> here:
0024  *
0025  *  object->(
0026  *
0027  * we end up with
0028  *
0029  *  object->functionCall(
0030  *
0031  * and the cursor placed after the closing bracket: the opening bracket is merged with the existing
0032  * bracket.
0033  *
0034  * However, if we do the same with
0035  *
0036  *  object->
0037  *
0038  * we end up with
0039  *
0040  *  object->functionCall()
0041  *
0042  * again with the cursor placed after the opening bracket.  This time, the brackets were not merged.
0043  *
0044  * This helper class is used to test how Macros and replaying of last changes works with complex
0045  * code completion.
0046  */
0047 class FakeCodeCompletionTestModel : public KTextEditor::CodeCompletionModel
0048 {
0049     Q_OBJECT
0050 
0051 public:
0052     explicit FakeCodeCompletionTestModel(KTextEditor::View *parent);
0053     /**
0054      * List of completions, in sorted order.
0055      * A string ending with "()" is treated as a call to a function with no arguments.
0056      * A string ending with "(...)" is treated as a call to a function with at least one argument.  The "..." is not
0057      * inserted into the text.
0058      * A string ending with "();" or "(...);" is the same as above, and the semi-colon is added.  Bracket merging
0059      * never happens with strings ending with ";".
0060      */
0061     void setCompletions(const QStringList &completions);
0062     void setRemoveTailOnComplete(bool removeTailOnCompletion);
0063     void setFailTestOnInvocation(bool failTestOnInvocation);
0064     bool wasInvoked();
0065     void clearWasInvoked();
0066     /**
0067      * A more reliable form of setAutomaticInvocationEnabled().
0068      */
0069     void forceInvocationIfDocTextIs(const QString &desiredDocText);
0070     void doNotForceInvocation();
0071     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0072     void executeCompletionItem(KTextEditor::View *view, const KTextEditor::Range &word, const QModelIndex &index) const override;
0073     KTextEditor::CodeCompletionInterface *cc() const;
0074 
0075 private:
0076     void failTest() const;
0077     QStringList m_completions;
0078     KTextEditor::ViewPrivate *m_kateView;
0079     KTextEditor::Document *m_kateDoc;
0080     bool m_removeTailOnCompletion;
0081     bool m_failTestOnInvocation;
0082     mutable bool m_wasInvoked;
0083     QString m_forceInvocationIfDocTextIs;
0084 
0085 private Q_SLOTS:
0086     void textInserted(KTextEditor::Document *document, KTextEditor::Range range);
0087     void textRemoved(KTextEditor::Document *document, KTextEditor::Range range);
0088     void checkIfShouldForceInvocation();
0089 };
0090 
0091 #endif /* FAKE_CODE_COMPLETION_TEST_MODEL_H */