From: Ilya Biryukov Date: Mon, 17 Sep 2018 07:46:20 +0000 (+0000) Subject: [clang-Format] Fix indentation of member call after block X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d046e91f17b5336a9c603c142613f2b80a8ff888;p=clang [clang-Format] Fix indentation of member call after block Summary: before patch: > echo "test() {([]() -> {int b = 32;return 3;}).as("");});" | clang-format -style=Google ``` test() { ([]() -> { int b = 32; return 3; }) .as(); }); ``` after patch: > echo "test() {([]() -> {int b = 32;return 3;}).as("");});" | clang-format -style=Google ``` test() { ([]() -> { int b = 32; return 3; }).as(); }); ``` Patch by Anders Karlsson (ank)! Reviewers: klimek Reviewed By: klimek Subscribers: danilaml, acoomans, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45719 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@342363 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index f035aa71e5..c9c96456f4 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -403,7 +403,9 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { // }.bind(...)); // FIXME: We should find a more generic solution to this problem. !(State.Column <= NewLineColumn && - Style.Language == FormatStyle::LK_JavaScript)) + Style.Language == FormatStyle::LK_JavaScript) && + !(Previous.closesScopeAfterBlock() && + State.Column <= NewLineColumn)) return true; // If the template declaration spans multiple lines, force wrap before the diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index d9bff0112c..e3b31f9244 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -325,6 +325,14 @@ struct FormatToken { } template bool isNot(T Kind) const { return !is(Kind); } + bool closesScopeAfterBlock() const { + if (BlockKind == BK_Block) + return true; + if (closesScope()) + return Previous->closesScopeAfterBlock(); + return false; + } + /// \c true if this token starts a sequence with the given tokens in order, /// following the ``Next`` pointers, ignoring comments. template diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 70bc9e0ed7..db2e226a30 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -4557,6 +4557,40 @@ TEST_F(FormatTest, FormatsBuilderPattern) { verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + + // Dont break if only closing statements before member call + verifyFormat("test() {\n" + " ([]() -> {\n" + " int b = 32;\n" + " return 3;\n" + " }).foo();\n" + "}"); + verifyFormat("test() {\n" + " (\n" + " []() -> {\n" + " int b = 32;\n" + " return 3;\n" + " },\n" + " foo, bar)\n" + " .foo();\n" + "}"); + verifyFormat("test() {\n" + " ([]() -> {\n" + " int b = 32;\n" + " return 3;\n" + " })\n" + " .foo()\n" + " .bar();\n" + "}"); + verifyFormat("test() {\n" + " ([]() -> {\n" + " int b = 32;\n" + " return 3;\n" + " })\n" + " .foo(\"aaaaaaaaaaaaaaaaa\"\n" + " \"bbbb\");\n" + "}", + getLLVMStyleWithColumns(30)); } TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {