QmlJS indenter: Improve indentation of function expressions.

As discussed with Tor Arne and Fawzi in
http://codereview.qt-project.org/#change,18080

Change-Id: Ia0482d2ed3ddd61fa002e2c76b948301af52a795
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@nokia.com>
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@nokia.com>
This commit is contained in:
Christian Kamm
2012-03-01 08:35:21 +01:00
committed by Fawzi Mohamed
parent 9705001414
commit ab64c1aef3
3 changed files with 35 additions and 27 deletions

View File

@@ -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:

View File

@@ -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<Line> 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<Line> data;

View File

@@ -4,8 +4,8 @@ var x = {
x: 12,
y: "abc",
z: function (x) {
return a
},
return a
},
abc: 15
},
z: 12