1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "clangcompletionassistinterface.h"
29 
30 #include <cpptools/cppcompletionassistprocessor.h>
31 
32 #include <clangsupport/codecompletion.h>
33 
34 #include <QCoreApplication>
35 #include <QTextCursor>
36 
37 namespace ClangCodeModel {
38 namespace Internal {
39 
40 using ClangBackEnd::CodeCompletions;
41 using ClangBackEnd::CompletionCorrection;
42 
43 class ClangCompletionAssistProcessor : public CppTools::CppCompletionAssistProcessor
44 {
45     Q_DECLARE_TR_FUNCTIONS(ClangCodeModel::Internal::ClangCompletionAssistProcessor)
46 
47 public:
48     ClangCompletionAssistProcessor();
49     ~ClangCompletionAssistProcessor() override;
50 
51     TextEditor::IAssistProposal *perform(const TextEditor::AssistInterface *interface) override;
52 
53     void handleAvailableCompletions(const CodeCompletions &completions);
running()54     bool running() final { return m_requestSent; }
55 
56     const TextEditor::TextEditorWidget *textEditorWidget() const;
57 
58 private:
59     void cancel() override;
60     TextEditor::IAssistProposal *startCompletionHelper();
61     int startOfOperator(int pos, unsigned *kind, bool wantFunctionCall) const;
62     int findStartOfName(int pos = -1) const;
63     bool accepts() const;
64 
65     TextEditor::IAssistProposal *createProposal();
66     TextEditor::IAssistProposal *createFunctionHintProposal(
67             const CodeCompletions &completions);
68 
69     QList<TextEditor::AssistProposalItemInterface *> toAssistProposalItems(
70             const CodeCompletions &completions) const;
71     bool completeInclude(const QTextCursor &cursor);
72     bool completeInclude(int position);
73     void completeIncludePath(const QString &realPath, const QStringList &suffixes);
74     bool completePreprocessorDirectives();
75     bool completeDoxygenKeywords();
76     void addCompletionItem(const QString &text,
77                            const QIcon &icon = QIcon(),
78                            int order = 0);
79 
80     struct UnsavedFileContentInfo {
81         QByteArray unsavedContent;
82         bool isDocumentModified = false;
83     };
84     UnsavedFileContentInfo unsavedFileContent(const QByteArray &customFileContent) const;
85 
86     void sendFileContent(const QByteArray &customFileContent);
87     bool sendCompletionRequest(int position,
88                                const QByteArray &customFileContent,
89                                int functionNameStartPosition = -1);
90 
91     CodeCompletions applyCompletionFixIt(const CodeCompletions &completions);
92 
93 private:
94     struct Position { int line; int column; };
95     Position extractLineColumn(int position);
96 
97     QScopedPointer<const ClangCompletionAssistInterface> m_interface;
98     unsigned m_completionOperator;
99     enum CompletionRequestType { NormalCompletion, FunctionHintCompletion };
100     CompletionRequestType m_sentRequestType = NormalCompletion;
101     int m_position = -1;
102     QByteArray m_content;
103     bool m_requestSent = false;
104     bool m_addSnippets = false; // For type == Type::NormalCompletion
105     bool m_fallbackToNormalCompletion = true;
106 };
107 
108 } // namespace Internal
109 } // namespace ClangCodeModel
110