From 66eb27e68799228e3aa710da79d410d86af511d1 Mon Sep 17 00:00:00 2001 From: Fawzi Mohamed Date: Wed, 21 Nov 2012 11:05:03 +0100 Subject: [PATCH] qmljs: indent expression in if condition and indent lambda expressions * indent expressions (like function defs) in if condition fixes QTCREATORBUG-7243 Change-Id: I61197317a8dcba72a45b660600711748e2e2b962 Reviewed-by: Christian Kamm Reviewed-by: Erik Verbruggen --- src/libs/qmljs/qmljscodeformatter.cpp | 11 ++-------- src/libs/qmljs/qmljscodeformatter.h | 1 - .../qmlcodeformatter/tst_qmlcodeformatter.cpp | 22 +++++++++++++++++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/libs/qmljs/qmljscodeformatter.cpp b/src/libs/qmljs/qmljscodeformatter.cpp index 8c26a9ca420..ade21c87874 100644 --- a/src/libs/qmljs/qmljscodeformatter.cpp +++ b/src/libs/qmljs/qmljscodeformatter.cpp @@ -407,16 +407,10 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) break; case condition_open: + if (tryInsideExpression()) + break; switch (kind) { case RightParenthesis: turnInto(substatement); break; - case LeftParenthesis: enter(condition_paren_open); break; - } break; - - // paren nesting - case condition_paren_open: - switch (kind) { - case RightParenthesis: leave(); break; - case LeftParenthesis: enter(condition_paren_open); break; } break; case switch_statement: @@ -1131,7 +1125,6 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd case signal_arglist_open: case function_arglist_open: case paren_open: - case condition_paren_open: if (!lastToken) *indentDepth = tokenPosition + 1; else diff --git a/src/libs/qmljs/qmljscodeformatter.h b/src/libs/qmljs/qmljscodeformatter.h index fa00963e471..0965e0e54c8 100644 --- a/src/libs/qmljs/qmljscodeformatter.h +++ b/src/libs/qmljs/qmljscodeformatter.h @@ -154,7 +154,6 @@ public: // must be public to make Q_GADGET introspection work else_clause, // The else line of an if-else construct. condition_open, // Start of a condition in 'if', 'while', entered after opening paren - condition_paren_open, // After an lparen in a condition substatement, // The first line after a conditional or loop construct. substatement_open, // The brace that opens a substatement block. diff --git a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp index b59c8359dd6..b1fed72441b 100644 --- a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp +++ b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp @@ -75,7 +75,8 @@ private Q_SLOTS: void ifStatementWithBraces3(); void ifStatementMixed(); void ifStatementAndComments(); - void ifStatementLongCondition(); + void ifStatementLongCondition1(); + void ifStatementLongCondition2(); void moreIfThenElse(); void strayElse(); void oneLineIf(); @@ -941,7 +942,7 @@ void tst_QMLCodeFormatter::ifStatementAndComments() checkIndent(data); } -void tst_QMLCodeFormatter::ifStatementLongCondition() +void tst_QMLCodeFormatter::ifStatementLongCondition1() { QList data; data << Line("Rectangle {") @@ -958,6 +959,23 @@ void tst_QMLCodeFormatter::ifStatementLongCondition() checkIndent(data); } +void tst_QMLCodeFormatter::ifStatementLongCondition2() +{ + QList data; + data << Line("function test() {") + << Line(" if (foo(function() {") + << Line(" if (xx) {") + << Line(" yy = yy - 1") + << Line(" } else {") + << Line(" yy = yy + 1") + << Line(" }") + << Line(" })) {") + << Line(" zz = !zz") + << Line(" }") + << Line("}"); + checkIndent(data); +} + void tst_QMLCodeFormatter::strayElse() { QList data;