Commit d292eca5 authored by Christian Kamm's avatar Christian Kamm
Browse files

C++ indenter: Indent member initializer lists correctly.

Without requiring ',' to be an electric character.

Task-number: QTCREATORBUG-1866
Reviewed-by: Roberto Raggi
parent a457d2ee
......@@ -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;
......
......@@ -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.
......
......@@ -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("}")
;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment