From 44fe2ff9f09ec783c6e240866b1b920dad741f49 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Fri, 15 Sep 2023 14:35:45 +0200 Subject: [PATCH] Align closing parentheses of method declaration parameters list --- .../openrewrite/java/format/TabsAndIndentsTest.java | 6 ++++-- .../java/format/TabsAndIndentsVisitor.java | 13 ++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/format/TabsAndIndentsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/format/TabsAndIndentsTest.java index 5a7f6d0faec..64b191138c2 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/format/TabsAndIndentsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/format/TabsAndIndentsTest.java @@ -109,7 +109,8 @@ private void firstArgNoPrefix(String first, private void firstArgOnNewLine( String first, int times, - String third) { + String third + ) { } } """, @@ -122,7 +123,8 @@ private void firstArgNoPrefix(String first, private void firstArgOnNewLine( String first, int times, - String third) { + String third + ) { } } """ diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java index cdeb41a2d12..b5d816e529c 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java @@ -210,8 +210,15 @@ public JRightPadded visitRightPadded(@Nullable JRightPadded right, JRi } case METHOD_DECLARATION_PARAMETER: case RECORD_STATE_VECTOR: { + if (elem instanceof J.Empty) { + elem = elem.withPrefix(indentTo(elem.getPrefix(), indent, loc.getAfterLocation())); + after = right.getAfter(); + break; + } JContainer container = getCursor().getParentOrThrow().getValue(); - J firstArg = container.getElements().iterator().next(); + List elements = container.getElements(); + J firstArg = elements.iterator().next(); + J lastArg = elements.get(elements.size() - 1); if (style.getMethodDeclarationParameters().getAlignWhenMultiple()) { J.MethodDeclaration method = getCursor().firstEnclosing(J.MethodDeclaration.class); if (method != null) { @@ -224,13 +231,13 @@ public JRightPadded visitRightPadded(@Nullable JRightPadded right, JRi } getCursor().getParentOrThrow().putMessage("lastIndent", alignTo - style.getContinuationIndent()); elem = visitAndCast(elem, p); - after = indentTo(right.getAfter(), alignTo, loc.getAfterLocation()); + after = indentTo(right.getAfter(), t == lastArg ? indent : alignTo, loc.getAfterLocation()); } else { after = right.getAfter(); } } else { elem = visitAndCast(elem, p); - after = indentTo(right.getAfter(), style.getContinuationIndent(), loc.getAfterLocation()); + after = indentTo(right.getAfter(), t == lastArg ? indent : style.getContinuationIndent(), loc.getAfterLocation()); } break; }