Skip to content

Commit 4b6e41b

Browse files
authored
[clang-format] Don't insert spaces after keywords in protobuf field o… (#111229)
…ptions Fixes #54848.
1 parent b443711 commit 4b6e41b

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4910,6 +4910,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
49104910
if (Left.is(tok::star) && Right.is(tok::comment))
49114911
return true;
49124912

4913+
const auto *BeforeLeft = Left.Previous;
4914+
49134915
if (IsCpp) {
49144916
if (Left.is(TT_OverloadedOperator) &&
49154917
Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
@@ -4962,7 +4964,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
49624964
if (Left.Tok.getIdentifierInfo() && Right.Tok.isLiteral())
49634965
return true;
49644966
} else if (Style.isProto()) {
4965-
if (Right.is(tok::period) &&
4967+
if (Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
49664968
Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
49674969
Keywords.kw_repeated, Keywords.kw_extend)) {
49684970
return true;
@@ -5070,8 +5072,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
50705072
if (Left.is(TT_FatArrow))
50715073
return true;
50725074
// for await ( ...
5073-
if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
5074-
Left.Previous->is(tok::kw_for)) {
5075+
if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && BeforeLeft &&
5076+
BeforeLeft->is(tok::kw_for)) {
50755077
return true;
50765078
}
50775079
if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
@@ -5108,7 +5110,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
51085110
return false;
51095111
// Valid JS method names can include keywords, e.g. `foo.delete()` or
51105112
// `bar.instanceof()`. Recognize call positions by preceding period.
5111-
if (Left.Previous && Left.Previous->is(tok::period) &&
5113+
if (BeforeLeft && BeforeLeft->is(tok::period) &&
51125114
Left.Tok.getIdentifierInfo()) {
51135115
return false;
51145116
}
@@ -5126,22 +5128,22 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
51265128
// "of" is only a keyword if it appears after another identifier
51275129
// (e.g. as "const x of y" in a for loop), or after a destructuring
51285130
// operation (const [x, y] of z, const {a, b} of c).
5129-
(Left.is(Keywords.kw_of) && Left.Previous &&
5130-
(Left.Previous->is(tok::identifier) ||
5131-
Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
5132-
(!Left.Previous || Left.Previous->isNot(tok::period))) {
5131+
(Left.is(Keywords.kw_of) && BeforeLeft &&
5132+
(BeforeLeft->is(tok::identifier) ||
5133+
BeforeLeft->isOneOf(tok::r_square, tok::r_brace)))) &&
5134+
(!BeforeLeft || BeforeLeft->isNot(tok::period))) {
51335135
return true;
51345136
}
5135-
if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
5136-
Left.Previous->is(tok::period) && Right.is(tok::l_paren)) {
5137+
if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5138+
BeforeLeft->is(tok::period) && Right.is(tok::l_paren)) {
51375139
return false;
51385140
}
51395141
if (Left.is(Keywords.kw_as) &&
51405142
Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
51415143
return true;
51425144
}
5143-
if (Left.is(tok::kw_default) && Left.Previous &&
5144-
Left.Previous->is(tok::kw_export)) {
5145+
if (Left.is(tok::kw_default) && BeforeLeft &&
5146+
BeforeLeft->is(tok::kw_export)) {
51455147
return true;
51465148
}
51475149
if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))

clang/unittests/Format/FormatTestProto.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ TEST_F(FormatTestProto, MessageFieldAttributes) {
190190
" aaaaaaaaaaaaaaaa: true\n"
191191
" }\n"
192192
"];");
193+
verifyFormat("repeated A a = 1 [(annotation).int32.repeated.test = true];");
193194
}
194195

195196
TEST_F(FormatTestProto, DoesntWrapFileOptions) {

0 commit comments

Comments
 (0)