From ea241063736f16670d5cfd2af757df13db3a7d2e Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Fri, 14 Oct 2011 12:52:42 +0200 Subject: [PATCH] QmlJS indenter: Fix ternary multiline indent. Task-number: QTCREATORBUG-6208 Change-Id: I7e5267291bc01226b5339cb4abdbb15856c58477 Reviewed-by: Leandro T. C. Melo --- src/libs/qmljs/qmljscodeformatter.cpp | 23 +++++++++------------ src/libs/qmljs/qmljscodeformatter.h | 1 + .../qmljstools/qmljsqtstylecodeformatter.cpp | 7 ++----- .../qmlcodeformatter/tst_qmlcodeformatter.cpp | 24 ++++++++++++++++++++++ 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/libs/qmljs/qmljscodeformatter.cpp b/src/libs/qmljs/qmljscodeformatter.cpp index b02075e4f8..c884e56d5b 100644 --- a/src/libs/qmljs/qmljscodeformatter.cpp +++ b/src/libs/qmljs/qmljscodeformatter.cpp @@ -258,6 +258,14 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) default: enter(expression); continue; } break; + case ternary_op: + if (kind == Colon) { + enter(ternary_op_after_colon); + enter(expression_continuation); + break; + } + // fallthrough + case ternary_op_after_colon: case expression: if (tryInsideExpression()) break; @@ -332,18 +340,6 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) default: leave(); continue; } break; - case ternary_op: - if (tryInsideExpression()) - break; - switch (kind) { - case RightParenthesis: - case RightBracket: - case RightBrace: - case Comma: - case Semicolon: leave(); continue; - case Colon: enter(expression); break; // entering expression makes maybe_continuation work - } break; - case jsblock_open: case substatement_open: if (tryStatement()) @@ -494,7 +490,8 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) // some states might be continued on the next line if (topState == expression || topState == expression_or_objectdefinition - || topState == objectliteral_assignment) { + || topState == objectliteral_assignment + || topState == ternary_op_after_colon) { enter(expression_maybe_continuation); } // multi-line comment start? diff --git a/src/libs/qmljs/qmljscodeformatter.h b/src/libs/qmljs/qmljscodeformatter.h index 4a1f372a6d..7af62d5485 100644 --- a/src/libs/qmljs/qmljscodeformatter.h +++ b/src/libs/qmljs/qmljscodeformatter.h @@ -145,6 +145,7 @@ public: // must be public to make Q_GADGET introspection work bracket_element_maybe_objectdefinition, // after an identifier in bracket_element_start ternary_op, // The ? : operator + ternary_op_after_colon, // after the : in a ternary jsblock_open, diff --git a/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp b/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp index 4b809d89ee..176c016b42 100644 --- a/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp +++ b/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp @@ -139,18 +139,15 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd if (*indentDepth == tokenPosition) { // expression_or_objectdefinition doesn't want the indent // expression_or_label already has it - // ternary already adjusts indents nicely if (parentState.type != expression_or_objectdefinition && parentState.type != expression_or_label - && parentState.type != binding_assignment - && parentState.type != ternary_op) { + && parentState.type != binding_assignment) { *indentDepth += 2*m_indentSize; } } // expression_or_objectdefinition and expression_or_label have already consumed the first token else if (parentState.type != expression_or_objectdefinition - && parentState.type != expression_or_label - && parentState.type != ternary_op) { + && parentState.type != expression_or_label) { *indentDepth = tokenPosition; } break; diff --git a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp index 3dcfbdf91b..cc90b0b627 100644 --- a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp +++ b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp @@ -94,6 +94,7 @@ private Q_SLOTS: void labelledStatements1(); void labelledStatements2(); void labelledStatements3(); + void multilineTernaryInProperty(); }; struct Line { @@ -1198,6 +1199,29 @@ void tst_QMLCodeFormatter::labelledStatements3() checkIndent(data); } +void tst_QMLCodeFormatter::multilineTernaryInProperty() +{ + QList data; + data << Line("Item {") + << Line(" property int a: 1 ?") + << Line(" 2 :") + << Line(" 3 +") + << Line(" 4") + << Line(" property int a: 1 ? 2") + << Line(" : 3 +") + << Line(" 4") + << Line(" a: 1 ?") + << Line(" 2 :") + << Line(" 3") + << Line(" a: 1 ? 2") + << Line(" : 3 +") + << Line(" 4") + << Line(" ba: 1") + << Line("}") + ; + checkIndent(data); +} + QTEST_APPLESS_MAIN(tst_QMLCodeFormatter) #include "tst_qmlcodeformatter.moc" -- 2.11.0