diff --git a/src/libs/qmljs/qmljsutils.cpp b/src/libs/qmljs/qmljsutils.cpp index fe5bd1967da408d6e9c57ca1a631231b9829ef81..956196e5f3d90af74bb05920869537587bb1cc39 100644 --- a/src/libs/qmljs/qmljsutils.cpp +++ b/src/libs/qmljs/qmljsutils.cpp @@ -96,20 +96,29 @@ SourceLocation QmlJS::fullLocationForQualifiedId(AST::UiQualifiedId *qualifiedId return locationFromRange(start, end); } -QString QmlJS::idOfObject(UiObjectDefinition *object) +QString QmlJS::idOfObject(UiObjectDefinition *object, UiScriptBinding **idBinding) { - if (!object) + if (!object) { + if (idBinding) + *idBinding = 0; return QString(); - return idOfObject(object->initializer); + } + return idOfObject(object->initializer, idBinding); } -QString QmlJS::idOfObject(UiObjectBinding *object) +QString QmlJS::idOfObject(UiObjectBinding *object, UiScriptBinding **idBinding) { - if (!object) + if (!object) { + if (idBinding) + *idBinding = 0; return QString(); - return idOfObject(object->initializer); + } + return idOfObject(object->initializer, idBinding); } -QString QmlJS::idOfObject(UiObjectInitializer *initializer) +QString QmlJS::idOfObject(UiObjectInitializer *initializer, UiScriptBinding **idBinding) { + if (idBinding) + *idBinding = 0; + if (!initializer) return QString(); @@ -123,6 +132,8 @@ QString QmlJS::idOfObject(UiObjectInitializer *initializer) continue; if (ExpressionStatement *expstmt = cast<ExpressionStatement *>(script->statement)) { if (IdentifierExpression *idexp = cast<IdentifierExpression *>(expstmt->expression)) { + if (idBinding) + *idBinding = script; return idexp->name.toString(); } } diff --git a/src/libs/qmljs/qmljsutils.h b/src/libs/qmljs/qmljsutils.h index 776b9a45e65c268dd5facb2051709e83ac2c51d1..edca7239ce8f0390e24c23af4b0e2c140aa607f3 100644 --- a/src/libs/qmljs/qmljsutils.h +++ b/src/libs/qmljs/qmljsutils.h @@ -18,9 +18,9 @@ QMLJS_EXPORT AST::SourceLocation locationFromRange(const AST::SourceLocation &st QMLJS_EXPORT AST::SourceLocation fullLocationForQualifiedId(AST::UiQualifiedId *); -QMLJS_EXPORT QString idOfObject(AST::UiObjectDefinition *object); -QMLJS_EXPORT QString idOfObject(AST::UiObjectBinding *object); -QMLJS_EXPORT QString idOfObject(AST::UiObjectInitializer *initializer); +QMLJS_EXPORT QString idOfObject(AST::UiObjectDefinition *object, AST::UiScriptBinding **idBinding = 0); +QMLJS_EXPORT QString idOfObject(AST::UiObjectBinding *object, AST::UiScriptBinding **idBinding = 0); +QMLJS_EXPORT QString idOfObject(AST::UiObjectInitializer *initializer, AST::UiScriptBinding **idBinding = 0); QMLJS_EXPORT AST::UiObjectInitializer *initializerOfObject(AST::Node *node); QMLJS_EXPORT AST::UiQualifiedId *qualifiedTypeNameId(AST::Node *node); diff --git a/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp b/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp index 677bf12c5c475ca584e4daae4f0c3696ca8d223d..83f94e72ff1a8775bf073b6caa90de80855f177c 100644 --- a/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp +++ b/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp @@ -80,7 +80,7 @@ public: { QString componentName = m_componentName; QString path = QFileInfo(fileName()).path(); - ComponentNameDialog::go(&componentName, &path, assistInterface()->widget()); + ComponentNameDialog::go(&componentName, &path, assistInterface()->editor()); if (componentName.isEmpty() || path.isEmpty()) return; diff --git a/src/plugins/qmljseditor/qmljseditor.pro b/src/plugins/qmljseditor/qmljseditor.pro index 89b60b91e5ae20017a33e154fa7339a0f7f3e520..2510b41c48ca70552bd570f06734d10b060a2fdd 100644 --- a/src/plugins/qmljseditor/qmljseditor.pro +++ b/src/plugins/qmljseditor/qmljseditor.pro @@ -37,7 +37,8 @@ HEADERS += \ qmljscompletionassist.h \ qmljsquickfix.h \ qmljssemanticinfoupdater.h \ - qmljssemantichighlighter.h + qmljssemantichighlighter.h \ + qmljswrapinloader.h SOURCES += \ qmljseditor.cpp \ @@ -68,7 +69,8 @@ SOURCES += \ qmljscompletionassist.cpp \ qmljsquickfix.cpp \ qmljssemanticinfoupdater.cpp \ - qmljssemantichighlighter.cpp + qmljssemantichighlighter.cpp \ + qmljswrapinloader.cpp RESOURCES += qmljseditor.qrc OTHER_FILES += QmlJSEditor.mimetypes.xml diff --git a/src/plugins/qmljseditor/qmljsquickfixassist.cpp b/src/plugins/qmljseditor/qmljsquickfixassist.cpp index 0d30dae87375b66593911876e052fd319d3f4afa..30a0dda51d74863174dd243724ffb938d4310bd2 100644 --- a/src/plugins/qmljseditor/qmljsquickfixassist.cpp +++ b/src/plugins/qmljseditor/qmljsquickfixassist.cpp @@ -67,7 +67,7 @@ QmlJSRefactoringFilePtr QmlJSQuickFixAssistInterface::currentFile() const return m_currentFile; } -QWidget *QmlJSQuickFixAssistInterface::widget() const +QmlJSTextEditorWidget *QmlJSQuickFixAssistInterface::editor() const { return m_editor; } diff --git a/src/plugins/qmljseditor/qmljsquickfixassist.h b/src/plugins/qmljseditor/qmljsquickfixassist.h index 50d2ec00e5bd367501cab40c8f09265c46b1893b..51a21e35b9fcc7ed0da33631cbe0afcbfd561c01 100644 --- a/src/plugins/qmljseditor/qmljsquickfixassist.h +++ b/src/plugins/qmljseditor/qmljsquickfixassist.h @@ -52,7 +52,7 @@ public: const SemanticInfo &semanticInfo() const; QmlJSTools::QmlJSRefactoringFilePtr currentFile() const; - QWidget *widget() const; + QmlJSTextEditorWidget *editor() const; private: QmlJSTextEditorWidget *m_editor; diff --git a/src/plugins/qmljseditor/qmljsquickfixes.cpp b/src/plugins/qmljseditor/qmljsquickfixes.cpp index 394e8c1d1c50e3bd4a993a2a7236c3330d3699a4..d5effeea93c131ba232d954a83252b47db1164b1 100644 --- a/src/plugins/qmljseditor/qmljsquickfixes.cpp +++ b/src/plugins/qmljseditor/qmljsquickfixes.cpp @@ -32,6 +32,7 @@ #include "qmljsquickfix.h" #include "qmljscomponentfromobjectdef.h" +#include "qmljswrapinloader.h" #include "qmljseditor.h" #include "qmljsquickfixassist.h" @@ -138,4 +139,5 @@ void registerQuickFixes(ExtensionSystem::IPlugin *plugIn) { plugIn->addAutoReleasedObject(new SplitInitializerOp); plugIn->addAutoReleasedObject(new ComponentFromObjectDef); + plugIn->addAutoReleasedObject(new WrapInLoader); } diff --git a/src/plugins/qmljseditor/qmljswrapinloader.cpp b/src/plugins/qmljseditor/qmljswrapinloader.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e685883d9ca3a5b14bce490648a578208c4b4fc0 --- /dev/null +++ b/src/plugins/qmljseditor/qmljswrapinloader.cpp @@ -0,0 +1,150 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (info@qt.nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** 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. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at info@qt.nokia.com. +** +**************************************************************************/ + +#include "qmljswrapinloader.h" +#include "qmljsquickfixassist.h" + +#include <coreplugin/ifile.h> + +#include <qmljs/parser/qmljsast_p.h> +#include <qmljs/qmljsdocument.h> +#include <qmljs/qmljsutils.h> +#include <qmljs/qmljsbind.h> +#include <qmljstools/qmljsrefactoringchanges.h> + +#include <QtCore/QDir> +#include <QtCore/QFileInfo> + +using namespace QmlJS; +using namespace QmlJS::AST; +using namespace QmlJSEditor; +using namespace QmlJSEditor::Internal; +using namespace QmlJSTools; + +namespace { + +class Operation: public QmlJSQuickFixOperation +{ + UiObjectDefinition *m_objDef; + +public: + Operation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface, + UiObjectDefinition *objDef) + : QmlJSQuickFixOperation(interface, 0) + , m_objDef(objDef) + { + Q_ASSERT(m_objDef != 0); + + setDescription(WrapInLoader::tr("Wrap Component in Loader")); + } + + QString findFreeName(const QString &base) + { + QString tryName = base; + int extraNumber = 1; + const ObjectValue *found = 0; + const ScopeChain &scope = assistInterface()->semanticInfo().scopeChain(); + forever { + scope.lookup(tryName, &found); + if (!found || extraNumber > 1000) + break; + tryName = base + QString::number(extraNumber++); + } + return tryName; + } + + virtual void performChanges(QmlJSRefactoringFilePtr currentFile, + const QmlJSRefactoringChanges &) + { + UiScriptBinding *idBinding; + const QString id = idOfObject(m_objDef, &idBinding); + QString baseName = id; + if (baseName.isEmpty()) { + for (UiQualifiedId *it = m_objDef->qualifiedTypeNameId; it; it = it->next) { + if (!it->next) + baseName = it->name.toString(); + } + } + + // find ids + const QString componentId = findFreeName(QLatin1String("component_") + baseName); + const QString loaderId = findFreeName(QLatin1String("loader_") + baseName); + + Utils::ChangeSet changes; + int objDefStart = m_objDef->firstSourceLocation().begin(); + int objDefEnd = m_objDef->lastSourceLocation().end(); + QString comment = WrapInLoader::tr( + "// TODO: Move position bindings from the component to the Loader.\n" + "// Check all uses of 'parent' inside the root element of the component.\n"); + if (idBinding) { + comment += WrapInLoader::tr( + "// Rename all outer uses of the id '%1' to '%2.item'.\n").arg( + id, loaderId); + } + changes.insert(objDefStart, comment + + QString("Component {\n" + " id: %1\n").arg(componentId)); + changes.insert(objDefEnd, QString("\n" + "}\n" + "Loader {\n" + " id: %2\n" + " sourceComponent: %1\n" + "}\n").arg(componentId, loaderId)); + currentFile->setChangeSet(changes); + currentFile->appendIndentRange(Range(objDefStart, objDefEnd)); + currentFile->apply(); + } +}; + +} // end of anonymous namespace + + +QList<QmlJSQuickFixOperation::Ptr> WrapInLoader::match( + const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface) +{ + const int pos = interface->currentFile()->cursor().position(); + + QList<Node *> path = interface->semanticInfo().rangePath(pos); + for (int i = path.size() - 1; i >= 0; --i) { + Node *node = path.at(i); + if (UiObjectDefinition *objDef = cast<UiObjectDefinition *>(node)) { + if (!interface->currentFile()->isCursorOn(objDef->qualifiedTypeNameId)) + return noResult(); + // check that the node is not the root node + if (i > 0 && !cast<UiProgram*>(path.at(i - 1))) { + return singleResult(new Operation(interface, objDef)); + } + } + } + + return noResult(); +} diff --git a/src/plugins/qmljseditor/qmljswrapinloader.h b/src/plugins/qmljseditor/qmljswrapinloader.h new file mode 100644 index 0000000000000000000000000000000000000000..2d33d94c5b946b576af6ba8ea8d15e5d932472e3 --- /dev/null +++ b/src/plugins/qmljseditor/qmljswrapinloader.h @@ -0,0 +1,51 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (info@qt.nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** 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. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at info@qt.nokia.com. +** +**************************************************************************/ + +#ifndef QMLJSWRAPINLOADER_H +#define QMLJSWRAPINLOADER_H + +#include "qmljsquickfix.h" + +namespace QmlJSEditor { +namespace Internal { + +class WrapInLoader: public QmlJSQuickFixFactory +{ +public: + virtual QList<QmlJSQuickFixOperation::Ptr> match( + const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface); +}; + +} // namespace Internal +} // namespace QmlJSEditor + +#endif // QMLJSWRAPINLOADER_H