OSDN Git Service

QmlJS: Add quick fix to wrap element in Loader.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljseditor / qmljswrapinloader.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
30 **
31 **************************************************************************/
32
33 #include "qmljswrapinloader.h"
34 #include "qmljsquickfixassist.h"
35
36 #include <coreplugin/ifile.h>
37
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>
43
44 #include <QtCore/QDir>
45 #include <QtCore/QFileInfo>
46
47 using namespace QmlJS;
48 using namespace QmlJS::AST;
49 using namespace QmlJSEditor;
50 using namespace QmlJSEditor::Internal;
51 using namespace QmlJSTools;
52
53 namespace {
54
55 class Operation: public QmlJSQuickFixOperation
56 {
57     UiObjectDefinition *m_objDef;
58
59 public:
60     Operation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
61               UiObjectDefinition *objDef)
62         : QmlJSQuickFixOperation(interface, 0)
63         , m_objDef(objDef)
64     {
65         Q_ASSERT(m_objDef != 0);
66
67         setDescription(WrapInLoader::tr("Wrap Component in Loader"));
68     }
69
70     QString findFreeName(const QString &base)
71     {
72         QString tryName = base;
73         int extraNumber = 1;
74         const ObjectValue *found = 0;
75         const ScopeChain &scope = assistInterface()->semanticInfo().scopeChain();
76         forever {
77             scope.lookup(tryName, &found);
78             if (!found || extraNumber > 1000)
79                 break;
80             tryName = base + QString::number(extraNumber++);
81         }
82         return tryName;
83     }
84
85     virtual void performChanges(QmlJSRefactoringFilePtr currentFile,
86                                 const QmlJSRefactoringChanges &)
87     {
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) {
93                 if (!it->next)
94                     baseName = it->name.toString();
95             }
96         }
97
98         // find ids
99         const QString componentId = findFreeName(QLatin1String("component_") + baseName);
100         const QString loaderId = findFreeName(QLatin1String("loader_") + baseName);
101
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");
108         if (idBinding) {
109             comment += WrapInLoader::tr(
110                         "//       Rename all outer uses of the id '%1' to '%2.item'.\n").arg(
111                         id, loaderId);
112         }
113         changes.insert(objDefStart, comment +
114                        QString("Component {\n"
115                                "    id: %1\n").arg(componentId));
116         changes.insert(objDefEnd, QString("\n"
117                                           "}\n"
118                                           "Loader {\n"
119                                           "    id: %2\n"
120                                           "    sourceComponent: %1\n"
121                                           "}\n").arg(componentId, loaderId));
122         currentFile->setChangeSet(changes);
123         currentFile->appendIndentRange(Range(objDefStart, objDefEnd));
124         currentFile->apply();
125     }
126 };
127
128 } // end of anonymous namespace
129
130
131 QList<QmlJSQuickFixOperation::Ptr> WrapInLoader::match(
132     const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface)
133 {
134     const int pos = interface->currentFile()->cursor().position();
135
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))
141                 return noResult();
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));
145             }
146         }
147     }
148
149     return noResult();
150 }