Skip to content

Commit

Permalink
AutoFormat bug with method parameter alignment when using tabs (#3724)
Browse files Browse the repository at this point in the history
* Test case reproducing autoformat bug for continuation indents and constructor arguments

* Properly calculate indentation when using tabs

* Fix off-by-1-error

---------

Co-authored-by: Knut Wannheden <[email protected]>
  • Loading branch information
shanman190 and knutwannheden authored Nov 22, 2023
1 parent 775c2d5 commit a9ab03f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,27 @@ private void firstArgOnNewLine(
);
}

@Test
void alignMethodDeclarationParamsWhenContinuationIndentUsingTabs() {
rewriteRun(
tabsAndIndents(style -> style.withUseTabCharacter(true)),
java(
"""
import java.util.*;
class Foo {
Foo(
String var1,
String var2,
String var3
) {
}
}
"""
)
);
}

// https://rules.sonarsource.com/java/tag/confusing/RSPEC-3973
@DocumentExample
@SuppressWarnings("SuspiciousIndentAfterControlStatement")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public <T> JRightPadded<T> visitRightPadded(@Nullable JRightPadded<T> right, JRi
if (method != null) {
int alignTo;
if (firstArg.getPrefix().getLastWhitespace().contains("\n")) {
alignTo = firstArg.getPrefix().getLastWhitespace().length() - 1;
alignTo = getLengthOfWhitespace(firstArg.getPrefix().getLastWhitespace());
} else {
String source = method.print(getCursor());
int firstArgIndex = source.indexOf(firstArg.print(getCursor()));
Expand Down

0 comments on commit a9ab03f

Please sign in to comment.