[PATCH] D43298: [clang-format] Support repeated field lists in protos

2018-02-15 Thread Krasimir Georgiev via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL325252: [clang-format] Support repeated field lists in 
protos (authored by krasimir, committed by ).
Herald added a subscriber: llvm-commits.

Repository:
  rL LLVM

https://reviews.llvm.org/D43298

Files:
  cfe/trunk/lib/Format/TokenAnnotator.cpp
  cfe/trunk/unittests/Format/FormatTestProto.cpp
  cfe/trunk/unittests/Format/FormatTestTextProto.cpp

Index: cfe/trunk/unittests/Format/FormatTestTextProto.cpp
===
--- cfe/trunk/unittests/Format/FormatTestTextProto.cpp
+++ cfe/trunk/unittests/Format/FormatTestTextProto.cpp
@@ -31,14 +31,18 @@
 return *Result;
   }
 
-  static std::string format(llvm::StringRef Code) {
-FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
-Style.ColumnLimit = 60; // To make writing tests easier.
+  static std::string format(llvm::StringRef Code, const FormatStyle ) {
 return format(Code, 0, Code.size(), Style);
   }
 
+  static void verifyFormat(llvm::StringRef Code, const FormatStyle ) {
+EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
+  }
+
   static void verifyFormat(llvm::StringRef Code) {
-EXPECT_EQ(Code.str(), format(test::messUp(Code)));
+FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
+Style.ColumnLimit = 60; // To make writing tests easier.
+verifyFormat(Code, Style);
   }
 };
 
@@ -390,5 +394,43 @@
 TEST_F(FormatTestTextProto, NoSpaceAfterPercent) {
   verifyFormat("key: %d");
 }
+
+TEST_F(FormatTestTextProto, FormatsRepeatedListInitializers) {
+  verifyFormat("keys: []");
+  verifyFormat("keys: [ 1 ]");
+  verifyFormat("keys: [ 'ala', 'bala' ]");
+  verifyFormat("keys:\n"
+   "[ 'ala', 'bala', 'porto', 'kala', 'too', 'long', 'ng' ]");
+  verifyFormat("key: item\n"
+   "keys: [\n"
+   "  'ala',\n"
+   "  'bala',\n"
+   "  'porto',\n"
+   "  'kala',\n"
+   "  'too',\n"
+   "  'long',\n"
+   "  'long',\n"
+   "  'long'\n"
+   "]\n"
+   "key: item\n"
+   "msg {\n"
+   "  key: item\n"
+   "  keys: [\n"
+   "'ala',\n"
+   "'bala',\n"
+   "'porto',\n"
+   "'kala',\n"
+   "'too',\n"
+   "'long',\n"
+   "'long'\n"
+   "  ]\n"
+   "}\n"
+   "key: value"
+   );
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
+  Style.ColumnLimit = 60; // To make writing tests easier.
+  Style.Cpp11BracedListStyle = true;
+  verifyFormat("keys: [1]", Style);
+}
 } // end namespace tooling
 } // end namespace clang
Index: cfe/trunk/unittests/Format/FormatTestProto.cpp
===
--- cfe/trunk/unittests/Format/FormatTestProto.cpp
+++ cfe/trunk/unittests/Format/FormatTestProto.cpp
@@ -438,5 +438,35 @@
"};");
 }
 
+TEST_F(FormatTestProto, FormatsRepeatedListInitializersInOptions) {
+  verifyFormat("option (MyProto.options) = {\n"
+   "  key: item\n"
+   "  keys: [\n"
+   "'ala',\n"
+   "'bala',\n"
+   "'porto',\n"
+   "'kala',\n"
+   "'too',\n"
+   "'long',\n"
+   "'long',\n"
+   "'long'\n"
+   "  ]\n"
+   "  key: [ item ]\n"
+   "  msg {\n"
+   "key: item\n"
+   "keys: [\n"
+   "  'ala',\n"
+   "  'bala',\n"
+   "  'porto',\n"
+   "  'kala',\n"
+   "  'too',\n"
+   "  'long',\n"
+   "  'long'\n"
+   "]\n"
+   "  }\n"
+   "  key: value\n"
+   "};");
+}
+
 } // end namespace tooling
 } // end namespace clang
Index: cfe/trunk/lib/Format/TokenAnnotator.cpp
===
--- cfe/trunk/lib/Format/TokenAnnotator.cpp
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp
@@ -383,11 +383,18 @@
 //   }
 // }
 //
