Skip to content

Commit

Permalink
[clang-format] Fix aligning of java-style declarations
Browse files Browse the repository at this point in the history
- Modify TokenAnnotator to work fine with java-style array declarations.
- Add test for aligning of java declarations.

Fixes #55931.

Differential Revision: https://reviews.llvm.org/D129628
  • Loading branch information
eoan-ermine authored and owenca committed Aug 16, 2022
1 parent fbc4c26 commit d8d331b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -2065,6 +2065,11 @@ class AnnotatingParser {
if (PreviousNotConst->isSimpleTypeSpecifier())
return true;

// type[] a in Java
if (Style.Language == FormatStyle::LK_Java &&
PreviousNotConst->is(tok::r_square))
return true;

// const a = in JavaScript.
return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
}
Expand Down
11 changes: 11 additions & 0 deletions clang/unittests/Format/FormatTestJava.cpp
Expand Up @@ -584,6 +584,17 @@ TEST_F(FormatTestJava, AlignsBlockComments) {
" void f() {}"));
}

TEST_F(FormatTestJava, AlignDeclarations) {
FormatStyle Style = getLLVMStyle(FormatStyle::LK_Java);
Style.AlignConsecutiveDeclarations.Enabled = true;
verifyFormat("private final String[] args;\n"
"private final A_ParserHelper parserHelper;\n"
"private final int numOfCmdArgs;\n"
"private int numOfCmdArgs;\n"
"private String[] args;",
Style);
}

TEST_F(FormatTestJava, KeepsDelimitersOnOwnLineInJavaDocComments) {
EXPECT_EQ("/**\n"
" * javadoc line 1\n"
Expand Down

0 comments on commit d8d331b

Please sign in to comment.