QmlJS: Handle inner ids in 'Wrap in Loader' quick fix.

Change-Id: I7385f49928db78abd2deb7783ca0a38288ae7446
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
This commit is contained in:
Christian Kamm
2011-11-04 13:59:23 +01:00
parent 13c3736832
commit de6ea6c3b9
3 changed files with 62 additions and 6 deletions

View File

@@ -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));