diff options
author | Christian Kamm <christian.d.kamm@nokia.com> | 2011-11-04 13:59:23 +0100 |
---|---|---|
committer | Christian Kamm <christian.d.kamm@nokia.com> | 2011-11-04 14:13:17 +0100 |
commit | de6ea6c3b9b9cc318e7210d62c0c666552920c69 (patch) | |
tree | fe782d0d6dfcddb2c158e67c449d22280b33e99b /src/plugins/qmljseditor/qmljswrapinloader.cpp | |
parent | 13c3736832c4f28def2c5af9e873bf8bfc7ed54b (diff) | |
download | qt-creator-de6ea6c3b9b9cc318e7210d62c0c666552920c69.tar.gz |
QmlJS: Handle inner ids in 'Wrap in Loader' quick fix.
Change-Id: I7385f49928db78abd2deb7783ca0a38288ae7446
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
Diffstat (limited to 'src/plugins/qmljseditor/qmljswrapinloader.cpp')
-rw-r--r-- | src/plugins/qmljseditor/qmljswrapinloader.cpp | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/src/plugins/qmljseditor/qmljswrapinloader.cpp b/src/plugins/qmljseditor/qmljswrapinloader.cpp index e685883d9c..0d9c0d4e78 100644 --- a/src/plugins/qmljseditor/qmljswrapinloader.cpp +++ b/src/plugins/qmljseditor/qmljswrapinloader.cpp @@ -52,6 +52,31 @@ using namespace QmlJSTools; namespace { +class FindIds : protected Visitor +{ +public: + typedef QHash<QString, SourceLocation> Result; + + Result operator()(Node *node) + { + result.clear(); + Node::accept(node, this); + return result; + } + +protected: + virtual bool visit(UiObjectInitializer *ast) + { + UiScriptBinding *idBinding; + QString id = idOfObject(ast, &idBinding); + if (!id.isEmpty()) + result[id] = locationFromRange(idBinding->statement); + return true; + } + + Result result; +}; + class Operation: public QmlJSQuickFixOperation { UiObjectDefinition *m_objDef; @@ -100,8 +125,10 @@ public: const QString loaderId = findFreeName(QLatin1String("loader_") + baseName); Utils::ChangeSet changes; - int objDefStart = m_objDef->firstSourceLocation().begin(); - int objDefEnd = m_objDef->lastSourceLocation().end(); + + FindIds::Result innerIds = FindIds()(m_objDef); + innerIds.remove(id); + 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"); @@ -110,6 +137,27 @@ public: "// Rename all outer uses of the id '%1' to '%2.item'.\n").arg( id, loaderId); } + + // handle inner ids + QString innerIdForwarders; + QHashIterator<QString, SourceLocation> it(innerIds); + while (it.hasNext()) { + it.next(); + const QString innerId = it.key(); + comment += WrapInLoader::tr( + "// Rename all outer uses of the id '%1' to '%2.item.%1'.\n").arg( + innerId, loaderId); + changes.replace(it.value().begin(), it.value().end(), QString("inner_%1").arg(innerId)); + innerIdForwarders += QString("\nproperty alias %1: inner_%1").arg(innerId); + } + if (!innerIdForwarders.isEmpty()) { + innerIdForwarders.append(QLatin1Char('\n')); + const int afterOpenBrace = m_objDef->initializer->lbraceToken.end(); + changes.insert(afterOpenBrace, innerIdForwarders); + } + + const int objDefStart = m_objDef->firstSourceLocation().begin(); + const int objDefEnd = m_objDef->lastSourceLocation().end(); changes.insert(objDefStart, comment + QString("Component {\n" " id: %1\n").arg(componentId)); |