From 62a71b75a0d2eaf9c68df5d6b12d70c75a7b8856 Mon Sep 17 00:00:00 2001 From: Christian Kamm <christian.d.kamm@nokia.com> Date: Wed, 7 Sep 2011 12:06:46 +0200 Subject: [PATCH] QmlJS indenter: Fix indentation for property initializers. property int foo: { used to start an object literal but is a block statement now. Change-Id: I9ffbce4927b444314f1a43aba65ca3d9d234e47c Reviewed-on: http://codereview.qt-project.org/4339 Reviewed-by: Fawzi Mohamed <fawzi.mohamed@nokia.com> --- src/libs/qmljs/qmljscodeformatter.cpp | 17 +--------- src/libs/qmljs/qmljscodeformatter.h | 1 - .../qmljstools/qmljsqtstylecodeformatter.cpp | 15 +++------ .../qmlcodeformatter/tst_qmlcodeformatter.cpp | 32 +++++++++++-------- 4 files changed, 24 insertions(+), 41 deletions(-) diff --git a/src/libs/qmljs/qmljscodeformatter.cpp b/src/libs/qmljs/qmljscodeformatter.cpp index 32ab5585da2..eb7e61642d2 100644 --- a/src/libs/qmljs/qmljscodeformatter.cpp +++ b/src/libs/qmljs/qmljscodeformatter.cpp @@ -166,21 +166,6 @@ 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; @@ -222,7 +207,7 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) case property_maybe_initializer: switch (kind) { - case Colon: enter(property_initializer); break; + case Colon: turnInto(binding_assignment); break; default: leave(true); continue; } break; diff --git a/src/libs/qmljs/qmljscodeformatter.h b/src/libs/qmljs/qmljscodeformatter.h index 5bb1fdb422c..bd2e9228d52 100644 --- a/src/libs/qmljs/qmljscodeformatter.h +++ b/src/libs/qmljs/qmljscodeformatter.h @@ -127,7 +127,6 @@ public: // must be public to make Q_GADGET introspection work binding_or_objectdefinition, // after an identifier 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 17d7f7fdca8..a85a25685e3 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 || parentState.type == property_initializer) + if (parentState.type == binding_assignment) *savedIndentDepth = state(1).savedIndentDepth; if (firstToken) @@ -117,7 +117,6 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd break; case binding_assignment: - case property_initializer: case objectliteral_assignment: if (lastToken) *indentDepth = *savedIndentDepth + 4; @@ -134,7 +133,6 @@ 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; } @@ -155,8 +153,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd break; case bracket_open: - if (parentState.type == expression && (state(1).type == binding_assignment - || state(1).type == property_initializer)) { + if (parentState.type == expression && state(1).type == binding_assignment) { *savedIndentDepth = state(2).savedIndentDepth; *indentDepth = *savedIndentDepth + m_indentSize; } else if (!lastToken) { @@ -201,16 +198,15 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd } // fallthrough case substatement_open: - // special case for foo: { - if (parentState.type == binding_assignment && state(1).type == binding_or_objectdefinition) + // special case for "foo: {" and "property int foo: {" + if (parentState.type == binding_assignment) *savedIndentDepth = state(1).savedIndentDepth; *indentDepth = *savedIndentDepth + m_indentSize; break; case objectliteral_open: if (parentState.type == expression - || parentState.type == objectliteral_assignment - || parentState.type == property_initializer) { + || parentState.type == objectliteral_assignment) { // undo the continuation indent of the expression *indentDepth = parentState.savedIndentDepth; *savedIndentDepth = *indentDepth; @@ -293,7 +289,6 @@ 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 f45f35ef68d..b7ef68d88ef 100644 --- a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp +++ b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp @@ -87,7 +87,7 @@ private Q_SLOTS: void objectLiteral2(); void objectLiteral3(); void objectLiteral4(); - void objectLiteral5(); + void propertyWithStatement(); void keywordStatement(); void namespacedObjects(); }; @@ -1031,24 +1031,28 @@ void tst_QMLCodeFormatter::objectLiteral4() checkIndent(data); } -void tst_QMLCodeFormatter::objectLiteral5() +void tst_QMLCodeFormatter::propertyWithStatement() { QList<Line> data; data << Line("Rectangle {") - << Line(" property int x: { a: 12, b: 13 }") + << Line(" property int x: { if (a) break }") << Line(" property int y: {") - << Line(" a: 1 +") - << Line(" 2 + 3") - << Line(" + 4") + << Line(" if (a)") + << Line(" break") + << Line(" switch (a) {") + << Line(" case 1:") + << Line(" case 2:") + << Line(" continue") + << Line(" }") << Line(" }") - << Line(" property int y: {") - << Line(" a: 1 +") - << Line(" 2 + 3") - << Line(" + 4,") - << Line(" b: {") - << Line(" adef: 1 +") - << Line(" 2 + 3") - << Line(" + 4,") + << Line(" property int y:") + << Line(" {") + << Line(" if (a)") + << Line(" break") + << Line(" switch (a) {") + << Line(" case 1:") + << Line(" case 2:") + << Line(" continue") << Line(" }") << Line(" }") << Line("}") -- GitLab