Hello,
This patch adds support for formatting proto text format embedded in .proto
files.
This kind of embedded text
can
appear in custom field options in
.
proto files.
For example:
option (MyProto.options) = {
field_a: OK
field_b: 123
msg_field: { field_c: 123 }
};
Thanks,
-K
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index 017afe1..91c540f 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -1604,7 +1604,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Tok.getNextNonComment() && Tok.Type != TT_ObjCMethodExpr &&
!Tok.Previous->is(tok::question) &&
- (Tok.Type != TT_DictLiteral || Style.SpacesInContainerLiterals);
+ (Tok.Type != TT_DictLiteral || Style.SpacesInContainerLiterals) &&
+ (Style.Language != FormatStyle::LK_Proto);
if (Tok.Previous->Type == TT_UnaryOperator ||
Tok.Previous->Type == TT_CastRParen)
return Tok.Type == TT_BinaryOperator;
diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp
index 20dd573..2ec9502 100644
--- a/lib/Format/UnwrappedLineParser.cpp
+++ b/lib/Format/UnwrappedLineParser.cpp
@@ -780,7 +780,21 @@ void UnwrappedLineParser::parseStructuralElement() {
nextToken();
if (Line->Tokens.size() == 1) {
if (FormatTok->Tok.is(tok::colon)) {
- parseLabel();
+ // If this is a proto option, break after a field declaration.
+ if (Style.Language == FormatStyle::LK_Proto) {
+ nextToken();
+ if (tok::isLiteral(FormatTok->Tok.getKind()) ||
+ tok::isAnyIdentifier(FormatTok->Tok.getKind()) ||
+ FormatTok->isOneOf(tok::kw_true, tok::kw_false)) {
+ nextToken();
+ if (FormatTok->Tok.is(tok::r_brace) ||
+ FormatTok->Tok.is(tok::identifier)) {
+ addUnwrappedLine();
+ }
+ }
+ } else {
+ parseLabel();
+ }
return;
}
// Recognize function-like macro usages without trailing semicolon.
@@ -803,7 +817,10 @@ void UnwrappedLineParser::parseStructuralElement() {
case tok::equal:
nextToken();
if (FormatTok->Tok.is(tok::l_brace)) {
- parseBracedList();
+ // In proto files, an equals sign is used in field options.
+ if (Style.Language != FormatStyle::LK_Proto) {
+ parseBracedList();
+ }
}
break;
case tok::l_square:
diff --git a/unittests/Format/FormatTestProto.cpp b/unittests/Format/FormatTestProto.cpp
index bfd5025..07bff4a 100644
--- a/unittests/Format/FormatTestProto.cpp
+++ b/unittests/Format/FormatTestProto.cpp
@@ -98,8 +98,29 @@ 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: {\n"
+ " field_d: 123\n"
+ " field_e: OK\n"
+ " }\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) {
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits