Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] SpacesInSquareBrackets not working for Java #77833

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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