C++ indenter: Fix for statement indentation and add test.

This commit is contained in:
Christian Kamm
2010-09-15 10:57:57 +02:00
parent 7167bdddfb
commit e5cbb56fe7
2 changed files with 43 additions and 4 deletions

View File

@@ -1062,7 +1062,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
case if_statement: case if_statement:
case return_statement: case return_statement:
if (firstToken) if (firstToken)
*savedIndentDepth = tokenPosition; *indentDepth = *savedIndentDepth = tokenPosition;
*paddingDepth = 2*m_indentSize; *paddingDepth = 2*m_indentSize;
break; break;
@@ -1144,6 +1144,9 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
} }
case substatement_open: case substatement_open:
// undo parent continuation indent
*savedPaddingDepth = 0;
if (firstToken) { if (firstToken) {
*savedIndentDepth = tokenPosition; *savedIndentDepth = tokenPosition;
*indentDepth = *savedIndentDepth; *indentDepth = *savedIndentDepth;
@@ -1192,8 +1195,8 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
case substatement: case substatement:
// undo the continuation indent of the parent // undo the continuation indent of the parent
*indentDepth = parentState.savedIndentDepth; *savedPaddingDepth = 0;
*savedIndentDepth = *indentDepth;
break; break;
case maybe_else: { case maybe_else: {
@@ -1207,7 +1210,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
} break; } break;
case for_statement_paren_open: case for_statement_paren_open:
*indentDepth = tokenPosition + 1; *paddingDepth = tokenPosition + 1 - *indentDepth;
break; break;
case multiline_comment_start: case multiline_comment_start:
@@ -1397,6 +1400,7 @@ bool QtStyleCodeFormatter::shouldClearPaddingOnEnter(int state)
case return_statement: case return_statement:
case block_open: case block_open:
case substatement_open: case substatement_open:
case substatement:
return true; return true;
} }
return false; return false;

View File

@@ -47,6 +47,7 @@ private Q_SLOTS:
void streamOp(); void streamOp();
void blockStmtInIf(); void blockStmtInIf();
void nestedInitializer(); void nestedInitializer();
void forStatement();
}; };
struct Line { struct Line {
@@ -904,6 +905,40 @@ void tst_CodeFormatter::nestedInitializer()
checkIndent(data); checkIndent(data);
} }
void tst_CodeFormatter::forStatement()
{
QList<Line> data;
data
<< Line("void foo()")
<< Line("{")
<< Line(" for (a; b; c)")
<< Line(" bar();")
<< Line(" for (a; b; c) {")
<< Line(" bar();")
<< Line(" }")
<< Line(" for (a; b; c)")
<< Line(" {")
<< Line(" bar();")
<< Line(" }")
<< Line(" for (a;")
<< Line(" ~ b;")
<< Line(" ~ c)")
<< Line(" bar();")
<< Line(" for (a;")
<< Line(" ~ b;")
<< Line(" ~ c) {")
<< Line(" bar();")
<< Line(" }")
<< Line(" for (a;")
<< Line(" ~ b;")
<< Line(" ~ c)")
<< Line(" {")
<< Line(" bar();")
<< Line(" }")
;
checkIndent(data);
}
QTEST_APPLESS_MAIN(tst_CodeFormatter) QTEST_APPLESS_MAIN(tst_CodeFormatter)
#include "tst_codeformatter.moc" #include "tst_codeformatter.moc"