From: Krasimir Georgiev Date: Thu, 25 Jan 2018 14:10:43 +0000 (+0000) Subject: [clang-format] Fixes indentation of inner text proto messages X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f949e0451d222ba394d169f052a0658b259a4af3;p=clang [clang-format] Fixes indentation of inner text proto messages 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 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index ca648dc1ec..19f2ddae6c 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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; diff --git a/unittests/Format/FormatTestTextProto.cpp b/unittests/Format/FormatTestTextProto.cpp index 82da8737a3..26de5c9d10 100644 --- a/unittests/Format/FormatTestTextProto.cpp +++ b/unittests/Format/FormatTestTextProto.cpp @@ -289,6 +289,10 @@ TEST_F(FormatTestTextProto, SupportsAngleBracketMessageFields) { " headheadheadheadheadhead_id: 1\n" " product_data \n" ">"); + + verifyFormat("dcccwrnfioeruvginerurneitinfo {\n" + " exte3nsionrnfvui {key: value}\n" + "}"); } TEST_F(FormatTestTextProto, DiscardsUnbreakableTailIfCanBreakAfter) {