Skip to content

Commit 006bf8d

Browse files
[clang-format][NFC] Handle language specific stuff at the top...
... of TokenAnnotator::splitPenalty. That is in my eyes a bit clearer in the workflow. As a drive-by introduce (but not adapt anywhere else) isProto(). Differential Revision: https://reviews.llvm.org/D135871
1 parent c5755f4 commit 006bf8d

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

clang/include/clang/Format/Format.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,7 @@ struct FormatStyle {
26172617
bool isJson() const { return Language == LK_Json; }
26182618
bool isJavaScript() const { return Language == LK_JavaScript; }
26192619
bool isVerilog() const { return Language == LK_Verilog; }
2620+
bool isProto() const { return Language == LK_Proto; }
26202621

26212622
/// Language, this format style is targeted at.
26222623
/// \version 3.5

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3097,6 +3097,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
30973097
if (Left.is(tok::semi))
30983098
return 0;
30993099

3100+
// Language specific handling.
31003101
if (Style.Language == FormatStyle::LK_Java) {
31013102
if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
31023103
return 1;
@@ -3116,13 +3117,16 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
31163117
// Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
31173118
if (Left.opensScope() && Right.closesScope())
31183119
return 200;
3120+
} else if (Style.isProto()) {
3121+
if (Right.is(tok::l_square))
3122+
return 1;
3123+
if (Right.is(tok::period))
3124+
return 500;
31193125
}
31203126

31213127
if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
31223128
return 1;
31233129
if (Right.is(tok::l_square)) {
3124-
if (Style.Language == FormatStyle::LK_Proto)
3125-
return 1;
31263130
if (Left.is(tok::r_square))
31273131
return 200;
31283132
// Slightly prefer formatting local lambda definitions like functions.
@@ -3135,10 +3139,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
31353139
}
31363140
}
31373141

3138-
if (Left.is(tok::coloncolon) ||
3139-
(Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto)) {
3142+
if (Left.is(tok::coloncolon))
31403143
return 500;
3141-
}
31423144
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
31433145
Right.is(tok::kw_operator)) {
31443146
if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)

0 commit comments

Comments
 (0)