r256736 - clang-format: [Proto] Improve wrapping of message field attributes.

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 3 23:27:33 PST 2016


Author: djasper
Date: Mon Jan  4 01:27:33 2016
New Revision: 256736

URL: http://llvm.org/viewvc/llvm-project?rev=256736&view=rev
Log:
clang-format: [Proto] Improve wrapping of message field attributes.

Before:
  optional AAA aaa = 1 [foo =
			    {
			      key: "a"  //
			    },
			bar = {
			  key: "a"  //
			}];

After:
  optional AAA aaa = 1 [
    foo = {
      key: "a"  //
    },
    bar = {
      key: "a"  //
    }
  ];

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

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=256736&r1=256735&r2=256736&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Mon Jan  4 01:27:33 2016
@@ -153,7 +153,8 @@ bool ContinuationIndenter::mustBreak(con
       !Current.isOneOf(tok::r_paren, tok::r_brace))
     return true;
   if (((Previous.is(TT_DictLiteral) && Previous.is(tok::l_brace)) ||
-       Previous.is(TT_ArrayInitializerLSquare)) &&
+       (Previous.is(TT_ArrayInitializerLSquare) &&
+        Previous.ParameterCount > 1)) &&
       Style.ColumnLimit > 0 &&
       getLengthToMatchingParen(Previous) + State.Column - 1 >
           getColumnLimit(State))
@@ -728,7 +729,7 @@ unsigned ContinuationIndenter::moveState
   //   }, a, b, c);
   if (Current.isNot(tok::comment) && Previous &&
       Previous->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
-      State.Stack.size() > 1) {
+      !Previous->is(TT_DictLiteral) && State.Stack.size() > 1) {
     if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline)
       for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
         State.Stack[i].NoLineBreak = true;

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=256736&r1=256735&r2=256736&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Jan  4 01:27:33 2016
@@ -285,10 +285,11 @@ private:
                  Contexts.back().ContextKind == tok::l_brace &&
                  Parent->isOneOf(tok::l_brace, tok::comma)) {
         Left->Type = TT_JsComputedPropertyName;
-      } else if (Parent &&
-                 Parent->isOneOf(tok::at, tok::equal, tok::comma, tok::l_paren,
-                                 tok::l_square, tok::question, tok::colon,
-                                 tok::kw_return)) {
+      } else if (Style.Language == FormatStyle::LK_Proto ||
+                 (Parent &&
+                  Parent->isOneOf(tok::at, tok::equal, tok::comma, tok::l_paren,
+                                  tok::l_square, tok::question, tok::colon,
+                                  tok::kw_return))) {
         Left->Type = TT_ArrayInitializerLSquare;
       } else {
         BindingIncrease = 10;

Modified: cfe/trunk/unittests/Format/FormatTestProto.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestProto.cpp?rev=256736&r1=256735&r2=256736&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestProto.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestProto.cpp Mon Jan  4 01:27:33 2016
@@ -88,9 +88,10 @@ TEST_F(FormatTestProto, UnderstandsRetur
 TEST_F(FormatTestProto, MessageFieldAttributes) {
   verifyFormat("optional string test = 1 [default = \"test\"];");
   verifyFormat("optional bool a = 1 [default = true, deprecated = true];");
-  verifyFormat("optional LongMessageType long_proto_field = 1\n"
-               "    [default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
-               "     deprecated = true];");
+  verifyFormat("optional LongMessageType long_proto_field = 1 [\n"
+               "  default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
+               "  deprecated = true\n"
+               "];");
   verifyFormat("optional LongMessageType long_proto_field = 1\n"
                "    [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
   verifyFormat("repeated double value = 1\n"
@@ -103,6 +104,16 @@ TEST_F(FormatTestProto, MessageFieldAttr
                "  aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
                "  bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
                "}];");
+  verifyFormat("repeated double value = 1 [\n"
+               "  (aaaaaaa.aaaaaaaaa) = {\n"
+               "    aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
+               "    bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
+               "  },\n"
+               "  (bbbbbbb.bbbbbbbbb) = {\n"
+               "    aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
+               "    bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
+               "  }\n"
+               "];");
   verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
                "  type: \"AAAAAAAAAA\"\n"
                "  is: \"AAAAAAAAAA\"\n"
@@ -113,6 +124,14 @@ TEST_F(FormatTestProto, MessageFieldAttr
                "  bbbbbbb: BBBB,\n"
                "  bbbb: BBB\n"
                "}];");
+  verifyFormat("optional AAA aaa = 1 [\n"
+               "  foo = {\n"
+               "    key: 'a'  //\n"
+               "  },\n"
+               "  bar = {\n"
+               "    key: 'a'  //\n"
+               "  }\n"
+               "];");
 }
 
 TEST_F(FormatTestProto, DoesntWrapFileOptions) {
@@ -130,7 +149,6 @@ TEST_F(FormatTestProto, FormatsOptions)
                "  field_c: \"OK\"\n"
                "  msg_field: {field_d: 123}\n"
                "};");
-
   verifyFormat("option (MyProto.options) = {\n"
                "  field_a: OK\n"
                "  field_b: \"OK\"\n"
@@ -140,14 +158,12 @@ TEST_F(FormatTestProto, FormatsOptions)
                "    field_e: OK\n"
                "  }\n"
                "};");
-
   verifyFormat("option (MyProto.options) = {\n"
                "  field_a: OK  // Comment\n"
                "  field_b: \"OK\"\n"
                "  field_c: \"OK\"\n"
                "  msg_field: {field_d: 123}\n"
                "};");
-
   verifyFormat("option (MyProto.options) = {\n"
                "  field_c: \"OK\"\n"
                "  msg_field{field_d: 123}\n"




More information about the cfe-commits mailing list