]> granicus.if.org Git - clang/commitdiff
[clang-format] Fixes indentation of inner text proto messages
authorKrasimir Georgiev <krasimir@google.com>
Thu, 25 Jan 2018 14:10:43 +0000 (14:10 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Thu, 25 Jan 2018 14:10:43 +0000 (14:10 +0000)
Summary:
Consider the text proto:
```
message {
  sub { key: value }
}
```
Previously the first `{` was TT_Unknown, which caused the inner message to be
indented by the continuation width. This didn't happen for:
```
message {
  sub: { key: value }
}
```
This is because the code to mark the first `{` as a TT_DictLiteral was only
considering the case where it marches forward and reaches a `:`.

This patch updates this by looking not only for `:`, but also for `<` and `{`.

Reviewers: djasper

Reviewed By: djasper

Subscribers: klimek, cfe-commits

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

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

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestTextProto.cpp

index ca648dc1ec841b67f0487c3d6bd5bc4e07768fdd..19f2ddae6cf7d5f1da9102c022087636d0349188 100644 (file)
@@ -462,13 +462,15 @@ private:
           FormatToken *Previous = CurrentToken->getPreviousNonComment();
           if (Previous->is(TT_JsTypeOptionalQuestion))
             Previous = Previous->getPreviousNonComment();
-          if (((CurrentToken->is(tok::colon) &&
-                (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
-               Style.Language == FormatStyle::LK_Proto ||
-               Style.Language == FormatStyle::LK_TextProto) &&
-              (Previous->Tok.getIdentifierInfo() ||
-               Previous->is(tok::string_literal)))
-            Previous->Type = TT_SelectorName;
+          if ((CurrentToken->is(tok::colon) &&
+               (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
+              Style.Language == FormatStyle::LK_Proto ||
+              Style.Language == FormatStyle::LK_TextProto) {
+            Left->Type = TT_DictLiteral;
+            if (Previous->Tok.getIdentifierInfo() ||
+                Previous->is(tok::string_literal))
+              Previous->Type = TT_SelectorName;
+          }
           if (CurrentToken->is(tok::colon) ||
               Style.Language == FormatStyle::LK_JavaScript)
             Left->Type = TT_DictLiteral;
index 82da8737a377f60c07a8a986ed2f05e8bf6fd74b..26de5c9d106babc5de92866a74eac31abcc7ee6f 100644 (file)
@@ -289,6 +289,10 @@ TEST_F(FormatTestTextProto, SupportsAngleBracketMessageFields) {
                "  headheadheadheadheadhead_id: 1\n"
                "  product_data <product {1}>\n"
                ">");
+
+  verifyFormat("dcccwrnfioeruvginerurneitinfo {\n"
+               "  exte3nsionrnfvui {key: value}\n"
+               "}");
 }
 
 TEST_F(FormatTestTextProto, DiscardsUnbreakableTailIfCanBreakAfter) {