C++ indenter: Make building custom styles easier, fix style issues.

Keep more information by using enter() instead of turnInto() when moving
from a *_start to *_open.
This commit is contained in:
Christian Kamm
2010-07-05 12:56:37 +02:00
parent 3100fc0b7e
commit 19db6c9826
3 changed files with 188 additions and 72 deletions

View File

@@ -40,6 +40,8 @@ private Q_SLOTS:
void memberInitializer();
void templates();
void operatorOverloads();
void gnuStyle();
void whitesmithsStyle();
};
struct Line {
@@ -73,12 +75,19 @@ QString concatLines(QList<Line> lines)
return result;
}
void checkIndent(QList<Line> data)
void checkIndent(QList<Line> data, int style = 0)
{
QString text = concatLines(data);
QTextDocument document(text);
QtStyleCodeFormatter formatter;
formatter.setDocument(&document);
if (style == 1) {// gnu
formatter.setIndentSubstatementBraces(true);
} else if (style == 2) { // whitesmiths
formatter.setIndentSubstatementStatements(false);
formatter.setIndentSubstatementBraces(true);
formatter.setIndentDeclarationMembers(false);
formatter.setIndentDeclarationBraces(true);
}
int i = 0;
foreach (const Line &l, data) {
@@ -594,6 +603,10 @@ void tst_CodeFormatter::braceList()
<< Line("void foo () {")
<< Line(" int[] a = { foo, bar, ")
<< Line(" car };")
<< Line(" int[] a = {")
<< Line(" a, b,")
<< Line(" c")
<< Line(" };")
<< Line(" int k;")
;
checkIndent(data);
@@ -689,6 +702,46 @@ void tst_CodeFormatter::operatorOverloads()
checkIndent(data);
}
void tst_CodeFormatter::gnuStyle()
{
QList<Line> data;
data << Line("struct S")
<< Line("{")
<< Line(" void foo()")
<< Line(" {")
<< Line(" if (a)")
<< Line(" {")
<< Line(" fpp;")
<< Line(" }")
<< Line(" if (b) {")
<< Line(" fpp;")
<< Line(" }")
<< Line(" }")
<< Line("};")
;
checkIndent(data, 1);
}
void tst_CodeFormatter::whitesmithsStyle()
{
QList<Line> data;
data << Line("struct S")
<< Line(" {")
<< Line(" void foo()")
<< Line(" {")
<< Line(" if (a)")
<< Line(" {")
<< Line(" fpp;")
<< Line(" }")
<< Line(" if (b) {")
<< Line(" fpp;")
<< Line(" }")
<< Line(" }")
<< Line(" };")
;
checkIndent(data, 2);
}
QTEST_APPLESS_MAIN(tst_CodeFormatter)
#include "tst_codeformatter.moc"