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 #include "cppquickfixassistant.h"
27 
28 #include "cppeditorconstants.h"
29 #include "cppeditorwidget.h"
30 #include "cppquickfixes.h"
31 
32 #include <cpptools/cppmodelmanager.h>
33 #include <cpptools/cpprefactoringchanges.h>
34 
35 #include <texteditor/codeassist/genericproposal.h>
36 #include <texteditor/codeassist/iassistprocessor.h>
37 #include <texteditor/textdocument.h>
38 
39 #include <cplusplus/ASTPath.h>
40 
41 #include <utils/algorithm.h>
42 #include <utils/qtcassert.h>
43 
44 using namespace TextEditor;
45 using namespace CppTools;
46 using namespace CPlusPlus;
47 
48 namespace CppEditor {
49 namespace Internal {
50 
51 // -------------------------
52 // CppQuickFixAssistProcessor
53 // -------------------------
54 class CppQuickFixAssistProcessor : public IAssistProcessor
55 {
perform(const AssistInterface * interface)56     IAssistProposal *perform(const AssistInterface *interface) override
57     {
58         QSharedPointer<const AssistInterface> assistInterface(interface);
59         auto cppInterface = assistInterface.staticCast<const CppQuickFixInterface>();
60 
61         QuickFixOperations quickFixes;
62         for (CppQuickFixFactory *factory : CppQuickFixFactory::cppQuickFixFactories())
63             factory->match(*cppInterface, quickFixes);
64 
65         return GenericProposal::createProposal(interface, quickFixes);
66     }
67 };
68 
69 // -------------------------
70 // CppQuickFixAssistProvider
71 // -------------------------
runType() const72 IAssistProvider::RunType CppQuickFixAssistProvider::runType() const
73 {
74     return Synchronous;
75 }
76 
createProcessor() const77 IAssistProcessor *CppQuickFixAssistProvider::createProcessor() const
78 {
79     return new CppQuickFixAssistProcessor;
80 }
81 
82 // --------------------------
83 // CppQuickFixAssistInterface
84 // --------------------------
CppQuickFixInterface(CppEditorWidget * editor,AssistReason reason)85 CppQuickFixInterface::CppQuickFixInterface(CppEditorWidget *editor,
86                                                        AssistReason reason)
87     : AssistInterface(editor->document(), editor->position(),
88                       editor->textDocument()->filePath(), reason)
89     , m_editor(editor)
90     , m_semanticInfo(editor->semanticInfo())
91     , m_snapshot(CppModelManager::instance()->snapshot())
92     , m_currentFile(CppRefactoringChanges::file(editor, m_semanticInfo.doc))
93     , m_context(m_semanticInfo.doc, m_snapshot)
94 {
95     QTC_CHECK(m_semanticInfo.doc);
96     QTC_CHECK(m_semanticInfo.doc->translationUnit());
97     QTC_CHECK(m_semanticInfo.doc->translationUnit()->ast());
98     ASTPath astPath(m_semanticInfo.doc);
99     m_path = astPath(editor->textCursor());
100 }
101 
path() const102 const QList<AST *> &CppQuickFixInterface::path() const
103 {
104     return m_path;
105 }
106 
snapshot() const107 Snapshot CppQuickFixInterface::snapshot() const
108 {
109     return m_snapshot;
110 }
111 
semanticInfo() const112 SemanticInfo CppQuickFixInterface::semanticInfo() const
113 {
114     return m_semanticInfo;
115 }
116 
context() const117 const LookupContext &CppQuickFixInterface::context() const
118 {
119     return m_context;
120 }
121 
editor() const122 CppEditorWidget *CppQuickFixInterface::editor() const
123 {
124     return m_editor;
125 }
126 
currentFile() const127 CppRefactoringFilePtr CppQuickFixInterface::currentFile() const
128 {
129     return m_currentFile;
130 }
131 
isCursorOn(unsigned tokenIndex) const132 bool CppQuickFixInterface::isCursorOn(unsigned tokenIndex) const
133 {
134     return currentFile()->isCursorOn(tokenIndex);
135 }
136 
isCursorOn(const AST * ast) const137 bool CppQuickFixInterface::isCursorOn(const AST *ast) const
138 {
139     return currentFile()->isCursorOn(ast);
140 }
141 
142 } // namespace Internal
143 } // namespace CppEditor
144