]> granicus.if.org Git - clang/commitdiff
[clang-format] Fix indent of 'key <...>' and 'key {...}' in text protos
authorKrasimir Georgiev <krasimir@google.com>
Thu, 3 Aug 2017 14:17:29 +0000 (14:17 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Thu, 3 Aug 2017 14:17:29 +0000 (14:17 +0000)
Summary:
This patch fixes the indentation of the code pattern `key <...>`and `key {...}` in text protos.
Previously, such line would be alinged depending on the column of the previous
colon, which usually indents too much.

I'm gonna go ahead and commit this since it's a straightforward bugfix.

Reviewers: djasper, klimek

Subscribers: klimek, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@309941 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/ContinuationIndenter.cpp
unittests/Format/FormatTestProto.cpp
unittests/Format/FormatTestTextProto.cpp

index 26dacf6562be6f0d3d30445d5cf8fc47bf8928ff..25a470474b714c744e4f10dc4b7008eff7f8229d 100644 (file)
@@ -731,7 +731,10 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
   if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope())
     return State.Stack[State.Stack.size() - 2].LastSpace;
   if (Current.is(tok::identifier) && Current.Next &&
-      Current.Next->is(TT_DictLiteral))
+      (Current.Next->is(TT_DictLiteral) ||
+       ((Style.Language == FormatStyle::LK_Proto ||
+         Style.Language == FormatStyle::LK_TextProto) &&
+        Current.Next->isOneOf(TT_TemplateOpener, tok::l_brace))))
     return State.Stack.back().Indent;
   if (NextNonComment->is(TT_ObjCStringLiteral) &&
       State.StartOfStringLiteral != 0)
index ca26c543959196f275cb4ce12b3021ad5e51ce0a..df94d8172730b23ac33826ea6212aac814ff8f80 100644 (file)
@@ -361,6 +361,19 @@ TEST_F(FormatTestProto, FormatsOptions) {
                "  data1 <key1: value1>\n"
                "  data2 {key2: value2}\n"
                ">;");
+
+  verifyFormat("option (MyProto.options) = <\n"
+               "  app_id: 'com.javax.swing.salsa.latino'\n"
+               "  head_id: 1\n"
+               "  data <key: value>\n"
+               ">;");
+
+  verifyFormat("option (MyProto.options) = {\n"
+               "  app_id: 'com.javax.swing.salsa.latino'\n"
+               "  head_id: 1\n"
+               "  headheadheadheadheadhead_id: 1\n"
+               "  product_data {product {1}}\n"
+               "};");
 }
 
 TEST_F(FormatTestProto, FormatsService) {
index 2de7e181f2cb854cb3d49be735f375a98c3e771f..0a7bcdd82362311387fca8b0a8413b23e716de72 100644 (file)
@@ -245,6 +245,50 @@ TEST_F(FormatTestTextProto, SupportsAngleBracketMessageFields) {
                ">\n"
                "field: OK,\n"
                "field_c <field <field <>>>");
+
+  verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
+               "head_id: 1\n"
+               "data <key: value>");
+
+  verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
+               "head_id: 1\n"
+               "data <key: value>\n"
+               "tail_id: 2");
+
+  verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
+               "head_id: 1\n"
+               "data <key: value>\n"
+               "data {key: value}");
+
+  verifyFormat("app {\n"
+               "  app_id: 'com.javax.swing.salsa.latino'\n"
+               "  head_id: 1\n"
+               "  data <key: value>\n"
+               "}");
+
+  verifyFormat("app: {\n"
+               "  app_id: 'com.javax.swing.salsa.latino'\n"
+               "  head_id: 1\n"
+               "  data <key: value>\n"
+               "}");
+
+  verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
+               "headheadheadheadheadhead_id: 1\n"
+               "product_data {product {1}}");
+
+  verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
+               "headheadheadheadheadhead_id: 1\n"
+               "product_data <product {1}>");
+
+  verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
+               "headheadheadheadheadhead_id: 1\n"
+               "product_data <product <1>>");
+
+  verifyFormat("app <\n"
+               "  app_id: 'com.javax.swing.salsa.latino'\n"
+               "  headheadheadheadheadhead_id: 1\n"
+               "  product_data <product {1}>\n"
+               ">");
 }
 } // end namespace tooling
 } // end namespace clang