From: Daniel Jasper Date: Mon, 18 Feb 2013 11:05:07 +0000 (+0000) Subject: Prevent line breaks that make stuff hard to read. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cf5767d65a0ee64b22c242eb758e8684a6ea5a59;p=clang Prevent line breaks that make stuff hard to read. Before: aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); After: aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa) .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa) .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175432 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 939211cf65..ee3516af0c 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -251,6 +251,7 @@ public: State.VariablePos = 0; State.LineContainsContinuedForLoopSection = false; State.ParenLevel = 0; + State.StartOfLineLevel = State.ParenLevel; DEBUG({ DebugTokenState(*State.NextToken); @@ -384,6 +385,9 @@ private: /// \brief The level of nesting inside (), [], <> and {}. unsigned ParenLevel; + /// \brief The \c ParenLevel at the start of this line. + unsigned StartOfLineLevel; + /// \brief A stack keeping track of properties applying to parenthesis /// levels. std::vector Stack; @@ -401,6 +405,8 @@ private: return LineContainsContinuedForLoopSection; if (Other.ParenLevel != ParenLevel) return Other.ParenLevel < ParenLevel; + if (Other.StartOfLineLevel < StartOfLineLevel) + return Other.StartOfLineLevel < StartOfLineLevel; return Other.Stack < Stack; } }; @@ -489,6 +495,7 @@ private: } State.Stack.back().LastSpace = State.Column; + State.StartOfLineLevel = State.ParenLevel; if (Current.is(tok::colon) && Current.Type != TT_ConditionalExpr) State.Stack.back().Indent += 2; } else { @@ -772,6 +779,14 @@ private: !(State.NextToken->is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)) return false; + // This prevents breaks like: + // ... + // SomeParameter, OtherParameter).DoSomething( + // ... + // As they hide "DoSomething" and generally bad for readability. + if (State.NextToken->Parent->is(tok::l_paren) && + State.ParenLevel <= State.StartOfLineLevel) + return false; // Trying to insert a parameter on a new line if there are already more than // one parameter on the current line is bin packing. if (State.Stack.back().HasMultiParameterLine && diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index a41c713ac5..fad7998b2c 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1457,6 +1457,13 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n" " .insert(ccccccccccccccccccccccc);"); + verifyGoogleFormat( + "aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" + " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" + " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + // Here, it is not necessary to wrap at "." or "->". verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");