Skip to content

Commit 6c4f968

Browse files
authored
[clang-format] Fix brace wrapping for Java records (#164711)
The brace wrapping for Java records should now behave similar to classes. Before, opening braces for Java records were always placed in the same line as the record definition.
1 parent db6231b commit 6c4f968

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ class LineJoiner {
285285
if (Tok && Tok->is(tok::kw_typedef))
286286
Tok = Tok->getNextNonComment();
287287
if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,
288-
tok::kw_extern, Keywords.kw_interface)) {
288+
tok::kw_extern, Keywords.kw_interface,
289+
Keywords.kw_record)) {
289290
return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
290291
? tryMergeSimpleBlock(I, E, Limit)
291292
: 0;
@@ -498,7 +499,8 @@ class LineJoiner {
498499
ShouldMerge = Style.AllowShortEnumsOnASingleLine;
499500
} else if (TheLine->Last->is(TT_CompoundRequirementLBrace)) {
500501
ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
501-
} else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
502+
} else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace,
503+
TT_RecordLBrace)) {
502504
// NOTE: We use AfterClass (whereas AfterStruct exists) for both classes
503505
// and structs, but it seems that wrapping is still handled correctly
504506
// elsewhere.
@@ -507,7 +509,7 @@ class LineJoiner {
507509
!Style.BraceWrapping.SplitEmptyRecord);
508510
} else if (TheLine->InPPDirective ||
509511
TheLine->First->isNoneOf(tok::kw_class, tok::kw_enum,
510-
tok::kw_struct)) {
512+
tok::kw_struct, Keywords.kw_record)) {
511513
// Try to merge a block with left brace unwrapped that wasn't yet
512514
// covered.
513515
ShouldMerge = !Style.BraceWrapping.AfterFunction ||

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,11 @@ static bool isIIFE(const UnwrappedLine &Line,
948948
}
949949

950950
static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
951-
const FormatToken &InitialToken) {
951+
const FormatToken &InitialToken,
952+
const bool IsJavaRecord) {
953+
if (IsJavaRecord)
954+
return Style.BraceWrapping.AfterClass;
955+
952956
tok::TokenKind Kind = InitialToken.Tok.getKind();
953957
if (InitialToken.is(TT_NamespaceMacro))
954958
Kind = tok::kw_namespace;
@@ -3200,7 +3204,7 @@ void UnwrappedLineParser::parseNamespace() {
32003204
if (FormatTok->is(tok::l_brace)) {
32013205
FormatTok->setFinalizedType(TT_NamespaceLBrace);
32023206

3203-
if (ShouldBreakBeforeBrace(Style, InitialToken))
3207+
if (ShouldBreakBeforeBrace(Style, InitialToken, /*IsJavaRecord=*/false))
32043208
addUnwrappedLine();
32053209

32063210
unsigned AddLevels =
@@ -3865,7 +3869,7 @@ bool UnwrappedLineParser::parseEnum() {
38653869
}
38663870

38673871
if (!Style.AllowShortEnumsOnASingleLine &&
3868-
ShouldBreakBeforeBrace(Style, InitialToken)) {
3872+
ShouldBreakBeforeBrace(Style, InitialToken, /*IsJavaRecord=*/false)) {
38693873
addUnwrappedLine();
38703874
}
38713875
// Parse enum body.
@@ -4160,7 +4164,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr, bool IsJavaRecord) {
41604164
if (ParseAsExpr) {
41614165
parseChildBlock();
41624166
} else {
4163-
if (ShouldBreakBeforeBrace(Style, InitialToken))
4167+
if (ShouldBreakBeforeBrace(Style, InitialToken, IsJavaRecord))
41644168
addUnwrappedLine();
41654169

41664170
unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u;

clang/unittests/Format/FormatTestJava.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,19 @@ TEST_F(FormatTestJava, TextBlock) {
848848
" Pat Q. Smith");
849849
}
850850

851+
TEST_F(FormatTestJava, BreakAfterRecord) {
852+
auto Style = getLLVMStyle(FormatStyle::LK_Java);
853+
Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
854+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
855+
Style.BraceWrapping.AfterClass = true;
856+
Style.BraceWrapping.SplitEmptyRecord = true;
857+
858+
verifyFormat("public record Foo(int i)\n"
859+
"{\n"
860+
"}",
861+
"public record Foo(int i) {}", Style);
862+
}
863+
851864
} // namespace
852865
} // namespace test
853866
} // namespace format

0 commit comments

Comments
 (0)