diff options
author | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-11-14 14:56:27 +0100 |
---|---|---|
committer | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-11-20 06:13:32 +0000 |
commit | 1817513d4f3a2e4e26be124dbe395340f798fd51 (patch) | |
tree | 949e5b784e36570f821b232ff1d776b81ef07c9a | |
parent | c83dedaf74944420a172d8607a757fcc03e2f568 (diff) |
ClangFormat: Fix keeping line breaks for lambdas
Tweak the bahaviour of keeping line breaks for child tokens.
Change-Id: I38f073b2ad8f554c44abbc80cfae7e89273fc60f
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
-rw-r--r-- | lib/Format/UnwrappedLineFormatter.cpp | 6 | ||||
-rw-r--r-- | lib/Format/UnwrappedLineParser.cpp | 2 | ||||
-rw-r--r-- | unittests/Format/FormatTest.cpp | 12 |
3 files changed, 15 insertions, 5 deletions
diff --git a/lib/Format/UnwrappedLineFormatter.cpp b/lib/Format/UnwrappedLineFormatter.cpp index 7e1d805a5f..d4dbdc5544 100644 --- a/lib/Format/UnwrappedLineFormatter.cpp +++ b/lib/Format/UnwrappedLineFormatter.cpp @@ -715,7 +715,8 @@ protected: // assert so that we can simply call this function for all tokens. return true; - if (NewLine) { + if (NewLine || (Previous.Children[0]->First->MustBreakBefore && + Style.KeepLineBreaksForNonEmptyLines)) { int AdditionalIndent = State.Stack.back().Indent - Previous.Children[0]->Level * Style.IndentWidth; @@ -786,8 +787,7 @@ public: while (State.NextToken) { bool Newline = Indenter->mustBreak(State) || - (State.NextToken->NewlinesBefore > 0 && - (Style.KeepLineBreaksForNonEmptyLines || Indenter->canBreak(State))); + (State.NextToken->NewlinesBefore > 0 && Indenter->canBreak(State)); unsigned Penalty = 0; formatChildren(State, Newline, /*DryRun=*/false, Penalty); Indenter->addTokenToState(State, Newline, /*DryRun=*/false); diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index e5afa1264a..b363506bb2 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -2517,6 +2517,8 @@ void UnwrappedLineParser::nextToken(int LevelDifference) { else readTokenWithJavaScriptASI(); FormatTok->Previous = Previous; + if (FormatTok->NewlinesBefore && Style.KeepLineBreaksForNonEmptyLines) + FormatTok->MustBreakBefore = true; } void UnwrappedLineParser::distributeComments( diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 3c94e4c9af..f9b575b289 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -353,9 +353,17 @@ TEST_F(FormatTest, RemovesEmptyLines) { Style.KeepLineBreaksForNonEmptyLines = true; Style.ColumnLimit = 0; EXPECT_EQ("int foo(int a,\n" - " int b) {}", + " int b)\n" + "{\n" + "}", format("int foo(int a,\n" - " int b) {}", + "int b) {}", + Style)); + + EXPECT_EQ("[]() {\n" + " foo(); }", + format("[]() {\n" + "foo(); }", Style)); } |