Commit de6ea6c3 authored by Christian Kamm's avatar Christian Kamm
Browse files

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



Change-Id: I7385f49928db78abd2deb7783ca0a38288ae7446
Reviewed-by: default avatarRoberto Raggi <roberto.raggi@nokia.com>
parent 13c37368
......@@ -106,8 +106,11 @@ QString QmlJS::idOfObject(Node *object, UiScriptBinding **idBinding)
*idBinding = 0;
UiObjectInitializer *initializer = initializerOfObject(object);
if (!initializer)
return QString();
if (!initializer) {
initializer = cast<UiObjectInitializer *>(object);
if (!initializer)
return QString();
}
for (UiObjectMemberList *iter = initializer->members; iter; iter = iter->next) {
if (UiScriptBinding *script = cast<UiScriptBinding*>(iter->member)) {
......
......@@ -29,12 +29,17 @@ QMLJS_EXPORT bool isValidBuiltinPropertyType(const QString &name);
QMLJS_EXPORT DiagnosticMessage errorMessage(const AST::SourceLocation &loc,
const QString &message);
template <class T>
AST::SourceLocation locationFromRange(const T *node)
{
return locationFromRange(node->firstSourceLocation(), node->lastSourceLocation());
}
template <class T>
DiagnosticMessage errorMessage(const T *node, const QString &message)
{
return DiagnosticMessage(DiagnosticMessage::Error,
locationFromRange(node->firstSourceLocation(),
node->lastSourceLocation()),
locationFromRange(node),
message);
}
......
......@@ -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));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment