Skip to content

Commit

Permalink
feat: style to reorder imports by Jackson code style
Browse files Browse the repository at this point in the history
  • Loading branch information
timo-a authored Oct 4, 2024
1 parent 6ede454 commit f58e68e
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/main/java/io/github/timoa/misc/JacksonImportStyle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.github.timoa.misc;

import org.openrewrite.java.style.ImportLayoutStyle;
import org.openrewrite.java.style.TabsAndIndentsStyle;
import org.openrewrite.style.NamedStyles;

import java.util.Arrays;

import static java.util.Collections.emptySet;
import static org.openrewrite.Tree.randomId;

public class JacksonImportStyle extends NamedStyles {
public JacksonImportStyle() {
super(randomId(),
"io.github.timoa.misc.JacksonImportStyle",
"custom style",
"custom style for the Jackson project",
emptySet(),
Arrays.asList(
importLayout(),
//needed so continuation indent, e.g. an `extends` on th next line is not modified.
new TabsAndIndentsStyle(false, -2, -2, -2, false, null
)

));
}

private static ImportLayoutStyle importLayout() {
return ImportLayoutStyle.builder()
.importPackage("java.*")
.blankLine()
.importAllOthers()
.blankLine()
.importPackage("com.fasterxml.jackson.annotation.*")
.blankLine()
.importPackage("com.fasterxml.jackson.core.*")
.importPackage("com.fasterxml.jackson.databind.*")
.importPackage("tools.jackson.*")//needed for master
.blankLine()
.importPackage("com.fasterxml.jackson.other.modules.*")
.blankLine()
.importStaticAllOthers()
.classCountToUseStarImport(Integer.MAX_VALUE)//disable collapsing imports to *
.build();
}

}
157 changes: 157 additions & 0 deletions src/test/java/io/github/timoa/misc/JacksonImportStyleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package io.github.timoa.misc;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.OrderImports;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static java.util.Collections.singletonList;
import static org.openrewrite.java.Assertions.java;

class JacksonImportStyleTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new OrderImports(false));
}

@DocumentExample
@Test
void simpleExample() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().styles(
singletonList(
new JacksonImportStyle())
)),
java(
"""
package com.fasterxml.jackson.core.write;
import com.fasterxml.jackson.core.*;
import java.io.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class Test {
}
""",
"""
package com.fasterxml.jackson.core.write;
import java.io.*;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.*;
import static org.junit.jupiter.api.Assertions.*;
class Test {
}
"""
)
);
}

@Test
void leaveNoGaps() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().styles(
singletonList(
new JacksonImportStyle())
)),
java(
"""
package com.fasterxml.jackson.core.write;
import com.fasterxml.jackson.core.*;
import java.io.*;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.jupiter.api.Test;
class GeneratorBasicDerivedTest {
}
""",
"""
package com.fasterxml.jackson.core.write;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.*;
import static org.junit.jupiter.api.Assertions.*;
class GeneratorBasicDerivedTest {
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/4165")
@Test
// todo reactivate once the issue above is resolved
void shouldNotIndentExtends() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().styles(
singletonList(
new JacksonImportStyle())
)),
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 {
}
"""
),
java(//different indentation here, just to be sure
"""
import static org.junit.jupiter.api.Assertions.*;
import java.io.*;
class Test2
extends BaseClass {
}
""",
"""
import java.io.*;
import static org.junit.jupiter.api.Assertions.*;
class Test2
extends BaseClass {
}
"""
)
);
}
}

0 comments on commit f58e68e

Please sign in to comment.