-// In the first case we want to spread the contents inside the square
-// braces; in the second we want to keep them inline.
+// or repeated fields (in options):
+//
+// option (Aaa.options) = {
+//   keys: [ 1, 2, 3 ]
+// }
+//
+// In the first and the third 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,
-

[PATCH] D43298: [clang-format] Support repeated field lists in protos

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

- Pull out check in lambda
- Address review comments


Repository:
  rC Clang

https://reviews.llvm.org/D43298

Files:
  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
@@ -31,14 +31,18 @@
 return *Result;
   }
 
-  static std::string format(llvm::StringRef Code) {
-FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
-Style.ColumnLimit = 60; // To make writing tests easier.
+  static std::string format(llvm::StringRef Code, const FormatStyle ) {
 return format(Code, 0, Code.size(), Style);
   }
 
+  static void verifyFormat(llvm::StringRef Code, const FormatStyle ) {
+EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
+  }
+
   static void verifyFormat(llvm::StringRef Code) {
-EXPECT_EQ(Code.str(), format(test::messUp(Code)));
+FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
+Style.ColumnLimit = 60; // To make writing tests easier.
+verifyFormat(Code, Style);
   }
 };
 
@@ -390,5 +394,43 @@
 TEST_F(FormatTestTextProto, NoSpaceAfterPercent) {
   verifyFormat("key: %d");
 }
+
+TEST_F(FormatTestTextProto, FormatsRepeatedListInitializers) {
+  verifyFormat("keys: []");
+  verifyFormat("keys: [ 1 ]");
+  verifyFormat("keys: [ 'ala', 'bala' ]");
+  verifyFormat("keys:\n"
+   "[ 'ala', 'bala', 'porto', 'kala', 'too', 'long', 'ng' ]");
+  verifyFormat("key: item\n"
+   "keys: [\n"
+   "  'ala',\n"
+   "  'bala',\n"
+   "  'porto',\n"
+   "  'kala',\n"
+   "  'too',\n"
+   "  'long',\n"
+   "  'long',\n"
+   "  'long'\n"
+   "]\n"
+   "key: item\n"
+   "msg {\n"
+   "  key: item\n"
+   "  keys: [\n"
+   "'ala',\n"
+   "'bala',\n"
+   "'porto',\n"
+   "'kala',\n"
+   "'too',\n"
+   "'long',\n"
+   "'long'\n"
+   "  ]\n"
+   "}\n"
+   "key: value"
+   );
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
+  Style.ColumnLimit = 60; // To make writing tests easier.
+  Style.Cpp11BracedListStyle = true;
+  verifyFormat("keys: [1]", Style);
+}
 } // end namespace tooling
 } // end namespace clang
Index: unittests/Format/FormatTestProto.cpp
===
--- unittests/Format/FormatTestProto.cpp
+++ unittests/Format/FormatTestProto.cpp
@@ -438,5 +438,35 @@
"};");
 }
 
+TEST_F(FormatTestProto, FormatsRepeatedListInitializersInOptions) {
+  verifyFormat("option (MyProto.options) = {\n"
+   "  key: item\n"
+   "  keys: [\n"
+   "'ala',\n"
+   "'bala',\n"
+   "'porto',\n"
+   "'kala',\n"
+   "'too',\n"
+   "'long',\n"
+   "'long',\n"
+   "'long'\n"
+   "  ]\n"
+   "  key: [ item ]\n"
+   "  msg {\n"
+   "key: item\n"
+   "keys: [\n"
+   "  'ala',\n"
+   "  'bala',\n"
+   "  'porto',\n"
+   "  'kala',\n"
+   "  'too',\n"
+   "  'long',\n"
+   "  'long'\n"
+   "]\n"
+   "  }\n"
+   "  key: value\n"
+   "};");
+}
+
 } // end namespace tooling
 } // end namespace clang
Index: lib/Format/TokenAnnotator.cpp
===
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -383,11 +383,18 @@
 //   }
 // }
 //
-// In the first case we want to spread the contents inside the square
-// braces; in the second we want to keep them inline.
+// or repeated fields (in options):
+//
+// option (Aaa.options) = {
+//   keys: [ 1, 2, 3 ]
+// }
+//
+// In the first and the third 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)) {
+tok::equal) &&
+!Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
   Left->Type = TT_ProtoExtensionLSquare;
   BindingIncrease = 10;
 }

[PATCH] D43298: [clang-format] Support repeated field lists in protos

