From 54a0538056c241616760980d38ea1dfeb594dd22 Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Thu, 3 Oct 2024 20:10:40 +0200 Subject: [PATCH 1/5] add test case to illustrate issue with unsolicited indentation. --- .../openrewrite/java/OrderImportsTest.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java index 0064a9f5b74..ab1b28dc318 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java @@ -709,4 +709,52 @@ public class Foo { ) ); } + + /** + * This style should only reorder the imports but it indents the `extends` keyword as well. + * Context is: I'm trying to enforce a custom order of imports, so I don't want this style to do anything else. + */ + @Issue("https://github.com/openrewrite/rewrite/issues/4165") + @Test + void shouldNotIndentExtends() { + rewriteRun( + spec -> spec.parser(JavaParser.fromJavaVersion().styles( + singletonList( + new NamedStyles( + randomId(), + "it.does.more.than.JustOrderImports", + "custom style", + null, + emptySet(), + singletonList( + ImportLayoutStyle.builder() + .importAllOthers() + .blankLine() + .importStaticAllOthers() + .build() + ) + ) + ))), + java("class BaseClass {}"), + java( + """ + import static org.junit.jupiter.api.Assertions.*; + + import java.io.*; + class Test + extends BaseClass { + } + """, + """ + import java.io.*; + + import static org.junit.jupiter.api.Assertions.*; + + class Test + extends BaseClass { + } + """ + ) + ); + } } From 5acbbf44c22c3fb59693395955c124dde7b75a4f Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Fri, 4 Oct 2024 14:16:09 +0200 Subject: [PATCH 2/5] fix test --- .../openrewrite/java/OrderImportsTest.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java index ab1b28dc318..db80cea957f 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java @@ -19,10 +19,13 @@ import org.openrewrite.DocumentExample; import org.openrewrite.Issue; import org.openrewrite.java.style.ImportLayoutStyle; +import org.openrewrite.java.style.TabsAndIndentsStyle; import org.openrewrite.style.NamedStyles; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import java.util.Arrays; + import static java.util.Collections.emptySet; import static java.util.Collections.singletonList; import static org.openrewrite.Tree.randomId; @@ -711,8 +714,9 @@ public class Foo { } /** - * This style should only reorder the imports but it indents the `extends` keyword as well. - * Context is: I'm trying to enforce a custom order of imports, so I don't want this style to do anything else. + * With only an ImportLayoutStyle and no TabAndIndentsStyle the recipe indents the `extends` keyword as well. + * This test verifies that TabAndIndentsStyle can be configured (with continuationIndent <= -2) so that it leaves + * the code following the imports as it is. */ @Issue("https://github.com/openrewrite/rewrite/issues/4165") @Test @@ -722,16 +726,17 @@ void shouldNotIndentExtends() { singletonList( new NamedStyles( randomId(), - "it.does.more.than.JustOrderImports", - "custom style", - null, + "it.will.only.OrderImports", + "Order imports by custom style", + "This style defines the order of imports and nothing else", emptySet(), - singletonList( + Arrays.asList( ImportLayoutStyle.builder() .importAllOthers() .blankLine() .importStaticAllOthers() - .build() + .build(), + new TabsAndIndentsStyle(false, -2, -2, -2, false, null) ) ) ))), @@ -743,6 +748,10 @@ void shouldNotIndentExtends() { import java.io.*; class Test extends BaseClass { + + int foo;//nonstandard indentation + + \tint ba;//tabbed indentation } """, """ @@ -752,6 +761,10 @@ class Test class Test extends BaseClass { + + int foo;//nonstandard indentation + + \tint ba;//tabbed indentation } """ ) From ffab4e6d054ba2c0bb3230698c3a7a905f0fff13 Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Fri, 4 Oct 2024 14:35:05 +0200 Subject: [PATCH 3/5] =?UTF-8?q?use=20prettier=20unicode=20=E2=89=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test/java/org/openrewrite/java/OrderImportsTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java index db80cea957f..1fcf810330f 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java @@ -715,7 +715,7 @@ public class Foo { /** * With only an ImportLayoutStyle and no TabAndIndentsStyle the recipe indents the `extends` keyword as well. - * This test verifies that TabAndIndentsStyle can be configured (with continuationIndent <= -2) so that it leaves + * This test verifies that TabAndIndentsStyle can be configured (with continuationIndent ≤ -2) so that it leaves * the code following the imports as it is. */ @Issue("https://github.com/openrewrite/rewrite/issues/4165") @@ -736,7 +736,7 @@ void shouldNotIndentExtends() { .blankLine() .importStaticAllOthers() .build(), - new TabsAndIndentsStyle(false, -2, -2, -2, false, null) + new TabsAndIndentsStyle(false, -2, -2, null, false, null) ) ) ))), From e56ff31097ab0e5d5c578fb4e5c9c2ab4d393666 Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Sun, 6 Oct 2024 01:43:29 +0200 Subject: [PATCH 4/5] found another one --- .../openrewrite/java/OrderImportsTest.java | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java index 1fcf810330f..bf6ac85aca2 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java @@ -742,7 +742,7 @@ void shouldNotIndentExtends() { ))), java("class BaseClass {}"), java( - """ + """ import static org.junit.jupiter.api.Assertions.*; import java.io.*; @@ -756,9 +756,9 @@ class Test """, """ import java.io.*; - + import static org.junit.jupiter.api.Assertions.*; - + class Test extends BaseClass { @@ -767,6 +767,39 @@ class Test \tint ba;//tabbed indentation } """ + ), + java( + """ + import com.fasterxml.jackson.core.*; + + import com.fasterxml.jackson.databind.*; + + public class CollectionDeserializer { + + void a() + { + } + /** + * JavaDoc without a gap. + */ + class B {} + } + """, + """ + import com.fasterxml.jackson.core.*; + import com.fasterxml.jackson.databind.*; + + public class CollectionDeserializer { + + void a() + { + } + /** + * JavaDoc without a gap. + */ + class B {} + } + """ ) ); } From 1fe9368851ee409127e65ce1c57628679f36ad80 Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Sun, 6 Oct 2024 02:01:41 +0200 Subject: [PATCH 5/5] and another one --- .../openrewrite/java/OrderImportsTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java index bf6ac85aca2..321f870ee80 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java @@ -800,6 +800,25 @@ void a() class B {} } """ + ), + java( + """ + import java.lang.annotation.Target; + + import com.fasterxml.jackson.annotation.JsonInclude; + import com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter; + + @Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE }) + public @interface JsonAppend {} + """, + """ + import com.fasterxml.jackson.annotation.JsonInclude; + import com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter; + import java.lang.annotation.Target; + + @Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE }) + public @interface JsonAppend {} + """ ) ); }