cppquickfix.cpp 5.38 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "cppquickfix.h"
#include "cppeditor.h"
32

33
34
35
36
#include <AST.h>
#include <TranslationUnit.h>
#include <Token.h>

37
#include <cplusplus/ASTPath.h>
38
#include <cplusplus/CppDocument.h>
39
#include <cplusplus/ResolveExpression.h>
Tobias Hunger's avatar
Tobias Hunger committed
40
#include <cplusplus/Overview.h>
41
#include <cplusplus/TypeOfExpression.h>
42
#include <cplusplus/DependencyTable.h>
43
#include <cplusplus/CppRewriter.h>
44

45
#include <cppeditor/cppeditor.h>
Roberto Raggi's avatar
Roberto Raggi committed
46
#include <cppeditor/cpprefactoringchanges.h>
47
#include <cpptools/cpptoolsconstants.h>
48
#include <cpptools/cppmodelmanagerinterface.h>
Roberto Raggi's avatar
Roberto Raggi committed
49
#include <extensionsystem/pluginmanager.h>
50
51

#include <QtGui/QTextBlock>
52

53
using namespace CppEditor;
54
using namespace CppEditor::Internal;
55
using namespace TextEditor;
56
using namespace CPlusPlus;
57
using namespace Utils;
58

59
60
61
CppQuickFixState::CppQuickFixState(TextEditor::BaseTextEditor *editor)
    : QuickFixState(editor)
{}
62

63
const QList<AST *> &CppQuickFixState::path() const
64
{
65
66
    return _path;
}
67

68
Snapshot CppQuickFixState::snapshot() const
69
{
70
71
    return _snapshot;
}
72

73
Document::Ptr CppQuickFixState::document() const
74
{
75
76
    return _semanticInfo.doc;
}
77

78
SemanticInfo CppQuickFixState::semanticInfo() const
79
{
80
81
    return _semanticInfo;
}
82

83
const LookupContext &CppQuickFixState::context() const
84
{
85
86
    return _context;
}
87

88
const CppRefactoringFile CppQuickFixState::currentFile() const
89
{
90
    return CppRefactoringFile(editor(), document());
91
}
92

93

94
95
96
97
CppQuickFixOperation::CppQuickFixOperation(const CppQuickFixState &state, int priority)
    : QuickFixOperation(priority)
    , _state(state)
{}
98

99
100
101
CppQuickFixOperation::~CppQuickFixOperation()
{}

102
103
void CppQuickFixOperation::perform()
{
104
    CppRefactoringChanges refactoring(_state.snapshot());
105
    CppRefactoringFile current = refactoring.file(fileName());
106
107
108
109

    performChanges(&current, &refactoring);
}

110
111
112
const CppQuickFixState &CppQuickFixOperation::state() const
{
    return _state;
113
114
}

115
116
117
118
QString CppQuickFixOperation::fileName() const
{ return state().document()->fileName(); }

CppQuickFixFactory::CppQuickFixFactory()
119
{
120
}
121

122
123
124
CppQuickFixFactory::~CppQuickFixFactory()
{
}
125

126
127
128
129
130
131
132
QList<QuickFixOperation::Ptr> CppQuickFixFactory::matchingOperations(QuickFixState *state)
{
    if (CppQuickFixState *cppState = static_cast<CppQuickFixState *>(state))
        return match(*cppState);
    else
        return QList<TextEditor::QuickFixOperation::Ptr>();
}
133

134
135
136
137
138
QList<CppQuickFixOperation::Ptr> CppQuickFixFactory::singleResult(CppQuickFixOperation *operation)
{
    QList<CppQuickFixOperation::Ptr> result;
    result.append(CppQuickFixOperation::Ptr(operation));
    return result;
139
140
}

141
QList<CppQuickFixOperation::Ptr> CppQuickFixFactory::noResult()
142
{
143
    return QList<CppQuickFixOperation::Ptr>();
144
145
}

146
CppQuickFixCollector::CppQuickFixCollector()
147
148
{
}
149

150
CppQuickFixCollector::~CppQuickFixCollector()
151
{
152
}
153

154
155
156
157
158
bool CppQuickFixCollector::supportsEditor(TextEditor::ITextEditable *editor)
{
    return CppTools::CppModelManagerInterface::instance()->isCppEditor(editor);
}

159
TextEditor::QuickFixState *CppQuickFixCollector::initializeCompletion(TextEditor::BaseTextEditor *editor)
160
{
161
162
    if (CPPEditor *cppEditor = qobject_cast<CPPEditor *>(editor)) {
        const SemanticInfo info = cppEditor->semanticInfo();
163

164
        if (info.revision != cppEditor->editorRevision()) {
165
166
167
            // outdated
            qWarning() << "TODO: outdated semantic info, force a reparse.";
            return 0;
168
        }
Roberto Raggi's avatar
Roberto Raggi committed
169

170
171
        if (info.doc) {
            ASTPath astPath(info.doc);
172

173
            const QList<AST *> path = astPath(cppEditor->textCursor());
174
            if (! path.isEmpty()) {
175
176
177
178
179
                CppQuickFixState *state = new CppQuickFixState(editor);
                state->_path = path;
                state->_semanticInfo = info;
                state->_snapshot = CppTools::CppModelManagerInterface::instance()->snapshot();
                state->_context = LookupContext(info.doc, state->snapshot());
180
181
                return state;
            }
182
        }
183
184
    }

185
    return 0;
186
187
}

188
QList<TextEditor::QuickFixFactory *> CppQuickFixCollector::quickFixFactories() const
Roberto Raggi's avatar
Roberto Raggi committed
189
{
190
191
192
193
194
    QList<TextEditor::QuickFixFactory *> results;
    ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
    foreach (CppQuickFixFactory *f, pm->getObjects<CppEditor::CppQuickFixFactory>())
        results.append(f);
    return results;
195
}