From ddb19248eb1bea29aaf5c9ed8b1b19e45b08e8e9 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Thu, 3 Aug 2017 14:17:29 +0000 Subject: [PATCH] [clang-format] Fix indent of 'key <...>' and 'key {...}' in text protos Summary: This patch fixes the indentation of the code pattern `key <...>`and `key {...}` in text protos. Previously, such line would be alinged depending on the column of the previous colon, which usually indents too much. I'm gonna go ahead and commit this since it's a straightforward bugfix. Reviewers: djasper, klimek Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D36143 llvm-svn: 309941 --- clang/lib/Format/ContinuationIndenter.cpp | 5 ++- clang/unittests/Format/FormatTestProto.cpp | 13 ++++++ .../unittests/Format/FormatTestTextProto.cpp | 44 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 26dacf6562be..25a470474b71 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -731,7 +731,10 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope()) return State.Stack[State.Stack.size() - 2].LastSpace; if (Current.is(tok::identifier) && Current.Next && - Current.Next->is(TT_DictLiteral)) + (Current.Next->is(TT_DictLiteral) || + ((Style.Language == FormatStyle::LK_Proto || + Style.Language == FormatStyle::LK_TextProto) && + Current.Next->isOneOf(TT_TemplateOpener, tok::l_brace)))) return State.Stack.back().Indent; if (NextNonComment->is(TT_ObjCStringLiteral) && State.StartOfStringLiteral != 0) diff --git a/clang/unittests/Format/FormatTestProto.cpp b/clang/unittests/Format/FormatTestProto.cpp index ca26c5439591..df94d8172730 100644 --- a/clang/unittests/Format/FormatTestProto.cpp +++ b/clang/unittests/Format/FormatTestProto.cpp @@ -361,6 +361,19 @@ TEST_F(FormatTestProto, FormatsOptions) { " data1 \n" " data2 {key2: value2}\n" ">;"); + + verifyFormat("option (MyProto.options) = <\n" + " app_id: 'com.javax.swing.salsa.latino'\n" + " head_id: 1\n" + " data \n" + ">;"); + + verifyFormat("option (MyProto.options) = {\n" + " app_id: 'com.javax.swing.salsa.latino'\n" + " head_id: 1\n" + " headheadheadheadheadhead_id: 1\n" + " product_data {product {1}}\n" + "};"); } TEST_F(FormatTestProto, FormatsService) { diff --git a/clang/unittests/Format/FormatTestTextProto.cpp b/clang/unittests/Format/FormatTestTextProto.cpp index 2de7e181f2cb..0a7bcdd82362 100644 --- a/clang/unittests/Format/FormatTestTextProto.cpp +++ b/clang/unittests/Format/FormatTestTextProto.cpp @@ -245,6 +245,50 @@ TEST_F(FormatTestTextProto, SupportsAngleBracketMessageFields) { ">\n" "field: OK,\n" "field_c >>"); + + verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n" + "head_id: 1\n" + "data "); + + verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n" + "head_id: 1\n" + "data \n" + "tail_id: 2"); + + verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n" + "head_id: 1\n" + "data \n" + "data {key: value}"); + + verifyFormat("app {\n" + " app_id: 'com.javax.swing.salsa.latino'\n" + " head_id: 1\n" + " data \n" + "}"); + + verifyFormat("app: {\n" + " app_id: 'com.javax.swing.salsa.latino'\n" + " head_id: 1\n" + " data \n" + "}"); + + verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n" + "headheadheadheadheadhead_id: 1\n" + "product_data {product {1}}"); + + verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n" + "headheadheadheadheadhead_id: 1\n" + "product_data "); + + verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n" + "headheadheadheadheadhead_id: 1\n" + "product_data >"); + + verifyFormat("app <\n" + " app_id: 'com.javax.swing.salsa.latino'\n" + " headheadheadheadheadhead_id: 1\n" + " product_data \n" + ">"); } } // end namespace tooling } // end namespace clang