1 /*
2     This file is part of the KDE libraries
3     SPDX-FileCopyrightText: 2014 Miquel Sabaté Solà <mikisabate@gmail.com>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #ifndef FAKE_CODE_COMPLETION_TEST_MODEL_H
9 #define FAKE_CODE_COMPLETION_TEST_MODEL_H
10 
11 #include "base.h"
12 
13 #include <ktexteditor/codecompletionmodel.h>
14 
15 namespace KTextEditor
16 {
17 class CodeCompletionInterface;
18 }
19 
20 /**
21  * Helper class that mimics some of the behaviour of KDevelop's code completion, in particular
22  * whether it performs "bracket merging" on completed function calls e.g. if we complete a call
23  * to "functionCall(int a)" at the end of the -> here:
24  *
25  *  object->(
26  *
27  * we end up with
28  *
29  *  object->functionCall(
30  *
31  * and the cursor placed after the closing bracket: the opening bracket is merged with the existing
32  * bracket.
33  *
34  * However, if we do the same with
35  *
36  *  object->
37  *
38  * we end up with
39  *
40  *  object->functionCall()
41  *
42  * again with the cursor placed after the opening bracket.  This time, the brackets were not merged.
43  *
44  * This helper class is used to test how Macros and replaying of last changes works with complex
45  * code completion.
46  */
47 class FakeCodeCompletionTestModel : public KTextEditor::CodeCompletionModel
48 {
49     Q_OBJECT
50 
51 public:
52     explicit FakeCodeCompletionTestModel(KTextEditor::View *parent);
53     /**
54      * List of completions, in sorted order.
55      * A string ending with "()" is treated as a call to a function with no arguments.
56      * A string ending with "(...)" is treated as a call to a function with at least one argument.  The "..." is not
57      * inserted into the text.
58      * A string ending with "();" or "(...);" is the same as above, and the semi-colon is added.  Bracket merging
59      * never happens with strings ending with ";".
60      */
61     void setCompletions(const QStringList &completions);
62     void setRemoveTailOnComplete(bool removeTailOnCompletion);
63     void setFailTestOnInvocation(bool failTestOnInvocation);
64     bool wasInvoked();
65     void clearWasInvoked();
66     /**
67      * A more reliable form of setAutomaticInvocationEnabled().
68      */
69     void forceInvocationIfDocTextIs(const QString &desiredDocText);
70     void doNotForceInvocation();
71     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
72     void executeCompletionItem(KTextEditor::View *view, const KTextEditor::Range &word, const QModelIndex &index) const override;
73     KTextEditor::CodeCompletionInterface *cc() const;
74 
75 private:
76     void failTest() const;
77     QStringList m_completions;
78     KTextEditor::ViewPrivate *m_kateView;
79     KTextEditor::Document *m_kateDoc;
80     bool m_removeTailOnCompletion;
81     bool m_failTestOnInvocation;
82     mutable bool m_wasInvoked;
83     QString m_forceInvocationIfDocTextIs;
84 
85 private Q_SLOTS:
86     void textInserted(KTextEditor::Document *document, KTextEditor::Range range);
87     void textRemoved(KTextEditor::Document *document, KTextEditor::Range range);
88     void checkIfShouldForceInvocation();
89 };
90 
91 #endif /* FAKE_CODE_COMPLETION_TEST_MODEL_H */
92