From ab64c1aef355deb4031d78ba57aefbf8b5f064d0 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Thu, 1 Mar 2012 08:35:21 +0100 Subject: [PATCH] QmlJS indenter: Improve indentation of function expressions. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As discussed with Tor Arne and Fawzi in http://codereview.qt-project.org/#change,18080 Change-Id: Ia0482d2ed3ddd61fa002e2c76b948301af52a795 Reviewed-by: Tor Arne Vestbø Reviewed-by: Fawzi Mohamed --- src/libs/qmljs/qmljscodeformatter.cpp | 23 ++---------- .../qmlcodeformatter/tst_qmlcodeformatter.cpp | 35 ++++++++++++++++--- tests/auto/qml/reformatter/objectliteral.js | 4 +-- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/libs/qmljs/qmljscodeformatter.cpp b/src/libs/qmljs/qmljscodeformatter.cpp index 1ef3c80c085..7c64c9058a9 100644 --- a/src/libs/qmljs/qmljscodeformatter.cpp +++ b/src/libs/qmljs/qmljscodeformatter.cpp @@ -1124,27 +1124,10 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd } break; - case function_start: { - // in these states, align to the 'function' keyword - const int parentType = parentState.type; - if (parentType == objectdefinition_open - || parentType == paren_open - || parentType == bracket_open) { - *indentDepth = tokenPosition; - *savedIndentDepth = *indentDepth; - break; - } - - // otherwise find the enclosing expression end state and align to that - for (int i = 1; state(i).type != topmost_intro; ++i) { - const int type = state(i).type; - if (isExpressionEndState(type)) { - *indentDepth = state(i - 1).savedIndentDepth; - break; - } - } + case function_start: + // align to the beginning of the line + *savedIndentDepth = *indentDepth = column(tokenAt(0).begin()); break; - } case do_statement_while_paren_open: case statement_with_condition_paren_open: diff --git a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp index 82cefa729be..5afbb77b96e 100644 --- a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp +++ b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp @@ -59,6 +59,7 @@ private Q_SLOTS: void functionExpression3(); void functionExpression4(); void functionExpression5(); + void functionExpression6(); void propertyDeclarations(); void signalDeclarations(); void ifBinding1(); @@ -398,12 +399,12 @@ void tst_QMLCodeFormatter::functionExpression2() data << Line("Rectangle {") << Line(" function foo(a, b, c) {") << Line(" foo(1, 123456, function() {") - << Line(" b;") - << Line(" })") + << Line(" b;") + << Line(" })") << Line(" foo(1, 123456, function()") - << Line(" {") - << Line(" b;") - << Line(" })") + << Line(" {") + << Line(" b;") + << Line(" })") << Line(" foobar(1, 123456,") << Line(" function () {") << Line(" b;") @@ -488,6 +489,30 @@ void tst_QMLCodeFormatter::functionExpression5() checkIndent(data); } +void tst_QMLCodeFormatter::functionExpression6() +{ + QList data; + data << Line("Rectangle {") + << Line(" property var foo: {") + << Line(" functioncall(1, 2, function() {") + << Line(" a()") + << Line(" }, function() {") + << Line(" a()") + << Line(" }, 6,") + << Line(" 7)") + << Line(" functioncall(1, 2,") + << Line(" function() {") + << Line(" a()") + << Line(" }, 3, function() {") + << Line(" a()") + << Line(" }, 6,") + << Line(" 7)") + << Line(" }") + << Line("}") + ; + checkIndent(data); +} + void tst_QMLCodeFormatter::propertyDeclarations() { QList data; diff --git a/tests/auto/qml/reformatter/objectliteral.js b/tests/auto/qml/reformatter/objectliteral.js index 478d550573c..9c8bfc46f1d 100644 --- a/tests/auto/qml/reformatter/objectliteral.js +++ b/tests/auto/qml/reformatter/objectliteral.js @@ -4,8 +4,8 @@ var x = { x: 12, y: "abc", z: function (x) { - return a - }, + return a + }, abc: 15 }, z: 12