]> granicus.if.org Git - clang/commitdiff
clang-format: [proto] Improve formatting of text-proto options.
authorDaniel Jasper <djasper@google.com>
Mon, 28 Jul 2014 14:08:09 +0000 (14:08 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 28 Jul 2014 14:08:09 +0000 (14:08 +0000)
Initial patch and tests by Kaushik Sridharan, thank you!

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

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

index 014c30e346ad18861a2a54124f1570e7de2d1431..ea68150d6a4294062ebed223c33dae31c4c165e8 100644 (file)
@@ -150,7 +150,8 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
       Previous.Type != TT_InlineASMColon &&
       Previous.Type != TT_ConditionalExpr && nextIsMultilineString(State))
     return true;
-  if (((Previous.Type == TT_DictLiteral && Previous.is(tok::l_brace)) ||
+  if (Style.Language != FormatStyle::LK_Proto &&
+      ((Previous.Type == TT_DictLiteral && Previous.is(tok::l_brace)) ||
        Previous.Type == TT_ArrayInitializerLSquare) &&
       Style.ColumnLimit > 0 &&
       getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State))
index 885d147b9b60b277d1eece704355dc135ed1d33d..269e2fc28aaedbe1d78677aa9ebfa0c35e0644b8 100644 (file)
@@ -311,8 +311,7 @@ private:
         if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
           return false;
         updateParameterCount(Left, CurrentToken);
-        if (CurrentToken->is(tok::colon) &&
-            Style.Language != FormatStyle::LK_Proto) {
+        if (CurrentToken->is(tok::colon)) {
           if (CurrentToken->getPreviousNonComment()->is(tok::identifier))
             CurrentToken->getPreviousNonComment()->Type = TT_SelectorName;
           Left->Type = TT_DictLiteral;
@@ -1683,6 +1682,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
   } else if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
     return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
            Style.BreakBeforeBraces == FormatStyle::BS_GNU;
+  } else if (Style.Language == FormatStyle::LK_Proto &&
+             Left.isNot(tok::l_brace) && Right.Type == TT_SelectorName) {
+    return true;
   }
 
   // If the last token before a '}' is a comma or a comment, the intention is to
index bfd502566757e49793dfc6d46ad5133b663374f9..3ff38eab024ea1c759f7fa4d7bdb32b310be55e6 100644 (file)
@@ -98,8 +98,27 @@ TEST_F(FormatTestProto, MessageFieldAttributes) {
 }
 
 TEST_F(FormatTestProto, FormatsOptions) {
-  verifyFormat("option java_package = \"my.test.package\";");
-  verifyFormat("option (my_custom_option) = \"abc\";");
+  verifyFormat("option (MyProto.options) = {\n"
+               "  field_a: OK\n"
+               "  field_b: \"OK\"\n"
+               "  field_c: \"OK\"\n"
+               "  msg_field: {field_d: 123}\n"
+               "};");
+
+  verifyFormat("option (MyProto.options) = {\n"
+               "  field_a: OK\n"
+               "  field_b: \"OK\"\n"
+               "  field_c: \"OK\"\n"
+               "  msg_field: {field_d: 123\n"
+               "              field_e: OK}\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"
+               "};");
 }
 
 TEST_F(FormatTestProto, FormatsService) {