Implements IndentWidth.

This is required for various styles that are for example based on
8-indent.

llvm-svn: 181690
This commit is contained in:
Manuel Klimek 2013-05-13 08:42:42 +00:00
parent 0480b9b54e
commit 13b97d8b82
3 changed files with 47 additions and 4 deletions

View File

@ -94,6 +94,9 @@ struct FormatStyle {
/// Otherwise puts them into the right-most column.
bool AlignEscapedNewlinesLeft;
/// \brief The number of characters to use for indentation.
unsigned IndentWidth;
bool operator==(const FormatStyle &R) const {
return AccessModifierOffset == R.AccessModifierOffset &&
AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft &&
@ -113,7 +116,8 @@ struct FormatStyle {
PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine &&
PointerBindsToType == R.PointerBindsToType &&
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
Standard == R.Standard;
Standard == R.Standard &&
IndentWidth == IndentWidth;
}
};

View File

@ -85,6 +85,7 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
IO.mapOptional("SpacesBeforeTrailingComments",
Style.SpacesBeforeTrailingComments);
IO.mapOptional("Standard", Style.Standard);
IO.mapOptional("IndentWidth", Style.IndentWidth);
}
};
}
@ -111,6 +112,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.PointerBindsToType = false;
LLVMStyle.SpacesBeforeTrailingComments = 1;
LLVMStyle.Standard = FormatStyle::LS_Cpp03;
LLVMStyle.IndentWidth = 2;
return LLVMStyle;
}
@ -132,6 +134,7 @@ FormatStyle getGoogleStyle() {
GoogleStyle.PointerBindsToType = true;
GoogleStyle.SpacesBeforeTrailingComments = 2;
GoogleStyle.Standard = FormatStyle::LS_Auto;
GoogleStyle.IndentWidth = 2;
return GoogleStyle;
}
@ -429,7 +432,7 @@ private:
if (Newline) {
unsigned WhitespaceStartColumn = State.Column;
if (Current.is(tok::r_brace)) {
State.Column = Line.Level * 2;
State.Column = Line.Level * Style.IndentWidth;
} else if (Current.is(tok::string_literal) &&
State.StartOfStringLiteral != 0) {
State.Column = State.StartOfStringLiteral;
@ -604,6 +607,11 @@ private:
Current.LastInChainOfCalls ? 0 : State.Column +
Current.FormatTok.TokenLength;
if (Current.Type == TT_CtorInitializerColon) {
// Indent 2 from the column, so:
// SomeClass::SomeClass()
// : First(...), ...
// Next(...)
// ^ line up here.
State.Stack.back().Indent = State.Column + 2;
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
State.Stack.back().AvoidBinPacking = true;
@ -656,7 +664,7 @@ private:
unsigned NewIndent;
bool AvoidBinPacking;
if (Current.is(tok::l_brace)) {
NewIndent = 2 + State.Stack.back().LastSpace;
NewIndent = Style.IndentWidth + State.Stack.back().LastSpace;
AvoidBinPacking = false;
} else {
NewIndent = 4 + std::max(State.Stack.back().LastSpace,
@ -1253,7 +1261,7 @@ private:
return IndentForLevel[Level];
if (Level == 0)
return 0;
return getIndent(IndentForLevel, Level - 1) + 2;
return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
}
/// \brief Get the offset of the line relatively to the level.

View File

@ -3959,6 +3959,36 @@ TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
"}");
}
TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
verifyFormat("class X {\n"
" void f() {\n"
" }\n"
"};",
getLLVMStyleWithColumns(12));
}
TEST_F(FormatTest, ConfigurableIndentWidth) {
FormatStyle EightIndent = getLLVMStyleWithColumns(18);
EightIndent.IndentWidth = 8;
verifyFormat("void f() {\n"
" someFunction();\n"
" if (true) {\n"
" f();\n"
" }\n"
"}",
EightIndent);
verifyFormat("class X {\n"
" void f() {\n"
" }\n"
"};",
EightIndent);
verifyFormat("int x[] = {\n"
" call(),\n"
" call(),\n"
"};",
EightIndent);
}
bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
for (size_t i = 1; i < Styles.size(); ++i)
if (!(Styles[0] == Styles[i]))
@ -4022,6 +4052,7 @@ TEST_F(FormatTest, ParsesConfiguration) {
PenaltyReturnTypeOnItsOwnLine, 1234u);
CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
SpacesBeforeTrailingComments, 1234u);
CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
Style.Standard = FormatStyle::LS_Auto;
CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);