r335459 - [clang-format] Keep @message together in text protos

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 25 05:43:12 PDT 2018


Author: krasimir
Date: Mon Jun 25 05:43:12 2018
New Revision: 335459

URL: http://llvm.org/viewvc/llvm-project?rev=335459&view=rev
Log:
[clang-format] Keep @message together in text protos

Summary:
In C++ code snippets of the form `@field` are common. This makes clang-format
keep them together in text protos, whereas before it would break them.

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D48543

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTestTextProto.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=335459&r1=335458&r2=335459&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Mon Jun 25 05:43:12 2018
@@ -379,7 +379,8 @@ bool ContinuationIndenter::mustBreak(con
   if (Current.is(TT_ObjCMethodExpr) && !Previous.is(TT_SelectorName) &&
       State.Line->startsWith(TT_ObjCMethodSpecifier))
     return true;
-  if (Current.is(TT_SelectorName) && State.Stack.back().ObjCSelectorNameFound &&
+  if (Current.is(TT_SelectorName) && !Previous.is(tok::at) &&
+      State.Stack.back().ObjCSelectorNameFound &&
       State.Stack.back().BreakBeforeParameter)
     return true;
 

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=335459&r1=335458&r2=335459&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Jun 25 05:43:12 2018
@@ -2949,15 +2949,32 @@ bool TokenAnnotator::mustBreakBefore(con
   //
   // Be careful to exclude the case  [proto.ext] { ... } since the `]` is
   // the TT_SelectorName there, but we don't want to break inside the brackets.
+  //
+  // Another edge case is @submessage { key: value }, which is a common
+  // substitution placeholder. In this case we want to keep `@` and `submessage`
+  // together.
+  //
   // We ensure elsewhere that extensions are always on their own line.
   if ((Style.Language == FormatStyle::LK_Proto ||
        Style.Language == FormatStyle::LK_TextProto) &&
       Right.is(TT_SelectorName) && !Right.is(tok::r_square) && Right.Next) {
+    // Keep `@submessage` together in:
+    // @submessage { key: value }
+    if (Right.Previous && Right.Previous->is(tok::at))
+      return false;
     // Look for the scope opener after selector in cases like:
     // selector { ...
     // selector: { ...
-    FormatToken *LBrace =
-        Right.Next->is(tok::colon) ? Right.Next->Next : Right.Next;
+    // selector: @base { ...
+    FormatToken *LBrace = Right.Next;
+    if (LBrace && LBrace->is(tok::colon)) {
+      LBrace = LBrace->Next;
+      if (LBrace && LBrace->is(tok::at)) {
+        LBrace = LBrace->Next;
+        if (LBrace)
+          LBrace = LBrace->Next;
+      }
+    }
     if (LBrace &&
         // The scope opener is one of {, [, <:
         // selector { ... }

Modified: cfe/trunk/unittests/Format/FormatTestTextProto.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestTextProto.cpp?rev=335459&r1=335458&r2=335459&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestTextProto.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestTextProto.cpp Mon Jun 25 05:43:12 2018
@@ -717,5 +717,23 @@ TEST_F(FormatTestTextProto, FormatsComme
                "# endfile comment");
 }
 
+TEST_F(FormatTestTextProto, KeepsAmpersandsNextToKeys) {
+  verifyFormat("@tmpl { field: 1 }");
+  verifyFormat("@placeholder: 1");
+  verifyFormat("@name <>");
+  verifyFormat("submessage: @base { key: value }");
+  verifyFormat("submessage: @base {\n"
+               "  key: value\n"
+               "  item: {}\n"
+               "}");
+  verifyFormat("submessage: {\n"
+               "  msg: @base {\n"
+               "    yolo: {}\n"
+               "    key: value\n"
+               "  }\n"
+               "  key: value\n"
+               "}");
+}
+
 } // end namespace tooling
 } // end namespace clang




More information about the cfe-commits mailing list