From dc859db169e171eb17f251f038c7ea026b8c86da Mon Sep 17 00:00:00 2001
From: Erik Verbruggen <erik.verbruggen@nokia.com>
Date: Mon, 15 Feb 2010 15:04:25 +0100
Subject: [PATCH] Fixed a bug where a property could be inserted after an
 object.

---
 .../core/filemanager/addobjectvisitor.cpp     |  2 +-
 .../core/filemanager/moveobjectvisitor.cpp    |  2 +-
 .../core/filemanager/qmlrewriter.cpp          | 34 +++++++++++++++++--
 .../core/filemanager/qmlrewriter.h            |  1 +
 4 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/src/plugins/qmldesigner/core/filemanager/addobjectvisitor.cpp b/src/plugins/qmldesigner/core/filemanager/addobjectvisitor.cpp
index 7e56a004211..ce00cbf23e1 100644
--- a/src/plugins/qmldesigner/core/filemanager/addobjectvisitor.cpp
+++ b/src/plugins/qmldesigner/core/filemanager/addobjectvisitor.cpp
@@ -72,7 +72,7 @@ bool AddObjectVisitor::visit(QmlJS::AST::UiObjectDefinition *ast)
 
 void AddObjectVisitor::insertInto(QmlJS::AST::UiObjectInitializer *ast)
 {
-    UiObjectMemberList *insertAfter = searchMemberToInsertAfter(ast->members, QString(), m_propertyOrder);
+    UiObjectMemberList *insertAfter = searchMemberToInsertAfter(ast->members, m_propertyOrder);
 
     int insertionPoint;
     int depth;
diff --git a/src/plugins/qmldesigner/core/filemanager/moveobjectvisitor.cpp b/src/plugins/qmldesigner/core/filemanager/moveobjectvisitor.cpp
index 85dee276662..0874bc46485 100644
--- a/src/plugins/qmldesigner/core/filemanager/moveobjectvisitor.cpp
+++ b/src/plugins/qmldesigner/core/filemanager/moveobjectvisitor.cpp
@@ -87,7 +87,7 @@ private:
     {
         if (targetPropertyName.isEmpty()) {
             // insert as UiObjectDefinition:
-            UiObjectMemberList *insertAfter = searchMemberToInsertAfter(ast->members, QString(), propertyOrder);
+            UiObjectMemberList *insertAfter = searchMemberToInsertAfter(ast->members, propertyOrder);
 
             if (insertAfter && insertAfter->member) {
                 moveInfo.destination = insertAfter->member->lastSourceLocation().end();
diff --git a/src/plugins/qmldesigner/core/filemanager/qmlrewriter.cpp b/src/plugins/qmldesigner/core/filemanager/qmlrewriter.cpp
index c06aa7b27ad..59813ac8a55 100644
--- a/src/plugins/qmldesigner/core/filemanager/qmlrewriter.cpp
+++ b/src/plugins/qmldesigner/core/filemanager/qmlrewriter.cpp
@@ -270,6 +270,34 @@ void QMLRewriter::includeLeadingEmptyLine(int &start) const
     start = prevBlock.position();
 }
 
+UiObjectMemberList *QMLRewriter::searchMemberToInsertAfter(UiObjectMemberList *members, const QStringList &propertyOrder)
+{
+    const int objectDefinitionInsertionPoint = propertyOrder.indexOf(QString::null);
+    UiObjectMemberList *previous = 0;
+    for (UiObjectMemberList *iter = members; iter; iter = iter->next) {
+        UiObjectMember *member = iter->member;
+        int idx = -1;
+
+        if (UiArrayBinding *arrayBinding = cast<UiArrayBinding*>(member))
+            idx = propertyOrder.indexOf(flatten(arrayBinding->qualifiedId));
+        else if (UiObjectBinding *objectBinding = cast<UiObjectBinding*>(member))
+            idx = propertyOrder.indexOf(flatten(objectBinding->qualifiedId));
+        else if (cast<UiObjectDefinition*>(member))
+            idx = propertyOrder.indexOf(QString::null);
+        else if (UiScriptBinding *scriptBinding = cast<UiScriptBinding*>(member))
+            idx = propertyOrder.indexOf(flatten(scriptBinding->qualifiedId));
+        else if (cast<UiPublicMember*>(member))
+            idx = propertyOrder.indexOf(QLatin1String("property"));
+
+        if (idx > objectDefinitionInsertionPoint)
+            return iter;
+
+        previous = iter;
+    }
+
+    return previous;
+}
+
 UiObjectMemberList *QMLRewriter::searchMemberToInsertAfter(UiObjectMemberList *members, const QString &propertyName, const QStringList &propertyOrder)
 {
     if (!members)
@@ -298,9 +326,9 @@ UiObjectMemberList *QMLRewriter::searchMemberToInsertAfter(UiObjectMemberList *m
     if (idx == -1)
         idx = propertyOrder.size() - 1;
 
-    for (; idx >= 0; --idx) {
-        const QString prop = propertyOrder.at(idx);
-        UiObjectMemberList *candidate = orderedMembers[prop];
+    for (; idx > 0; --idx) {
+        const QString prop = propertyOrder.at(idx - 1);
+        UiObjectMemberList *candidate = orderedMembers.value(prop, 0);
         if (candidate != 0)
             return candidate;
     }
diff --git a/src/plugins/qmldesigner/core/filemanager/qmlrewriter.h b/src/plugins/qmldesigner/core/filemanager/qmlrewriter.h
index 225c2a846fc..d6e7279ae02 100644
--- a/src/plugins/qmldesigner/core/filemanager/qmlrewriter.h
+++ b/src/plugins/qmldesigner/core/filemanager/qmlrewriter.h
@@ -79,6 +79,7 @@ protected:
     bool includeSurroundingWhitespace(int &start, int &end) const;
     void includeLeadingEmptyLine(int &start) const;
 
+    static QmlJS::AST::UiObjectMemberList *searchMemberToInsertAfter(QmlJS::AST::UiObjectMemberList *members, const QStringList &propertyOrder);
     static QmlJS::AST::UiObjectMemberList *searchMemberToInsertAfter(QmlJS::AST::UiObjectMemberList *members, const QString &propertyName, const QStringList &propertyOrder);
 
 protected:
-- 
GitLab