diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp index 591c1f2d163d703d6b540bed98e6995aef9cacba..a16248808895668691050ea7a09b7049539fa26e 100644 --- a/src/plugins/cpptools/cppcodeformatter.cpp +++ b/src/plugins/cpptools/cppcodeformatter.cpp @@ -1008,6 +1008,9 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd const int tokenPosition = column(tk.begin()); const bool firstToken = (tokenIndex() == 0); const bool lastToken = (tokenIndex() == tokenCount() - 1); + int nextTokenStart = 0; + if (!lastToken) + nextTokenStart = column(tokenAt(tokenIndex() + 1).begin()); switch (newState) { case namespace_start: @@ -1136,10 +1139,14 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd break; case brace_list_open: - if (parentState.type != initializer) - *indentDepth = parentState.savedIndentDepth + m_indentSize; - else if (lastToken) { - *savedIndentDepth = state(1).savedIndentDepth; + if (!lastToken) { + if (parentState.type == initializer) + *savedIndentDepth = tokenPosition; + *indentDepth = nextTokenStart; + } else { + // avoid existing continuation indents + if (parentState.type == initializer) + *savedIndentDepth = state(1).savedIndentDepth; *indentDepth = *savedIndentDepth + m_indentSize; } break; @@ -1256,6 +1263,7 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i } else if (topState.type != defun_open && topState.type != block_open && topState.type != substatement_open + && topState.type != brace_list_open && !topWasMaybeElse) { *indentDepth = topState.savedIndentDepth; } diff --git a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp index c08644980566124a35e7d0affd3524f225aec614..9e6b88a1ea95440eb8695e1d479b616bab2cfae5 100644 --- a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp +++ b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp @@ -46,6 +46,7 @@ private Q_SLOTS: void functionReturnType(); void streamOp(); void blockStmtInIf(); + void nestedInitializer(); }; struct Line { @@ -613,9 +614,9 @@ void tst_CodeFormatter::braceList() << Line(" b = 4") << Line(" };") << Line("void foo () {") - << Line(" int[] a = { foo, bar, ") - << Line(" car };") - << Line(" int[] a = {") + << Line(" int a[] = { foo, bar, ") + << Line(" car };") + << Line(" int a[] = {") << Line(" a, b,") << Line(" c") << Line(" };") @@ -837,6 +838,36 @@ void tst_CodeFormatter::blockStmtInIf() checkIndent(data); } +void tst_CodeFormatter::nestedInitializer() +{ + QList<Line> data; + data + << Line("SomeStruct v[] = {") + << Line(" {2}, {3},") + << Line(" {4}, {5},") + << Line("};") + << Line("S v[] = {{1}, {2},") + << Line(" {3}, {4},") + << Line(" };") + << Line("SomeStruct v[] = {") + << Line(" {") + << Line(" {2, 3,") + << Line(" 4, 5},") + << Line(" {1},") + << Line(" }") + << Line("};") + << Line("SomeStruct v[] = {{{2, 3},") + << Line(" {4, 5}") + << Line(" },") + << Line(" {{2, 3},") + << Line(" {4, 5},") + << Line(" }") + << Line(" };") + << Line("int i;") + ; + checkIndent(data); +} + QTEST_APPLESS_MAIN(tst_CodeFormatter) #include "tst_codeformatter.moc"