diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp index a17488ac1d39437af79dc78322cb607c1d76f7e0..d24f3d956321f3945cf82c48bd630fbf69f2f964 100644 --- a/src/plugins/cpptools/cppcodeformatter.cpp +++ b/src/plugins/cpptools/cppcodeformatter.cpp @@ -161,7 +161,7 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) case T_SEMICOLON: leave(true); break; case T_EQUAL: enter(initializer); break; case T_LBRACE: enter(defun_open); break; - case T_COLON: enter(member_init_open); break; + case T_COLON: enter(member_init_open); enter(member_init); break; case T_OPERATOR: enter(operator_declaration); break; } break; @@ -221,7 +221,24 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) case member_init_open: switch (kind) { case T_LBRACE: turnInto(defun_open); break; - case T_SEMICOLON: leave(); continue; // so we don't break completely if it's a bitfield or ternary + case T_COMMA: enter(member_init); break; + case T_SEMICOLON: leave(); continue; // try to recover + } break; + + case member_init: + switch (kind) { + case T_LPAREN: enter(member_init_paren_open); break; + case T_RPAREN: leave(); break; + case T_LBRACE: + case T_SEMICOLON: leave(); continue; // try to recover + } break; + + case member_init_paren_open: + if (tryExpression()) + break; + switch (kind) { + case T_RPAREN: leave(); continue; + case T_SEMICOLON: leave(); continue; // try to recover } break; case defun_open: @@ -1036,9 +1053,17 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd *savedIndentDepth = parentState.savedIndentDepth; if (firstToken) - *indentDepth = tokenPosition + tk.length() + 1; + *indentDepth = tokenPosition; else - *indentDepth = *savedIndentDepth + m_indentSize; + *indentDepth = *savedIndentDepth + m_indentSize - 2; // they'll get another 2 from member_init + break; + + case member_init: + *indentDepth = *savedIndentDepth + 2; // savedIndentDepth is the position of ':' + break; + + case member_init_paren_open: + *indentDepth = *savedIndentDepth + m_indentSize; break; case case_cont: @@ -1184,11 +1209,6 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i *indentDepth -= 2; } break; - case T_COMMA: - if (topState.type == member_init_open) { - *indentDepth -= 2; - } - break; case T_LBRACE: { if (topState.type == case_cont) { *indentDepth = topState.savedIndentDepth; diff --git a/src/plugins/cpptools/cppcodeformatter.h b/src/plugins/cpptools/cppcodeformatter.h index 6508ad8f6dc8c0dfb8fe7f2fc98e1e7224c6ce4c..68671c9f9c93767a641a0d1bfb54cbaac7bdab2e 100644 --- a/src/plugins/cpptools/cppcodeformatter.h +++ b/src/plugins/cpptools/cppcodeformatter.h @@ -111,7 +111,10 @@ protected: class_start, // after the 'class' token class_open, // Brace that opens a class definition. + member_init_open, // After ':' that starts a member initialization list. + member_init, // At the start and after every ',' in member_init_open + member_init_paren_open, // After '(' in member_init. enum_start, // After 'enum' enum_open, // Brace that opens a enum declaration. diff --git a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp index 796de10e52f646a09eb94c06e218534c9825b178..05795d8070cdcb1150b7c360c6cf516e75c7d8d7 100644 --- a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp +++ b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp @@ -672,7 +672,16 @@ void tst_CodeFormatter::memberInitializer() << Line("class Foo {") << Line(" Foo()") << Line(" : baR(),") - << Line(" moo(barR)") + << Line(" moodoo(barR + ") + << Line(" 42),") + << Line(" xyz()") + << Line(" {}") + << Line("};") + << Line("class Foo {") + << Line(" Foo() :") + << Line(" baR(),") + << Line(" moo(barR)") + << Line(" , moo(barR)") << Line(" {}") << Line("}") ;