diff --git a/src/libs/qmljs/qmljscodeformatter.cpp b/src/libs/qmljs/qmljscodeformatter.cpp index b02075e4f89..2e90179866e 100644 --- a/src/libs/qmljs/qmljscodeformatter.cpp +++ b/src/libs/qmljs/qmljscodeformatter.cpp @@ -90,6 +90,7 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) switch (kind) { case Identifier: enter(objectdefinition_or_js); continue; case Import: enter(top_qml); continue; + case LeftBrace: enter(top_js); enter(expression); continue; // if a file starts with {, it's likely json default: enter(top_js); continue; } break; diff --git a/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp b/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp index 4b809d89ee8..412c4c9d830 100644 --- a/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp +++ b/src/plugins/qmljstools/qmljsqtstylecodeformatter.cpp @@ -170,6 +170,9 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd if (parentState.type == expression && state(1).type == binding_assignment) { *savedIndentDepth = state(2).savedIndentDepth; *indentDepth = *savedIndentDepth + m_indentSize; + } else if (parentState.type == objectliteral_assignment) { + *savedIndentDepth = parentState.savedIndentDepth; + *indentDepth = *savedIndentDepth + m_indentSize; } else if (!lastToken) { *indentDepth = tokenPosition + 1; } else { diff --git a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp index 3dcfbdf91b8..a35c03386d5 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 json1(); }; struct Line { @@ -1198,6 +1199,33 @@ void tst_QMLCodeFormatter::labelledStatements3() checkIndent(data); } +void tst_QMLCodeFormatter::json1() +{ + QList data; + data << Line("{") + << Line(" \"lab\": \"abc\",") + << Line(" \"foo\": \"baroooo\",") + << Line(" \"foo\": [ \"baroooo\", \"foo\" ],") + << Line(" \"foo\": [") + << Line(" \"baroooo\",") + << Line(" \"doo\"") + << Line(" ],") + << Line(" \"lab\": \"abc\",") + << Line(" \"object\": {") + << Line(" \"lab\": \"abc\",") + << Line(" \"foo\": \"baroooo\",") + << Line(" \"foo\": [ \"baroooo\", \"foo\" ],") + << Line(" \"foo\": [") + << Line(" \"baroooo\",") + << Line(" \"doo\"") + << Line(" ],") + << Line(" \"lab\": \"abc\"") + << Line(" }") + << Line("}") + ; + checkIndent(data); +} + QTEST_APPLESS_MAIN(tst_QMLCodeFormatter) #include "tst_qmlcodeformatter.moc"