OSDN Git Service

afea7550b72c22b343f254fe0515ddf906cc59b1
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljseditor / qmljsquickfix.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qmljsquickfix.h"
35 #include "qmljscomponentfromobjectdef.h"
36 #include "qmljseditor.h"
37 #include "qmljs/parser/qmljsast_p.h"
38
39 #include <extensionsystem/iplugin.h>
40 #include <extensionsystem/pluginmanager.h>
41
42 #include <qmljs/qmljsmodelmanagerinterface.h>
43
44 #include <QtCore/QDebug>
45
46 using namespace QmlJS;
47 using namespace QmlJS::AST;
48 using namespace QmlJSEditor;
49 using namespace QmlJSEditor::Internal;
50 using namespace QmlJSTools;
51 using namespace TextEditor;
52 using TextEditor::RefactoringChanges;
53
54 QmlJSQuickFixState::QmlJSQuickFixState(TextEditor::BaseTextEditorWidget *editor)
55     : QuickFixState(editor)
56 {
57 }
58
59 SemanticInfo QmlJSQuickFixState::semanticInfo() const
60 {
61     return _semanticInfo;
62 }
63
64 Snapshot QmlJSQuickFixState::snapshot() const
65 {
66     return _semanticInfo.snapshot;
67 }
68
69 Document::Ptr QmlJSQuickFixState::document() const
70 {
71     return _semanticInfo.document;
72 }
73
74 const QmlJSRefactoringFile QmlJSQuickFixState::currentFile() const
75 {
76     return QmlJSRefactoringFile(editor(), document());
77 }
78
79 QmlJSQuickFixOperation::QmlJSQuickFixOperation(const QmlJSQuickFixState &state, int priority)
80     : QuickFixOperation(priority)
81     , _state(state)
82 {
83 }
84
85 QmlJSQuickFixOperation::~QmlJSQuickFixOperation()
86 {
87 }
88
89 void QmlJSQuickFixOperation::perform()
90 {
91     QmlJSRefactoringChanges refactoring(ExtensionSystem::PluginManager::instance()->getObject<QmlJS::ModelManagerInterface>(),
92                                     _state.snapshot());
93     QmlJSRefactoringFile current = refactoring.file(fileName());
94
95     performChanges(&current, &refactoring);
96 }
97
98 const QmlJSQuickFixState &QmlJSQuickFixOperation::state() const
99 {
100     return _state;
101 }
102
103 QString QmlJSQuickFixOperation::fileName() const
104 {
105     return state().document()->fileName();
106 }
107
108 QmlJSQuickFixFactory::QmlJSQuickFixFactory()
109 {
110 }
111
112 QmlJSQuickFixFactory::~QmlJSQuickFixFactory()
113 {
114 }
115
116 QList<QuickFixOperation::Ptr> QmlJSQuickFixFactory::matchingOperations(QuickFixState *state)
117 {
118     if (QmlJSQuickFixState *qmljsState = static_cast<QmlJSQuickFixState *>(state))
119         return match(*qmljsState);
120     else
121         return QList<TextEditor::QuickFixOperation::Ptr>();
122 }
123
124 QList<QmlJSQuickFixOperation::Ptr> QmlJSQuickFixFactory::noResult()
125 {
126     return QList<QmlJSQuickFixOperation::Ptr>();
127 }
128
129 QList<QmlJSQuickFixOperation::Ptr> QmlJSQuickFixFactory::singleResult(QmlJSQuickFixOperation *operation)
130 {
131     QList<QmlJSQuickFixOperation::Ptr> result;
132     result.append(QmlJSQuickFixOperation::Ptr(operation));
133     return result;
134 }
135
136 QmlJSQuickFixCollector::QmlJSQuickFixCollector()
137 {
138 }
139
140 QmlJSQuickFixCollector::~QmlJSQuickFixCollector()
141 {
142 }
143
144 bool QmlJSQuickFixCollector::supportsEditor(TextEditor::ITextEditor *editable) const
145 {
146     return qobject_cast<QmlJSTextEditorWidget *>(editable->widget()) != 0;
147 }
148
149 bool QmlJSQuickFixCollector::supportsPolicy(TextEditor::CompletionPolicy policy) const
150 {
151     return policy == TextEditor::QuickFixCompletion;
152 }
153
154 TextEditor::QuickFixState *QmlJSQuickFixCollector::initializeCompletion(TextEditor::BaseTextEditorWidget *editor)
155 {
156     if (QmlJSTextEditorWidget *qmljsEditor = qobject_cast<QmlJSTextEditorWidget *>(editor)) {
157         const SemanticInfo info = qmljsEditor->semanticInfo();
158
159         if (! info.isValid() || qmljsEditor->isOutdated()) {
160             // outdated
161             qWarning() << "TODO: outdated semantic info, force a reparse.";
162             return 0;
163         }
164
165         QmlJSQuickFixState *state = new QmlJSQuickFixState(editor);
166         state->_semanticInfo = info;
167         return state;
168     }
169
170     return 0;
171 }
172
173 QList<TextEditor::QuickFixFactory *> QmlJSQuickFixCollector::quickFixFactories() const
174 {
175     QList<TextEditor::QuickFixFactory *> results;
176     ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
177     foreach (QmlJSQuickFixFactory *f, pm->getObjects<QmlJSEditor::QmlJSQuickFixFactory>())
178         results.append(f);
179     return results;
180 }