2018-02-15 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.

Looks good.




Comment at: lib/Format/TokenAnnotator.cpp:2355
+   ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
+ (Style.SpacesInContainerLiterals ||
+  ((Style.Language == FormatStyle::LK_Proto ||

This is almost a duplicate of the one above. Can we pull out a function or 
lambda?


Repository:
  rC Clang

https://reviews.llvm.org/D43298



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


[PATCH] D43298: [clang-format] Support repeated field lists in protos

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

This patch adds support for list initialization of proto repeated fields:

  keys: [1, 2, 3]


Repository:
  rC Clang

https://reviews.llvm.org/D43298

Files:
  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
@@ -31,14 +31,18 @@
 return *Result;
   }
 
-  static std::string format(llvm::StringRef Code) {
-FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
-Style.ColumnLimit = 60; // To make writing tests easier.
+  static std::string format(llvm::StringRef Code, const FormatStyle ) {
 return format(Code, 0, Code.size(), Style);
   }
 
+  static void verifyFormat(llvm::StringRef Code, const FormatStyle ) {
+EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
+  }
+
   static void verifyFormat(llvm::StringRef Code) {
-EXPECT_EQ(Code.str(), format(test::messUp(Code)));
+FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
+Style.ColumnLimit = 60; // To make writing tests easier.
+verifyFormat(Code, Style);
   }
 };
 
@@ -386,5 +390,43 @@
"  }\n"
"}");
 }
+
+TEST_F(FormatTestTextProto, FormatsRepeatedListInitializers) {
+  verifyFormat("keys: []");
+  verifyFormat("keys: [ 1 ]");
+  verifyFormat("keys: [ 'ala', 'bala' ]");
+  verifyFormat("keys:\n"
+   "[ 'ala', 'bala', 'porto', 'kala', 'too', 'long', 'ng' ]");
+  verifyFormat("key: item\n"
+   "keys: [\n"
+   "  'ala',\n"
+   "  'bala',\n"
+   "  'porto',\n"
+   "  'kala',\n"
+   "  'too',\n"
+   "  'long',\n"
+   "  'long',\n"
+   "  'long'\n"
+   "]\n"
+   "key: item\n"
+   "msg {\n"
+   "  key: item\n"
+   "  keys: [\n"
+   "'ala',\n"
+   "'bala',\n"
+   "'porto',\n"
+   "'kala',\n"
+   "'too',\n"
+   "'long',\n"
+   "'long'\n"
+   "  ]\n"
+   "}\n"
+   "key: value"
+   );
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
+  Style.ColumnLimit = 60; // To make writing tests easier.
+  Style.Cpp11BracedListStyle = true;
+  verifyFormat("keys: [1]", Style);
+}
 } // end namespace tooling
 } // end namespace clang
Index: unittests/Format/FormatTestProto.cpp
===
--- unittests/Format/FormatTestProto.cpp
+++ unittests/Format/FormatTestProto.cpp
@@ -432,5 +432,35 @@
"};");
 }
 
+TEST_F(FormatTestProto, FormatsRepeatedListInitializersInOptions) {
+  verifyFormat("option (MyProto.options) = {\n"
+   "  key: item\n"
+   "  keys: [\n"
+   "'ala',\n"
+   "'bala',\n"
+   "'porto',\n"
+   "'kala',\n"
+   "'too',\n"
+   "'long',\n"
+   "'long',\n"
+   "'long'\n"
+   "  ]\n"
+   "  key: [ item ]\n"
+   "  msg {\n"
+   "key: item\n"
+   "keys: [\n"
+   "  'ala',\n"
+   "  'bala',\n"
+   "  'porto',\n"
+   "  'kala',\n"
+   "  'too',\n"
+   "  'long',\n"
+   "  'long'\n"
+   "]\n"
+   "  }\n"
+   "  key: value\n"
+   "};");
+}
+
 } // end namespace tooling
 } // end namespace clang
Index: lib/Format/TokenAnnotator.cpp
===
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -383,11 +383,18 @@
 //   }
 // }
 //
-// In the first case we want to spread the contents inside the square
-// braces; in the second we want to keep them inline.
+// or repeated fields (in options):
+//
+// option (Aaa.options) = {
+//   keys: [ 1, 2, 3 ]
+// }
+//
+// In the first and the third 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)) {
+tok::equal) &&
+!Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
   Left->Type = TT_ProtoExtensionLSquare;
   BindingIncrease = 10;