}
if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
- Current.Type != TT_LineComment)
+ (Current.Type != TT_LineComment || Previous.BlockKind == BK_BracedInit))
State.Stack.back().Indent = State.Column + Spaces;
if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style))
State.Stack.back().NoLineBreak = true;
NewIndent = State.Stack.back().LastSpace;
if (Current.opensBlockTypeList(Style)) {
NewIndent += Style.IndentWidth;
+ NewIndent = std::min(State.Column + 2, NewIndent);
++NewIndentLevel;
} else {
NewIndent += Style.ContinuationIndentWidth;
+ NewIndent = std::min(State.Column + 1, NewIndent);
}
}
const FormatToken *NextNoComment = Current.getNextNonComment();
FormatToken *Current = Line.First->Next;
bool InFunctionDecl = Line.MightBeFunctionDecl;
while (Current != NULL) {
- if (Current->Type == TT_LineComment)
- Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
- else if (Current->SpacesRequiredBefore == 0 &&
- spaceRequiredBefore(Line, *Current))
+ if (Current->Type == TT_LineComment) {
+ if (Current->Previous->BlockKind == BK_BracedInit)
+ Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1;
+ else
+ Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
+ } else if (Current->SpacesRequiredBefore == 0 &&
+ spaceRequiredBefore(Line, *Current)) {
Current->SpacesRequiredBefore = 1;
+ }
Current->MustBreakBefore =
Current->MustBreakBefore || mustBreakBefore(Line, *Current);
bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
const FormatToken &Right) {
if (Right.is(tok::comment)) {
- return Right.NewlinesBefore > 0;
+ return Right.Previous->BlockKind != BK_BracedInit &&
+ Right.NewlinesBefore > 0;
} else if (Right.Previous->isTrailingComment() ||
(Right.is(tok::string_literal) &&
Right.Previous->is(tok::string_literal))) {
if (Right.isTrailingComment())
// We rely on MustBreakBefore being set correctly here as we should not
// change the "binding" behavior of a comment.
- return false;
+ // The first comment in a braced lists is always interpreted as belonging to
+ // the first list element. Otherwise, it should be placed outside of the
+ // list.
+ return Left.BlockKind == BK_BracedInit;
if (Left.is(tok::question) && Right.is(tok::colon))
return false;
if (Right.Type == TT_ConditionalExpr || Right.is(tok::question))
verifyFormat("DoSomethingWithVector({} /* No data */);");
verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });");
verifyFormat(
- "someFunction(OtherParam, BracedList{\n"
- " // comment 1 (Forcing interesting break)\n"
- " param1, param2,\n"
- " // comment 2\n"
- " param3, param4\n"
- " });");
+ "someFunction(OtherParam,\n"
+ " BracedList{ // comment 1 (Forcing interesting break)\n"
+ " param1, param2,\n"
+ " // comment 2\n"
+ " param3, param4 });",
+ getLLVMStyleWithColumns(75));
verifyFormat(
"std::this_thread::sleep_for(\n"
" std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);");
" T member = {arg1, arg2};\n"
"};",
NoSpaces);
+
+ // FIXME: The alignment of these trailing comments might be bad. Then again,
+ // this might be utterly useless in real code.
verifyFormat("Constructor::Constructor()\n"
- " : some_value{ //\n"
- " aaaaaaa //\n"
+ " : some_value{ //\n"
+ " aaaaaaa //\n"
" } {}",
NoSpaces);
+
+ // In braced lists, the first comment is always assumed to belong to the
+ // first element. Thus, it can be moved to the next or previous line as
+ // appropriate.
+ EXPECT_EQ("function({// First element:\n"
+ " 1,\n"
+ " // Second element:\n"
+ " 2});",
+ format("function({\n"
+ " // First element:\n"
+ " 1,\n"
+ " // Second element:\n"
+ " 2});",
+ NoSpaces));
+ NoSpaces.ColumnLimit = 30;
+ EXPECT_EQ(
+ "std::vector<int> MyNumbers{\n"
+ " // First element:\n"
+ " 1,\n"
+ " // Second element:\n"
+ " 2};",
+ format("std::vector<int> MyNumbers{// First element:\n"
+ " 1,\n"
+ " // Second element:\n"
+ " 2};", NoSpaces));
}
TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {