[PATCH] D43180: [clang-format] Support text proto extensions

2018-02-13 Thread Krasimir Georgiev via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rC324995: [clang-format] Support text proto extensions 
(authored by krasimir, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D43180?vs=133872=134013#toc

Repository:
  rC Clang

https://reviews.llvm.org/D43180

Files:
  lib/Format/ContinuationIndenter.cpp
  lib/Format/FormatToken.h
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTestProto.cpp
  unittests/Format/FormatTestTextProto.cpp

Index: lib/Format/TokenAnnotator.cpp
===
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -368,12 +368,35 @@
  Parent->is(TT_TemplateCloser)) {
 Left->Type = TT_ArraySubscriptLSquare;
   } else if (Style.Language == FormatStyle::LK_Proto ||
- (!CppArrayTemplates && Parent &&
-  Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
-  tok::comma, tok::l_paren, tok::l_square,
-  tok::question, tok::colon, tok::kw_return,
-  // Should only be relevant to JavaScript:
-  tok::kw_default))) {
+ Style.Language == FormatStyle::LK_TextProto) {
+// Square braces in LK_Proto can either be message field attributes:
+//
+// optional Aaa aaa = 1 [
+//   (aaa) = aaa
+// ];
+//
+// or text proto extensions (in options):
+//
+// option (Aaa.options) = {
+//   [type.type/type] {
+// key: value
+//   }
+// }
+//
+// In the first case we want to spread the contents inside the square
+// braces; in the second we want to keep them inline.
+Left->Type = TT_ArrayInitializerLSquare;
+if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
+tok::equal)) {
+  Left->Type = TT_ProtoExtensionLSquare;
+  BindingIncrease = 10;
+}
+  } else if (!CppArrayTemplates && Parent &&
+ Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
+ tok::comma, tok::l_paren, tok::l_square,
+ tok::question, tok::colon, tok::kw_return,
+ // Should only be relevant to JavaScript:
+ tok::kw_default)) {
 Left->Type = TT_ArrayInitializerLSquare;
   } else {
 BindingIncrease = 10;
@@ -2396,6 +2419,12 @@
   return true;
 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
   return true;
+// Slashes occur in text protocol extension syntax: [type/type] { ... }.
+if (Left.is(tok::slash) || Right.is(tok::slash))
+  return false;
+if (Left.MatchingParen && Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
+Right.isOneOf(tok::l_brace, tok::less))
+  return !Style.Cpp11BracedListStyle;
   } else if (Style.Language == FormatStyle::LK_JavaScript) {
 if (Left.is(TT_JsFatArrow))
   return true;
@@ -2732,6 +2761,9 @@
   (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations))
 return true;
 
+  if (Right.is(TT_ProtoExtensionLSquare))
+return true;
+
   return false;
 }
 
Index: lib/Format/ContinuationIndenter.cpp
===
--- lib/Format/ContinuationIndenter.cpp
+++ lib/Format/ContinuationIndenter.cpp
@@ -943,6 +943,8 @@
   if (Previous.is(tok::r_paren) && !Current.isBinaryOperator() &&
   !Current.isOneOf(tok::colon, tok::comment))
 return ContinuationIndent;
+  if (Current.is(TT_ProtoExtensionLSquare))
+return State.Stack.back().Indent;
   if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment &&
   PreviousNonComment->isNot(tok::r_brace))
 // Ensure that we fall back to the continuation indent width instead of
Index: lib/Format/FormatToken.h
===
--- lib/Format/FormatToken.h
+++ lib/Format/FormatToken.h
@@ -88,6 +88,7 @@
   TYPE(TemplateCloser) \
   TYPE(TemplateOpener) \
   TYPE(TemplateString) \
+  TYPE(ProtoExtensionLSquare)  \
   TYPE(TrailingAnnotation) \
   TYPE(TrailingReturnArrow)\
   TYPE(TrailingUnaryOperator)  \
Index: unittests/Format/FormatTestTextProto.cpp
===
--- unittests/Format/FormatTestTextProto.cpp
+++ 

[PATCH] D43180: [clang-format] Support text proto extensions

2018-02-12 Thread Daniel Jasper via Phabricator via cfe-commits
djasper accepted this revision.
djasper added a comment.
This revision is now accepted and ready to land.

Cool, thanks.


Repository:
  rC Clang

https://reviews.llvm.org/D43180



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D43180: [clang-format] Support text proto extensions

2018-02-12 Thread Krasimir Georgiev via Phabricator via cfe-commits
krasimir marked an inline comment as done.
krasimir added inline comments.



