1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "qmljsquickfix.h"
35 #include "qmljscomponentfromobjectdef.h"
36 #include "qmljseditor.h"
37 #include "qmljs/parser/qmljsast_p.h"
39 #include <extensionsystem/iplugin.h>
40 #include <extensionsystem/pluginmanager.h>
42 #include <qmljs/qmljsmodelmanagerinterface.h>
44 #include <QtCore/QDebug>
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;
54 QmlJSQuickFixState::QmlJSQuickFixState(TextEditor::BaseTextEditorWidget *editor)
55 : QuickFixState(editor)
59 SemanticInfo QmlJSQuickFixState::semanticInfo() const
64 Snapshot QmlJSQuickFixState::snapshot() const
66 return _semanticInfo.snapshot;
69 Document::Ptr QmlJSQuickFixState::document() const
71 return _semanticInfo.document;
74 const QmlJSRefactoringFile QmlJSQuickFixState::currentFile() const
76 return QmlJSRefactoringFile(editor(), document());
79 QmlJSQuickFixOperation::QmlJSQuickFixOperation(const QmlJSQuickFixState &state, int priority)
80 : QuickFixOperation(priority)
85 QmlJSQuickFixOperation::~QmlJSQuickFixOperation()
89 void QmlJSQuickFixOperation::perform()
91 QmlJSRefactoringChanges refactoring(ExtensionSystem::PluginManager::instance()->getObject<QmlJS::ModelManagerInterface>(),
93 QmlJSRefactoringFile current = refactoring.file(fileName());
95 performChanges(¤t, &refactoring);
98 const QmlJSQuickFixState &QmlJSQuickFixOperation::state() const
103 QString QmlJSQuickFixOperation::fileName() const
105 return state().document()->fileName();
108 QmlJSQuickFixFactory::QmlJSQuickFixFactory()
112 QmlJSQuickFixFactory::~QmlJSQuickFixFactory()
116 QList<QuickFixOperation::Ptr> QmlJSQuickFixFactory::matchingOperations(QuickFixState *state)
118 if (QmlJSQuickFixState *qmljsState = static_cast<QmlJSQuickFixState *>(state))
119 return match(*qmljsState);
121 return QList<TextEditor::QuickFixOperation::Ptr>();
124 QList<QmlJSQuickFixOperation::Ptr> QmlJSQuickFixFactory::noResult()
126 return QList<QmlJSQuickFixOperation::Ptr>();
129 QList<QmlJSQuickFixOperation::Ptr> QmlJSQuickFixFactory::singleResult(QmlJSQuickFixOperation *operation)
131 QList<QmlJSQuickFixOperation::Ptr> result;
132 result.append(QmlJSQuickFixOperation::Ptr(operation));
136 QmlJSQuickFixCollector::QmlJSQuickFixCollector()
140 QmlJSQuickFixCollector::~QmlJSQuickFixCollector()
144 bool QmlJSQuickFixCollector::supportsEditor(TextEditor::ITextEditor *editable) const
146 return qobject_cast<QmlJSTextEditorWidget *>(editable->widget()) != 0;
149 bool QmlJSQuickFixCollector::supportsPolicy(TextEditor::CompletionPolicy policy) const
151 return policy == TextEditor::QuickFixCompletion;
154 TextEditor::QuickFixState *QmlJSQuickFixCollector::initializeCompletion(TextEditor::BaseTextEditorWidget *editor)
156 if (QmlJSTextEditorWidget *qmljsEditor = qobject_cast<QmlJSTextEditorWidget *>(editor)) {
157 const SemanticInfo info = qmljsEditor->semanticInfo();
159 if (! info.isValid() || qmljsEditor->isOutdated()) {
161 qWarning() << "TODO: outdated semantic info, force a reparse.";
165 QmlJSQuickFixState *state = new QmlJSQuickFixState(editor);
166 state->_semanticInfo = info;
173 QList<TextEditor::QuickFixFactory *> QmlJSQuickFixCollector::quickFixFactories() const
175 QList<TextEditor::QuickFixFactory *> results;
176 ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
177 foreach (QmlJSQuickFixFactory *f, pm->getObjects<QmlJSEditor::QmlJSQuickFixFactory>())