Skip to content

Commit

Permalink
[clang-format] SpacesInSquareBrackets not working for Java (#77833)
Browse files Browse the repository at this point in the history
spaces in [] needs to be handled the same in Java the same as C#.

Co-authored-by: paul_hoad <paul_hoad@amat.com>
  • Loading branch information
mydeveloperday and phoad107185 committed Jan 12, 2024
1 parent ef156f9 commit c65b939
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -4674,6 +4674,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
} else if (Style.Language == FormatStyle::LK_Java) {
if (Left.is(tok::r_square) && Right.is(tok::l_brace))
return true;
// spaces inside square brackets.
if (Left.is(tok::l_square) || Right.is(tok::r_square))
return Style.SpacesInSquareBrackets;

if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
spaceRequiredBeforeParens(Right);
Expand Down
15 changes: 15 additions & 0 deletions clang/unittests/Format/FormatTestJava.cpp
Expand Up @@ -603,6 +603,21 @@ TEST_F(FormatTestJava, ShortFunctions) {
Style);
}

TEST_F(FormatTestJava, ConfigurableSpacesInSquareBrackets) {
FormatStyle Spaces = getLLVMStyle(FormatStyle::LK_Java);

verifyFormat("Object[] arguments", Spaces);
verifyFormat("final Class<?>[] types = new Class<?>[numElements];", Spaces);
verifyFormat("types[i] = arguments[i].getClass();", Spaces);

Spaces.SpacesInSquareBrackets = true;

verifyFormat("Object[ ] arguments", Spaces);
verifyFormat("final Class<?>[ ] types = new Class<?>[ numElements ];",
Spaces);
verifyFormat("types[ i ] = arguments[ i ].getClass();", Spaces);
}

} // namespace
} // namespace test
} // namespace format
Expand Down

0 comments on commit c65b939

Please sign in to comment.