Comment at: unittests/Format/FormatTestTextProto.cpp:317
+
+TEST_F(FormatTestTextProto, FormatsExtensions) {
+  verifyFormat("[type] { key: value }");

djasper wrote:
> It might be useful to attach a test case for what happens if the "[...]" does 
> not fit on one line. I don't even know how I would format that, but it seems 
> important to know and not accidentally modify the behavior.
I added some tests. The current behavior seems reasonable.


Repository:
  rC Clang

https://reviews.llvm.org/D43180



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D43180: [clang-format] Support text proto extensions

2018-02-12 Thread Krasimir Georgiev via Phabricator via cfe-commits
krasimir updated this revision to Diff 133872.
krasimir added a comment.

- Add tests with long [...]


Repository:
  rC Clang

https://reviews.llvm.org/D43180

Files:
  lib/Format/ContinuationIndenter.cpp
  lib/Format/FormatToken.h
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTestProto.cpp
  unittests/Format/FormatTestTextProto.cpp

Index: unittests/Format/FormatTestTextProto.cpp
===
--- unittests/Format/FormatTestTextProto.cpp
+++ unittests/Format/FormatTestTextProto.cpp
@@ -313,5 +313,67 @@
   "  text: \"aaasaa\"\n"
   "}");
 }
+
+TEST_F(FormatTestTextProto, FormatsExtensions) {
+  verifyFormat("[type] { key: value }");
+  verifyFormat("[type] {\n"
+   "  key: value\n"
+   "}");
+  verifyFormat("[type.type] { key: value }");
+  verifyFormat("[type.type] < key: value >");
+  verifyFormat("[type.type/type.type] { key: value }");
+  verifyFormat("msg {\n"
+   "  [type.type] { key: value }\n"
+   "}");
+  verifyFormat("msg {\n"
+   "  [type.type] {\n"
+   "keyy: value\n"
+   "  }\n"
+   "}");
+  verifyFormat("key: value\n"
+   "[a.b] { key: value }");
+  verifyFormat("msg: <\n"
+   "  key: value\n"
+   "  [a.b.c/d.e]: < key: value >\n"
+   "  [f.g]: <\n"
+   "key: valuee\n"
+   "key: {}\n"
+   "  >\n"
+   "  key {}\n"
+   "  [h.i.j] < key: value >\n"
+   "  [a]: {\n"
+   "[b.c]: {}\n"
+   "[d] <>\n"
+   "[e/f]: 1\n"
+   "  }\n"
+   ">");
+  verifyFormat("[longg.long.long.long.long.long.long.long.long.long.long\n"
+   " .longg.longlong] { key: value }");
+  verifyFormat("[longg.long.long.long.long.long.long.long.long.long.long\n"
+   " .longg.longlong] {\n"
+   "  key: value\n"
+   "  key: value\n"
+   "  key: value\n"
+   "  key: value\n"
+   "}");
+  verifyFormat("[longg.long.long.long.long.long.long.long.long.long\n"
+   " .long/longg.longlong] { key: value }");
+  verifyFormat("[aa/\n"
+   " bb] { key: value }");
+  verifyFormat("[\n"
+   "] { key: value }");
+  verifyFormat("[\n"
+   "] {\n"
+   "  [type.type] {\n"
+   "keyy: value\n"
+   "  }\n"
+   "}");
+  verifyFormat("[aaa/\n"
+   " bbb] {\n"
+   "  [type.type] {\n"
+   "keyy: value\n"
+   "  }\n"
+   "}");
+}
 } // end namespace tooling
 } // end namespace clang
Index: unittests/Format/FormatTestProto.cpp
===
--- unittests/Format/FormatTestProto.cpp
+++ unittests/Format/FormatTestProto.cpp
@@ -421,5 +421,16 @@
   "}");
 }
 
+TEST_F(FormatTestProto, FormatsOptionsExtensions) {
+  verifyFormat("option (MyProto.options) = {\n"
+   "  msg_field: { field_d: 123 }\n"
+   "  [ext.t/u] { key: value }\n"
+   "  key: value\n"
+   "  [t.u/v] <\n"
+   "[ext] { key: value }\n"
+   "  >\n"
+   "};");
+}
+
 } // end namespace tooling
 } // end namespace clang
