Skip to content

Commit

Permalink
clang-format: Fix selective indentaiton in nested blocks.
Browse files Browse the repository at this point in the history
Buggy case:
  someFunction(
      [] {
        // comment
        int i; // invoke formatting here.
      },       // force line break
      aaa);

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236091 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
djasper-gh committed Apr 29, 2015
1 parent 93281cb commit ad0dad7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,9 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,

if (static_cast<int>(LevelIndent) - Offset >= 0)
LevelIndent -= Offset;
if (Tok->isNot(tok::comment) && !TheLine.InPPDirective)
if ((Tok->isNot(tok::comment) ||
IndentForLevel[TheLine.Level] == -1) &&
!TheLine.InPPDirective)
IndentForLevel[TheLine.Level] = LevelIndent;
} else if (!DryRun) {
Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
Expand Down
13 changes: 13 additions & 0 deletions unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3262,6 +3262,19 @@ TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
" int a; //\n"
"});",
0, 0, getLLVMStyle()));
EXPECT_EQ("someFunction(\n"
" [] {\n"
" // Only with this comment.\n"
" int i; // invoke formatting here.\n"
" }, // force line break\n"
" aaa);",
format("someFunction(\n"
" [] {\n"
" // Only with this comment.\n"
" int i; // invoke formatting here.\n"
" }, // force line break\n"
" aaa);",
63, 1, getLLVMStyle()));
}

TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
Expand Down

0 comments on commit ad0dad7

Please sign in to comment.