1 /*
2     SPDX-FileCopyrightText: 2006-2008 Hamish Rodda <rodda@kde.org>
3     SPDX-FileCopyrightText: 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #ifndef KDEVPLATFORM_CODECOMPLETIONWORKER_H
9 #define KDEVPLATFORM_CODECOMPLETIONWORKER_H
10 
11 #include <QList>
12 
13 #include <language/languageexport.h>
14 #include "../duchain/duchainpointer.h"
15 #include "../codecompletion/codecompletioncontext.h"
16 
17 class QMutex;
18 
19 namespace KTextEditor {
20 class Range;
21 class View;
22 class Cursor;
23 }
24 
25 namespace KDevelop {
26 class CompletionTreeElement;
27 class CodeCompletionModel;
28 
29 class KDEVPLATFORMLANGUAGE_EXPORT CodeCompletionWorker
30     : public QObject
31 {
32     Q_OBJECT
33 
34 public:
35     explicit CodeCompletionWorker(CodeCompletionModel* model);
36     ~CodeCompletionWorker() override;
37 
38     virtual void abortCurrentCompletion();
39 
40     void setFullCompletion(bool);
41     bool fullCompletion() const;
42 
43     KDevelop::CodeCompletionModel* model() const;
44 
45     ///When this is called, the result is shown in the completion-list.
46     ///Call this from within your code
47     void foundDeclarations(const QList<QExplicitlySharedDataPointer<CompletionTreeElement>>&,
48                            const CodeCompletionContext::Ptr& completionContext);
49 
50 Q_SIGNALS:
51 
52     ///Internal connections into the foreground completion model
53     void foundDeclarationsReal(const QList<QExplicitlySharedDataPointer<CompletionTreeElement>>&,
54                                const QExplicitlySharedDataPointer<CodeCompletionContext>& completionContext);
55 
56 protected:
57 
58     virtual void computeCompletions(const DUContextPointer& context, const KTextEditor::Cursor& position,
59                                     const QString& followingText, const KTextEditor::Range& contextRange,
60                                     const QString& contextText);
61     ///This can be overridden to compute an own grouping in the completion-list.
62     ///The default implementation groups items in a way that improves the efficiency of the completion-model, thus the default-implementation should be preferred.
63     virtual QList<QExplicitlySharedDataPointer<CompletionTreeElement>> computeGroups(
64         const QList<CompletionTreeItemPointer>& items,
65         const QExplicitlySharedDataPointer<CodeCompletionContext>& completionContext);
66     ///If you don't need to reimplement computeCompletions, you can implement only this.
67     virtual KDevelop::CodeCompletionContext* createCompletionContext(const KDevelop::DUContextPointer& context,
68                                                                      const QString& contextText,
69                                                                      const QString& followingText,
70                                                                      const CursorInRevision& position) const;
71 
72     ///Override this to change the text-range which is used as context-information for the completion context
73     ///The foreground-lock and a DUChain read lock are held when this is called
74     virtual void updateContextRange(KTextEditor::Range& contextRange, KTextEditor::View* view,
75                                     const DUContextPointer& context) const;
76 
77     ///Can be used to retrieve and set the aborting flag(Enabling it is equivalent to calling abortCompletion())
78     ///Is always reset from within computeCompletions
79     bool& aborting();
80 
81     ///Emits foundDeclarations() with an empty list. Always call this when you abort the process of computing completions
82     void failed();
83 
84 public Q_SLOTS:
85     ///Connection from the foreground thread within CodeCompletionModel
86     void computeCompletions(const KDevelop::DUContextPointer& context, const KTextEditor::Cursor& position,
87                             KTextEditor::View* view);
88     ///This can be used to do special processing within the background, completely bypassing the normal computeCompletions(..) etc. system.
89     ///It will be executed within the background when the model emits doSpecialProcessingInBackground
90     virtual void doSpecialProcessing(uint data);
91 
92 private:
93     bool m_hasFoundDeclarations;
94     QMutex* m_mutex;
95     bool m_abort;
96     bool m_fullCompletion;
97     KDevelop::CodeCompletionModel* m_model;
98 };
99 }
100 
101 #endif // KDEVPLATFORM_CODECOMPLETIONWORKER_H
102