OSDN Git Service

QmlJS indenter: Improve indentation of function literals.
authorChristian Kamm <christian.d.kamm@nokia.com>
Mon, 17 Oct 2011 11:58:00 +0000 (13:58 +0200)
committerChristian Kamm <christian.d.kamm@nokia.com>
Wed, 19 Oct 2011 06:33:28 +0000 (08:33 +0200)
Change-Id: Ic79745eefbf37e42d4dd42e73d3752bed34bbfa9
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@nokia.com>
src/libs/qmljs/qmljscodeformatter.cpp
tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp

index 5bbc32c..782e0f3 100644 (file)
@@ -1091,6 +1091,10 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
             // undo the continuation indent of the expression
             *indentDepth = parentState.savedIndentDepth;
             *savedIndentDepth = *indentDepth;
+        } else {
+            // always align to function keyword
+            *indentDepth = tokenPosition;
+            *savedIndentDepth = *indentDepth;
         }
         break;
 
index 6b8a86f..046540b 100644 (file)
@@ -54,7 +54,8 @@ private Q_SLOTS:
     void objectBinding();
     void arrayBinding();
     void functionDeclaration();
-    void functionExpression();
+    void functionExpression1();
+    void functionExpression2();
     void propertyDeclarations();
     void signalDeclarations();
     void ifBinding1();
@@ -358,7 +359,7 @@ void tst_QMLCodeFormatter::functionDeclaration()
     checkIndent(data);
 }
 
-void tst_QMLCodeFormatter::functionExpression()
+void tst_QMLCodeFormatter::functionExpression1()
 {
     QList<Line> data;
     data << Line("Rectangle {")
@@ -377,6 +378,32 @@ void tst_QMLCodeFormatter::functionExpression()
     checkIndent(data);
 }
 
+void tst_QMLCodeFormatter::functionExpression2()
+{
+    QList<Line> data;
+    data << Line("Rectangle {")
+         << Line("    function foo(a, b, c) {")
+         << Line("        foo(1, 123456, function() {")
+         << Line("                           b;")
+         << Line("                       })")
+         << Line("        foo(1, 123456, function()")
+         << Line("                       {")
+         << Line("                           b;")
+         << Line("                       })")
+         << Line("        foobar(1, 123456,")
+         << Line("               function () {")
+         << Line("                   b;")
+         << Line("               })")
+         << Line("        var x = doo_foobar(")
+         << Line("                    function () {")
+         << Line("                        b;")
+         << Line("                    })")
+         << Line("    }")
+         << Line("}")
+         ;
+    checkIndent(data);
+}
+
 void tst_QMLCodeFormatter::propertyDeclarations()
 {
     QList<Line> data;