Skip to content

Commit c65b939

Browse files
[clang-format] SpacesInSquareBrackets not working for Java (#77833)
spaces in [] needs to be handled the same in Java the same as C#. Co-authored-by: paul_hoad <[email protected]>
1 parent ef156f9 commit c65b939

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4674,6 +4674,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
46744674
} else if (Style.Language == FormatStyle::LK_Java) {
46754675
if (Left.is(tok::r_square) && Right.is(tok::l_brace))
46764676
return true;
4677+
// spaces inside square brackets.
4678+
if (Left.is(tok::l_square) || Right.is(tok::r_square))
4679+
return Style.SpacesInSquareBrackets;
4680+
46774681
if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
46784682
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
46794683
spaceRequiredBeforeParens(Right);

clang/unittests/Format/FormatTestJava.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,21 @@ TEST_F(FormatTestJava, ShortFunctions) {
603603
Style);
604604
}
605605

606+
TEST_F(FormatTestJava, ConfigurableSpacesInSquareBrackets) {
607+
FormatStyle Spaces = getLLVMStyle(FormatStyle::LK_Java);
608+
609+
verifyFormat("Object[] arguments", Spaces);
610+
verifyFormat("final Class<?>[] types = new Class<?>[numElements];", Spaces);
611+
verifyFormat("types[i] = arguments[i].getClass();", Spaces);
612+
613+
Spaces.SpacesInSquareBrackets = true;
614+
615+
verifyFormat("Object[ ] arguments", Spaces);
616+
verifyFormat("final Class<?>[ ] types = new Class<?>[ numElements ];",
617+
Spaces);
618+
verifyFormat("types[ i ] = arguments[ i ].getClass();", Spaces);
619+
}
620+
606621
} // namespace
607622
} // namespace test
608623
} // namespace format

0 commit comments

Comments
 (0)