1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
31 **************************************************************************/
33 #include "qmljswrapinloader.h"
34 #include "qmljsquickfixassist.h"
36 #include <coreplugin/ifile.h>
38 #include <qmljs/parser/qmljsast_p.h>
39 #include <qmljs/qmljsdocument.h>
40 #include <qmljs/qmljsutils.h>
41 #include <qmljs/qmljsbind.h>
42 #include <qmljstools/qmljsrefactoringchanges.h>
44 #include <QtCore/QDir>
45 #include <QtCore/QFileInfo>
47 using namespace QmlJS;
48 using namespace QmlJS::AST;
49 using namespace QmlJSEditor;
50 using namespace QmlJSEditor::Internal;
51 using namespace QmlJSTools;
55 class Operation: public QmlJSQuickFixOperation
57 UiObjectDefinition *m_objDef;
60 Operation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
61 UiObjectDefinition *objDef)
62 : QmlJSQuickFixOperation(interface, 0)
65 Q_ASSERT(m_objDef != 0);
67 setDescription(WrapInLoader::tr("Wrap Component in Loader"));
70 QString findFreeName(const QString &base)
72 QString tryName = base;
74 const ObjectValue *found = 0;
75 const ScopeChain &scope = assistInterface()->semanticInfo().scopeChain();
77 scope.lookup(tryName, &found);
78 if (!found || extraNumber > 1000)
80 tryName = base + QString::number(extraNumber++);
85 virtual void performChanges(QmlJSRefactoringFilePtr currentFile,
86 const QmlJSRefactoringChanges &)
88 UiScriptBinding *idBinding;
89 const QString id = idOfObject(m_objDef, &idBinding);
90 QString baseName = id;
91 if (baseName.isEmpty()) {
92 for (UiQualifiedId *it = m_objDef->qualifiedTypeNameId; it; it = it->next) {
94 baseName = it->name.toString();
99 const QString componentId = findFreeName(QLatin1String("component_") + baseName);
100 const QString loaderId = findFreeName(QLatin1String("loader_") + baseName);
102 Utils::ChangeSet changes;
103 int objDefStart = m_objDef->firstSourceLocation().begin();
104 int objDefEnd = m_objDef->lastSourceLocation().end();
105 QString comment = WrapInLoader::tr(
106 "// TODO: Move position bindings from the component to the Loader.\n"
107 "// Check all uses of 'parent' inside the root element of the component.\n");
109 comment += WrapInLoader::tr(
110 "// Rename all outer uses of the id '%1' to '%2.item'.\n").arg(
113 changes.insert(objDefStart, comment +
114 QString("Component {\n"
115 " id: %1\n").arg(componentId));
116 changes.insert(objDefEnd, QString("\n"
120 " sourceComponent: %1\n"
121 "}\n").arg(componentId, loaderId));
122 currentFile->setChangeSet(changes);
123 currentFile->appendIndentRange(Range(objDefStart, objDefEnd));
124 currentFile->apply();
128 } // end of anonymous namespace
131 QList<QmlJSQuickFixOperation::Ptr> WrapInLoader::match(
132 const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface)
134 const int pos = interface->currentFile()->cursor().position();
136 QList<Node *> path = interface->semanticInfo().rangePath(pos);
137 for (int i = path.size() - 1; i >= 0; --i) {
138 Node *node = path.at(i);
139 if (UiObjectDefinition *objDef = cast<UiObjectDefinition *>(node)) {
140 if (!interface->currentFile()->isCursorOn(objDef->qualifiedTypeNameId))
142 // check that the node is not the root node
143 if (i > 0 && !cast<UiProgram*>(path.at(i - 1))) {
144 return singleResult(new Operation(interface, objDef));