Skip to content

Commit

Permalink
Formatter: Fix method parameter alignment bug (#3612)
Browse files Browse the repository at this point in the history
The multi-line method parameter alignment which was introduced in #1920 caused excessive indentation when the declaring method had annotations.
  • Loading branch information
knutwannheden authored Oct 12, 2023
1 parent 572ce98 commit c54853f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ void alignMethodDeclarationParamsWhenMultiple() {
java(
"""
class Test {
@SuppressWarnings
private void firstArgNoPrefix(String first,
int times,
String third
int times,
String third
) {
}
private void firstArgOnNewLine(
Expand All @@ -118,6 +119,7 @@ private void firstArgOnNewLine(
""",
"""
class Test {
@SuppressWarnings
private void firstArgNoPrefix(String first,
int times,
String third
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ public <T> JRightPadded<T> visitRightPadded(@Nullable JRightPadded<T> right, JRi
alignTo = firstArg.getPrefix().getLastWhitespace().length() - 1;
} else {
String source = method.print(getCursor());
alignTo = source.indexOf(firstArg.print(getCursor())) - 1;
int firstArgIndex = source.indexOf(firstArg.print(getCursor()));
int lineBreakIndex = source.lastIndexOf('\n', firstArgIndex);
alignTo = (firstArgIndex - (lineBreakIndex == -1 ? 0 : lineBreakIndex)) - 1;
}
getCursor().getParentOrThrow().putMessage("lastIndent", alignTo - style.getContinuationIndent());
elem = visitAndCast(elem, p);
Expand Down

0 comments on commit c54853f

Please sign in to comment.