Skip to content

Commit 27012df

Browse files
authored
Prepare chopping and wrapping parameters in methodDeclarations. (#6129)
1 parent 74e97b1 commit 27012df

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

rewrite-java-test/src/test/java/org/openrewrite/java/format/TabsAndIndentsTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,42 @@ private void firstArgOnNewLine(
175175
);
176176
}
177177

178+
@Test
179+
void alignMethodDeclarationParamsWhenIndentationNeeded() {
180+
rewriteRun(
181+
java(
182+
"""
183+
class Test {
184+
@SuppressWarnings
185+
private void firstArgNoPrefix(
186+
String first,
187+
int times,
188+
String third) {}
189+
private void secondArgOnNewLine(
190+
String first,
191+
int times,
192+
String third
193+
) {}
194+
}
195+
""",
196+
"""
197+
class Test {
198+
@SuppressWarnings
199+
private void firstArgNoPrefix(
200+
String first,
201+
int times,
202+
String third) {}
203+
private void secondArgOnNewLine(
204+
String first,
205+
int times,
206+
String third
207+
) {}
208+
}
209+
"""
210+
)
211+
);
212+
}
213+
178214
@Issue("https://github.com/openrewrite/rewrite/issues/1913")
179215
@Test
180216
void alignMethodDeclarationParamsWhenContinuationIndent() {

rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,13 @@ private int computeFirstParameterColumn(J.MethodDeclaration method) {
397397
}
398398

399399
if (firstArg.getPrefix().getLastWhitespace().contains("\n")) {
400-
return getLengthOfWhitespace(firstArg.getPrefix().getLastWhitespace());
400+
int declPrefixLength = getLengthOfWhitespace(method.getPrefix().getLastWhitespace());
401+
int argPrefixLength = getLengthOfWhitespace(firstArg.getPrefix().getLastWhitespace());
402+
//noinspection ConstantConditions to be backwards compatible with older style versions
403+
if (declPrefixLength >= argPrefixLength) {
404+
return declPrefixLength + style.getContinuationIndent();
405+
}
406+
return argPrefixLength;
401407
} else {
402408
return computeColumnPosition(method, firstArg, getCursor());
403409
}

0 commit comments

Comments
 (0)