From 1aed32d86677b435eada4f8718bab3356630d74f Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Mon, 17 Sep 2012 13:14:45 +0200 Subject: [PATCH] C++11: Fix indent of brace initializers in ctor lists. Change-Id: Ib784df9e17ff2fe6bb249dcf8c2fee7310340f50 Reviewed-by: hjk --- src/plugins/cpptools/cppcodeformatter.cpp | 18 +++++++-- src/plugins/cpptools/cppcodeformatter.h | 3 +- .../codeformatter/tst_codeformatter.cpp | 37 +++++++++++++++++++ 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp index f1b50a24812..6c7efca2f68 100644 --- a/src/plugins/cpptools/cppcodeformatter.cpp +++ b/src/plugins/cpptools/cppcodeformatter.cpp @@ -300,6 +300,13 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) default: tryExpression(); break; } break; + case braceinit_open: + switch (kind) { + case T_RBRACE: leave(); break; + case T_RPAREN: leave(); continue; // recover? + default: tryExpression(); break; + } break; + case ternary_op: switch (kind) { case T_RPAREN: @@ -340,14 +347,16 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) case member_init: switch (kind) { - case T_LPAREN: enter(member_init_paren_open); break; - case T_RPAREN: leave(); break; case T_LBRACE: + case T_LPAREN: enter(member_init_nest_open); break; + case T_RBRACE: + case T_RPAREN: leave(); break; case T_SEMICOLON: leave(); continue; // try to recover } break; - case member_init_paren_open: + case member_init_nest_open: switch (kind) { + case T_RBRACE: case T_RPAREN: leave(); continue; case T_SEMICOLON: leave(); continue; // try to recover default: tryExpression(); break; @@ -765,6 +774,7 @@ bool CodeFormatter::tryExpression(bool alsoExpression) switch (kind) { case T_LPAREN: newState = arglist_open; break; case T_QUESTION: newState = ternary_op; break; + case T_LBRACE: newState = braceinit_open; break; case T_EQUAL: case T_AMPER_EQUAL: @@ -1235,7 +1245,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd case arglist_open: case condition_paren_open: - case member_init_paren_open: + case member_init_nest_open: if (!lastToken) *paddingDepth = nextTokenPosition-*indentDepth; else diff --git a/src/plugins/cpptools/cppcodeformatter.h b/src/plugins/cpptools/cppcodeformatter.h index 32e203bd8c3..c2410164c6d 100644 --- a/src/plugins/cpptools/cppcodeformatter.h +++ b/src/plugins/cpptools/cppcodeformatter.h @@ -124,7 +124,7 @@ public: // must be public to make Q_GADGET introspection work member_init_open, // After ':' that starts a member initialization list. member_init_expected, // At the start and after every ',' in member_init_open member_init, // After an identifier in member_init_expected - member_init_paren_open, // After '(' in member_init. + member_init_nest_open, // After '(' or '{' in member_init. enum_start, // After 'enum' enum_open, // Brace that opens a enum declaration. @@ -168,6 +168,7 @@ public: // must be public to make Q_GADGET introspection work stream_op, // After a '<<' or '>>' in a context where it's likely a stream operator. stream_op_cont, // When finding another stream operator in stream_op ternary_op, // The ? : operator + braceinit_open, // after '{' in an expression context condition_open, // Start of a condition in 'if', 'while', entered after opening paren condition_paren_open, // After an lparen in a condition diff --git a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp index 33f6861f7bd..129380001d8 100644 --- a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp +++ b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp @@ -117,6 +117,7 @@ private Q_SLOTS: void functionBodyAndBraces4(); void constructor1(); void constructor2(); + void constructor3(); void caseBody1(); void caseBody2(); void caseBody3(); @@ -1876,6 +1877,42 @@ void tst_CodeFormatter::constructor2() checkIndent(data); } +void tst_CodeFormatter::constructor3() +{ + QList data; + data << Line("class Foo {") + << Line(" Foo() : _a{0}, _b{1, {2, {3, \"foo\"}, 3}}") + << Line(" {") + << Line(" _b = 0") + << Line(" }") + << Line(" int _a;") + << Line(" Foo()") + << Line(" ~ : _foo{1},") + << Line(" ~ _bar{2},") + << Line(" ~ _carooooo(") + << Line(" ~ foo() + 12),") + << Line(" ~ _carooooo{foo(),") + << Line(" ~ 12}") + << Line(" {") + << Line(" _b = 0") + << Line(" }") + << Line(" int _b;") + << Line(" Foo()") + << Line(" ~ : _foo{1}") + << Line(" ~ , _bar{2}") + << Line(" ~ , _carooooo{") + << Line(" ~ foo() + 12}") + << Line(" ~ , _carooooo{foo(),") + << Line(" ~ 12}") + << Line(" {") + << Line(" _b = 0") + << Line(" }") + << Line("};") + ; + CppCodeStyleSettings codeStyle; + checkIndent(data); +} + void tst_CodeFormatter::caseBody1() { QList data;