File indexing completed on 2024-04-28 03:57:09

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