diff --git a/src/libs/qmljs/qmljscodeformatter.cpp b/src/libs/qmljs/qmljscodeformatter.cpp index edd596c5caaf4fd40f89cc9a3f80807100255e4a..f3811e50a97c789118ac7324e6d265afae4762be 100644 --- a/src/libs/qmljs/qmljscodeformatter.cpp +++ b/src/libs/qmljs/qmljscodeformatter.cpp @@ -165,6 +165,21 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) default: enter(expression); continue; } break; + // property inits don't take statements + case property_initializer: + switch (kind) { + case Semicolon: leave(true); break; + case LeftBrace: enter(objectliteral_open); break; + case On: + case As: + case List: + case Import: + case Signal: + case Property: + case Identifier: enter(expression_or_objectdefinition); break; + default: enter(expression); continue; + } break; + case objectdefinition_open: switch (kind) { case RightBrace: leave(true); break; @@ -206,7 +221,7 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) case property_maybe_initializer: switch (kind) { - case Colon: enter(binding_assignment); break; + case Colon: enter(property_initializer); break; default: leave(true); continue; } break; @@ -298,7 +313,7 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) case Colon: enter(objectliteral_assignment); break; case RightBracket: case RightParenthesis: leave(); continue; // error recovery - case RightBrace: leave(); break; + case RightBrace: leave(true); break; } break; // pretty much like expression, but ends with , or } diff --git a/src/libs/qmljs/qmljscodeformatter.h b/src/libs/qmljs/qmljscodeformatter.h index a3752ff67298b81bb8e01113baf40bc7d1022280..44b5c1968610e32645a46dba19d5dcd588532254 100644 --- a/src/libs/qmljs/qmljscodeformatter.h +++ b/src/libs/qmljs/qmljscodeformatter.h @@ -126,7 +126,8 @@ public: // must be public to make Q_GADGET introspection work binding_or_objectdefinition, // after an identifier - binding_assignment, // after : + binding_assignment, // after : in a binding + property_initializer, // after : in a property objectdefinition_open, // after { expression, diff --git a/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp b/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp index 07d44c5923a35229a65c49d38fa07d3eadb22239..4c87f1dacaeed06fafc8eab8dcb28dffb763da4c 100644 --- a/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp +++ b/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp @@ -101,7 +101,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd switch (newState) { case objectdefinition_open: { // special case for things like "gradient: Gradient {" - if (parentState.type == binding_assignment) + if (parentState.type == binding_assignment || parentState.type == property_initializer) *savedIndentDepth = state(1).savedIndentDepth; if (firstToken) @@ -117,6 +117,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd break; case binding_assignment: + case property_initializer: case objectliteral_assignment: if (lastToken) *indentDepth = *savedIndentDepth + 4; @@ -133,6 +134,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd // ternary already adjusts indents nicely if (parentState.type != expression_or_objectdefinition && parentState.type != binding_assignment + && parentState.type != property_initializer && parentState.type != ternary_op) { *indentDepth += 2 * m_indentSize; } @@ -153,7 +155,8 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd break; case bracket_open: - if (parentState.type == expression && state(1).type == binding_assignment) { + if (parentState.type == expression && (state(1).type == binding_assignment + || state(1).type == property_initializer)) { *savedIndentDepth = state(2).savedIndentDepth; *indentDepth = *savedIndentDepth + m_indentSize; } else if (!lastToken) { @@ -205,7 +208,9 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd break; case objectliteral_open: - if (parentState.type == expression || parentState.type == objectliteral_assignment) { + if (parentState.type == expression + || parentState.type == objectliteral_assignment + || parentState.type == property_initializer) { // undo the continuation indent of the expression *indentDepth = parentState.savedIndentDepth; *savedIndentDepth = *indentDepth; @@ -288,6 +293,7 @@ void QtStyleCodeFormatter::adjustIndent(const QList<Token> &tokens, int lexerSta case LeftBrace: if (topState.type == substatement || topState.type == binding_assignment + || topState.type == property_initializer || topState.type == case_cont) { *indentDepth = topState.savedIndentDepth; } diff --git a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp index e9137b26116e24a6df01555949a04bb973db5afb..d03593fba89150a45a82a3c90a95fd3b9aaae108 100644 --- a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp +++ b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp @@ -87,6 +87,7 @@ private Q_SLOTS: void objectLiteral2(); void objectLiteral3(); void objectLiteral4(); + void objectLiteral5(); void keywordStatement(); void namespacedObjects(); }; @@ -1030,6 +1031,31 @@ void tst_QMLCodeFormatter::objectLiteral4() checkIndent(data); } +void tst_QMLCodeFormatter::objectLiteral5() +{ + QList<Line> data; + data << Line("Rectangle {") + << Line(" property int x: { a: 12, b: 13 }") + << Line(" property int y: {") + << Line(" a: 1 +") + << Line(" 2 + 3") + << Line(" + 4") + << Line(" }") + << Line(" property int y: {") + << Line(" a: 1 +") + << Line(" 2 + 3") + << Line(" + 4,") + << Line(" b: {") + << Line(" adef: 1 +") + << Line(" 2 + 3") + << Line(" + 4,") + << Line(" }") + << Line(" }") + << Line("}") + ; + checkIndent(data); +} + void tst_QMLCodeFormatter::keywordStatement() { QList<Line> data;