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 "clangdiagnosticmanager.h"
29 #include "clangeditordocumentparser.h"
30 
31 #include <cpptools/builtineditordocumentprocessor.h>
32 #include <cpptools/semantichighlighter.h>
33 
34 #include <utils/futuresynchronizer.h>
35 #include <utils/id.h>
36 
37 #include <QFutureWatcher>
38 #include <QTimer>
39 
40 namespace ClangBackEnd {
41 class DiagnosticContainer;
42 class TokenInfoContainer;
43 class FileContainer;
44 }
45 
46 namespace ClangCodeModel {
47 namespace Internal {
48 
49 class BackendCommunicator;
50 
51 class ClangEditorDocumentProcessor : public CppTools::BaseEditorDocumentProcessor
52 {
53     Q_OBJECT
54 
55 public:
56     ClangEditorDocumentProcessor(BackendCommunicator &communicator,
57                                  TextEditor::TextDocument *document);
58     ~ClangEditorDocumentProcessor() override;
59 
60     // BaseEditorDocumentProcessor interface
61     void runImpl(const CppTools::BaseEditorDocumentParser::UpdateParams &updateParams) override;
62     void semanticRehighlight() override;
63     void recalculateSemanticInfoDetached(bool force) override;
64     CppTools::SemanticInfo recalculateSemanticInfo() override;
65     CppTools::BaseEditorDocumentParser::Ptr parser() override;
66     CPlusPlus::Snapshot snapshot() override;
67     bool isParserRunning() const override;
68 
69     bool hasProjectPart() const;
70     CppTools::ProjectPart::Ptr projectPart() const;
71     void clearProjectPart();
72 
73     ::Utils::Id diagnosticConfigId() const;
74 
75     void updateCodeWarnings(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
76                             const ClangBackEnd::DiagnosticContainer &firstHeaderErrorDiagnostic,
77                             uint documentRevision);
78     void updateHighlighting(const QVector<ClangBackEnd::TokenInfoContainer> &tokenInfos,
79                             const QVector<ClangBackEnd::SourceRangeContainer> &skippedPreprocessorRanges,
80                             uint documentRevision);
81     void updateTokenInfos(const QVector<ClangBackEnd::TokenInfoContainer> &tokenInfos,
82                           uint documentRevision);
83 
84     TextEditor::QuickFixOperations
85     extraRefactoringOperations(const TextEditor::AssistInterface &assistInterface) override;
86 
87     void invalidateDiagnostics() override;
88 
89     TextEditor::TextMarks diagnosticTextMarksAt(uint line, uint column) const;
90 
91     void editorDocumentTimerRestarted() override;
92 
93     void setParserConfig(const CppTools::BaseEditorDocumentParser::Configuration &config) override;
94 
95     QFuture<CppTools::CursorInfo> cursorInfo(const CppTools::CursorInfoParams &params) override;
96     QFuture<CppTools::CursorInfo> requestLocalReferences(const QTextCursor &cursor) override;
97     QFuture<CppTools::SymbolInfo> requestFollowSymbol(int line, int column) override;
98     QFuture<CppTools::ToolTipInfo> toolTipInfo(const QByteArray &codecName,
99                                                int line,
100                                                int column) override;
101 
102     void closeBackendDocument();
103 
104     void clearDiagnosticsWithFixIts();
105 
106     const QVector<ClangBackEnd::TokenInfoContainer> &tokenInfos() const;
107 
108     static void clearTaskHubIssues();
109     void generateTaskHubIssues();
110 
111     static void clearTextMarks(const Utils::FilePath &filePath);
112 
113 public:
114     static ClangEditorDocumentProcessor *get(const QString &filePath);
115 
116 signals:
117     void tokenInfosUpdated();
118 
119 private:
120     void onParserFinished();
121 
122     void updateBackendProjectPartAndDocument();
123     void updateBackendDocument(CppTools::ProjectPart &projectPart);
124     void updateBackendDocumentIfProjectPartExists();
125     void requestAnnotationsFromBackend();
126 
127     HeaderErrorDiagnosticWidgetCreator creatorForHeaderErrorDiagnosticWidget(
128             const ClangBackEnd::DiagnosticContainer &firstHeaderErrorDiagnostic);
129     ClangBackEnd::FileContainer simpleFileContainer(const QByteArray &codecName = QByteArray()) const;
130     ClangBackEnd::FileContainer fileContainerWithOptionsAndDocumentContent(
131         const QStringList &compilationArguments,
132         const ProjectExplorer::HeaderPaths headerPaths) const;
133     ClangBackEnd::FileContainer fileContainerWithDocumentContent() const;
134 
135 private:
136     TextEditor::TextDocument &m_document;
137     ClangDiagnosticManager m_diagnosticManager;
138     BackendCommunicator &m_communicator;
139     QSharedPointer<ClangEditorDocumentParser> m_parser;
140     CppTools::ProjectPart::Ptr m_projectPart;
141     ::Utils::Id m_diagnosticConfigId;
142     bool m_isProjectFile = false;
143     QFutureWatcher<void> m_parserWatcher;
144     QTimer m_updateBackendDocumentTimer;
145     unsigned m_parserRevision;
146     enum class InvalidationState { Off, Scheduled, Canceled } m_invalidationState;
147 
148     QVector<ClangBackEnd::TokenInfoContainer> m_tokenInfos;
149     CppTools::SemanticHighlighter m_semanticHighlighter;
150     CppTools::BuiltinEditorDocumentProcessor m_builtinProcessor;
151     Utils::FutureSynchronizer m_parserSynchronizer;
152 };
153 
154 } // namespace Internal
155 } // namespace ClangCodeModel
156