C++ indenter: Enable class, struct, enum, namespace, using in functions.

Fixes incorrect indentation we got previously when they were used in a
statement context.
This commit is contained in:
Christian Kamm
2010-07-08 08:35:12 +02:00
parent 870da8e7e3
commit 6078b36c9c
2 changed files with 34 additions and 21 deletions

View File

@@ -72,16 +72,8 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
switch (m_currentState.top().type) { switch (m_currentState.top().type) {
case topmost_intro: case topmost_intro:
if (tryDeclaration()) tryDeclaration();
break; break;
switch (kind) {
case T_NAMESPACE: enter(namespace_start); break;
case T_STRUCT:
case T_UNION:
case T_CLASS: enter(class_start); break;
case T_ENUM: enter(enum_start); break;
case T_USING: enter(using_start); break;
} break;
case namespace_start: case namespace_start:
switch (kind) { switch (kind) {
@@ -93,13 +85,7 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
if (tryDeclaration()) if (tryDeclaration())
break; break;
switch (kind) { switch (kind) {
case T_NAMESPACE: enter(namespace_start); break;
case T_RBRACE: leave(); continue; // always nested in namespace_start case T_RBRACE: leave(); continue; // always nested in namespace_start
case T_STRUCT:
case T_UNION:
case T_CLASS: enter(class_start); break;
case T_ENUM: enter(enum_start); break;
case T_USING: enter(using_start); break;
} break; } break;
case class_start: case class_start:
@@ -113,11 +99,6 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
break; break;
switch (kind) { switch (kind) {
case T_RBRACE: leave(); continue; // always nested in class_start case T_RBRACE: leave(); continue; // always nested in class_start
case T_STRUCT:
case T_UNION:
case T_CLASS: enter(class_start); break;
case T_ENUM: enter(enum_start); break;
case T_USING: enter(using_start); break;
} break; } break;
case enum_start: case enum_start:
@@ -704,6 +685,24 @@ bool CodeFormatter::tryDeclaration()
enter(template_start); enter(template_start);
return true; return true;
case T_NAMESPACE:
enter(namespace_start);
return true;
case T_STRUCT:
case T_UNION:
case T_CLASS:
enter(class_start);
return true;
case T_ENUM:
enter(enum_start);
return true;
case T_USING:
enter(using_start);
return true;
default: default:
return false; return false;
} }

View File

@@ -42,6 +42,7 @@ private Q_SLOTS:
void operatorOverloads(); void operatorOverloads();
void gnuStyle(); void gnuStyle();
void whitesmithsStyle(); void whitesmithsStyle();
void singleLineEnum();
}; };
struct Line { struct Line {
@@ -755,6 +756,19 @@ void tst_CodeFormatter::whitesmithsStyle()
checkIndent(data, 2); checkIndent(data, 2);
} }
void tst_CodeFormatter::singleLineEnum()
{
enum { a, b};
QList<Line> data;
data << Line("enum { foo, bar, car = 2 };")
<< Line("void blah() {")
<< Line(" enum { foo, bar, car = 2 };")
<< Line(" int i;")
<< Line("}")
;
checkIndent(data);
}
QTEST_APPLESS_MAIN(tst_CodeFormatter) QTEST_APPLESS_MAIN(tst_CodeFormatter)
#include "tst_codeformatter.moc" #include "tst_codeformatter.moc"