Index: lib/Format/TokenAnnotator.cpp
===
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -368,12 +368,35 @@
  Parent->is(TT_TemplateCloser)) {
 Left->Type = TT_ArraySubscriptLSquare;
   } else if (Style.Language == FormatStyle::LK_Proto ||
- (!CppArrayTemplates && Parent &&
-  Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
-  tok::comma, tok::l_paren, tok::l_square,
-  tok::question, tok::colon, tok::kw_return,
-  // Should only be relevant to JavaScript:
-  tok::kw_default))) {
+ Style.Language == FormatStyle::LK_TextProto) {
+// Square braces in LK_Proto can either be message field attributes:
+//
+// optional Aaa aaa = 1 [
+//   (aaa) = aaa
+// ];
+//
+// or text proto extensions (in options):
+  

[PATCH] D43180: [clang-format] Support text proto extensions

2018-02-12 Thread Daniel Jasper via Phabricator via cfe-commits
djasper added inline comments.



Comment at: unittests/Format/FormatTestTextProto.cpp:317
+
+TEST_F(FormatTestTextProto, FormatsExtensions) {
+  verifyFormat("[type] { key: value }");

It might be useful to attach a test case for what happens if the "[...]" does 
not fit on one line. I don't even know how I would format that, but it seems 
important to know and not accidentally modify the behavior.


Repository:
  rC Clang

https://reviews.llvm.org/D43180



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D43180: [clang-format] Support text proto extensions

2018-02-12 Thread Krasimir Georgiev via Phabricator via cfe-commits
krasimir created this revision.
Herald added subscribers: cfe-commits, klimek.

This adds support for text proto extensions, like:

  msg {
[type.type/ext] {
  key: value
}
  }


Repository:
  rC Clang

https://reviews.llvm.org/D43180

Files:
  lib/Format/ContinuationIndenter.cpp
  lib/Format/FormatToken.h
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTestProto.cpp
  unittests/Format/FormatTestTextProto.cpp

Index: unittests/Format/FormatTestTextProto.cpp
===
--- unittests/Format/FormatTestTextProto.cpp
+++ unittests/Format/FormatTestTextProto.cpp
@@ -313,5 +313,40 @@
   "  text: \"aaasaa\"\n"
   "}");
 }
+
+TEST_F(FormatTestTextProto, FormatsExtensions) {
+  verifyFormat("[type] { key: value }");
+  verifyFormat("[type] {\n"
+   "  key: value\n"
+   "}");
+  verifyFormat("[type.type] { key: value }");
+  verifyFormat("[type.type] < key: value >");
+  verifyFormat("[type.type/type.type] { key: value }");
+  verifyFormat("msg {\n"
+   "  [type.type] { key: value }\n"
+   "}");
+  verifyFormat("msg {\n"
+   "  [type.type] {\n"
+   "keyy: value\n"
+   "  }\n"
+   "}");
+  verifyFormat("key: value\n"
+   "[a.b] { key: value }");
+  verifyFormat("msg: <\n"
+   "  key: value\n"
+   "  [a.b.c/d.e]: < key: value >\n"
+   "  [f.g]: <\n"
+   "key: valuee\n"
+   "key: {}\n"
+   "  >\n"
+   "  key {}\n"
+   "  [h.i.j] < key: value >\n"
+   "  [a]: {\n"
+   "[b.c]: {}\n"
+   "[d] <>\n"
+   "[e/f]: 1\n"
+   "  }\n"
+   ">");
+}
 } // end namespace tooling
 } // end namespace clang
Index: unittests/Format/FormatTestProto.cpp
===
--- unittests/Format/FormatTestProto.cpp
+++ unittests/Format/FormatTestProto.cpp
@@ -421,5 +421,16 @@
   "}");
 }
 
+TEST_F(FormatTestProto, FormatsOptionsExtensions) {
+  verifyFormat("option (MyProto.options) = {\n"
+   "  msg_field: { field_d: 123 }\n"
+   "  [ext.t/u] { key: value }\n"
+   "  key: value\n"
+   "  [t.u/v] <\n"
+   "[ext] { key: value }\n"
+   "  >\n"
+   "};");
+}
+
 } // end namespace tooling
 } // end namespace clang
Index: lib/Format/TokenAnnotator.cpp
===
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -368,12 +368,35 @@
  Parent->is(TT_TemplateCloser)) {
 Left->Type = TT_ArraySubscriptLSquare;
   } else if (Style.Language == FormatStyle::LK_Proto ||
- (!CppArrayTemplates && Parent &&
-  Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
-  tok::comma, tok::l_paren, tok::l_square,
-  tok::question, tok::colon, tok::kw_return,
-  // Should only be relevant to JavaScript:
-  tok::kw_default))) {
+ Style.Language == FormatStyle::LK_TextProto) {
+// Square braces in LK_Proto can either be message field attributes:
+//
+// optional Aaa aaa = 1 [
+//   (aaa) = aaa
+// ];
+//
+// or text proto extensions (in options):
+//
+// option (Aaa.options) = {
+//   [type.type/type] {
+// key: value
+//   }
+// }
+//
+// In the first case we want to spread the contents inside the square
+// braces; in the second we want to keep them inline.
+Left->Type = TT_ArrayInitializerLSquare;
+if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
+tok::equal)) {
+  Left->Type = TT_ProtoExtensionLSquare;
+  BindingIncrease = 10;
+}
+  } else if (!CppArrayTemplates && Parent &&
+ Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
+ tok::comma, tok::l_paren, tok::l_square,
+ tok::question, tok::colon, tok::kw_return,
+ // Should only be relevant to JavaScript:
+ tok::kw_default)) {
 Left->Type = TT_ArrayInitializerLSquare;
   } else {
 BindingIncrease = 10;
@@ -2396,6 +2419,12 @@
   return true;
 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
   return true;
+// Slashes occur in