Skip to content

Commit

Permalink
Remove more unused params in Ktlint format step (#1897)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Nov 21, 2023
2 parents f3bf1e7 + 8469300 commit d5ddbcf
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ public Unit invoke(LintError lint, Boolean corrected) {

@Override
public String format(
String text,
Path path,
boolean isScript,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ public Unit invoke(LintError lint, Boolean corrected) {

@Override
public String format(
String text,
Path path,
boolean isScript,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ public Unit invoke(LintError lint, Boolean corrected) {

@Override
public String format(
String text,
Path path,
boolean isScript,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ public Unit invoke(LintError lint, Boolean corrected) {

@Override
public String format(
String text,
Path path,
boolean isScript,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
public interface KtLintCompatAdapter {

String format(
String text,
Path path,
boolean isScript,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import com.diffplug.spotless.glue.ktlint.compat.*;

public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {

private final boolean isScript;
private final KtLintCompatAdapter adapter;
private final FileSignature editorConfigPath;
private final Map<String, Object> editorConfigOverrideMap;

public KtlintFormatterFunc(String version, boolean isScript, FileSignature editorConfigPath,
public KtlintFormatterFunc(
String version,
FileSignature editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
String[] versions = version.split("\\.");
int majorVersion = Integer.parseInt(versions[0]);
Expand All @@ -57,16 +57,17 @@ public KtlintFormatterFunc(String version, boolean isScript, FileSignature edito
}
this.editorConfigPath = editorConfigPath;
this.editorConfigOverrideMap = editorConfigOverrideMap;
this.isScript = isScript;
}

@Override
public String applyWithFile(String unix, File file) {

Path absoluteEditorConfigPath = null;
if (editorConfigPath != null) {
absoluteEditorConfigPath = editorConfigPath.getOnlyFile().toPath();
}
return adapter.format(unix, file.toPath(), isScript, absoluteEditorConfigPath, editorConfigOverrideMap);
return adapter.format(
file.toPath(),
absoluteEditorConfigPath,
editorConfigOverrideMap);
}
}
27 changes: 8 additions & 19 deletions lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,26 @@ public class KtLintStep {
private KtLintStep() {}

private static final String DEFAULT_VERSION = "1.0.1";
static final String NAME = "ktlint";
static final String MAVEN_COORDINATE_0_DOT = "com.pinterest:ktlint:";
static final String MAVEN_COORDINATE_1_DOT = "com.pinterest.ktlint:ktlint-cli:";
private static final String NAME = "ktlint";
private static final String MAVEN_COORDINATE_0_DOT = "com.pinterest:ktlint:";
private static final String MAVEN_COORDINATE_1_DOT = "com.pinterest.ktlint:ktlint-cli:";

public static FormatterStep create(Provisioner provisioner) {
return create(defaultVersion(), provisioner);
}

public static FormatterStep create(String version, Provisioner provisioner) {
return create(version, provisioner, Collections.emptyMap());
}

public static FormatterStep create(String version, Provisioner provisioner,
Map<String, Object> editorConfigOverride) {
return create(version, provisioner, false, null, editorConfigOverride);
return create(version, provisioner, null, Collections.emptyMap());
}

public static FormatterStep create(String version,
Provisioner provisioner,
boolean isScript,
@Nullable FileSignature editorConfig,
Map<String, Object> editorConfigOverride) {
Objects.requireNonNull(version, "version");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(NAME,
() -> new State(version, provisioner, isScript, editorConfig, editorConfigOverride),
() -> new State(version, provisioner, editorConfig, editorConfigOverride),
State::createFormat);
}

Expand All @@ -72,35 +66,30 @@ public static String defaultVersion() {

static final class State implements Serializable {
private static final long serialVersionUID = 1L;

/** Are the files being linted Kotlin script files. */
private final boolean isScript;
/** The jar that contains the formatter. */
final JarState jarState;
private final JarState jarState;
private final TreeMap<String, Object> editorConfigOverride;
private final String version;
@Nullable
private final FileSignature editorConfigPath;

State(String version,
Provisioner provisioner,
boolean isScript,
@Nullable FileSignature editorConfigPath,
Map<String, Object> editorConfigOverride) throws IOException {
this.version = version;
this.editorConfigOverride = new TreeMap<>(editorConfigOverride);
this.jarState = JarState.from((version.startsWith("0.") ? MAVEN_COORDINATE_0_DOT : MAVEN_COORDINATE_1_DOT) + version,
provisioner);
this.editorConfigPath = editorConfigPath;
this.isScript = isScript;
}

FormatterFunc createFormat() throws Exception {
final ClassLoader classLoader = jarState.getClassLoader();
Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.ktlint.KtlintFormatterFunc");
Constructor<?> constructor = formatterFunc.getConstructor(
String.class, boolean.class, FileSignature.class, Map.class);
return (FormatterFunc.NeedsFile) constructor.newInstance(version, isScript, editorConfigPath, editorConfigOverride);
String.class, FileSignature.class, Map.class);
return (FormatterFunc.NeedsFile) constructor.newInstance(version, editorConfigPath, editorConfigOverride);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ public class KtLintCompat0Dot48Dot0AdapterTest {
@Test
public void testDefaults(@TempDir Path path) throws IOException {
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
String text = loadAndWriteText(path, "empty_class_body.kt");
loadAndWriteText(path, "empty_class_body.kt");
final Path filePath = Paths.get(path.toString(), "empty_class_body.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();

String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, filePath, false, null, editorConfigOverrideMap);
String formatted = ktLintCompat0Dot48Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
assertEquals("class empty_class_body\n", formatted);
}

@Test
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
String text = loadAndWriteText(path, "fails_no_semicolons.kt");
loadAndWriteText(path, "fails_no_semicolons.kt");
final Path filePath = Paths.get(path.toString(), "fails_no_semicolons.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();
Expand All @@ -54,7 +54,7 @@ public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
// ktlint_filename is an invalid rule in ktlint 0.48.0
editorConfigOverrideMap.put("ktlint_filename", "disabled");

String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, filePath, false, null, editorConfigOverrideMap);
String formatted = ktLintCompat0Dot48Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
assertEquals("class fails_no_semicolons {\n\tval i = 0;\n}\n", formatted);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ public class KtLintCompat0Dot49Dot0AdapterTest {
@Test
public void testDefaults(@TempDir Path path) throws IOException {
KtLintCompat0Dot49Dot0Adapter ktLintCompat0Dot49Dot0Adapter = new KtLintCompat0Dot49Dot0Adapter();
String text = loadAndWriteText(path, "EmptyClassBody.kt");
loadAndWriteText(path, "EmptyClassBody.kt");
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();

String formatted = ktLintCompat0Dot49Dot0Adapter.format(text, filePath, false, null, editorConfigOverrideMap);
String formatted = ktLintCompat0Dot49Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
assertEquals("class EmptyClassBody\n", formatted);
}

@Test
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
KtLintCompat0Dot49Dot0Adapter ktLintCompat0Dot49Dot0Adapter = new KtLintCompat0Dot49Dot0Adapter();
String text = loadAndWriteText(path, "FailsNoSemicolons.kt");
loadAndWriteText(path, "FailsNoSemicolons.kt");
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();
editorConfigOverrideMap.put("indent_style", "tab");
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");

String formatted = ktLintCompat0Dot49Dot0Adapter.format(text, filePath, false, null, editorConfigOverrideMap);
String formatted = ktLintCompat0Dot49Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ public class KtLintCompat0Dot50Dot0AdapterTest {
@Test
public void testDefaults(@TempDir Path path) throws IOException {
KtLintCompat0Dot50Dot0Adapter KtLintCompat0Dot50Dot0Adapter = new KtLintCompat0Dot50Dot0Adapter();
String text = loadAndWriteText(path, "EmptyClassBody.kt");
loadAndWriteText(path, "EmptyClassBody.kt");
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();

String formatted = KtLintCompat0Dot50Dot0Adapter.format(text, filePath, false, null, editorConfigOverrideMap);
String formatted = KtLintCompat0Dot50Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
assertEquals("class EmptyClassBody\n", formatted);
}

@Test
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
KtLintCompat0Dot50Dot0Adapter KtLintCompat0Dot50Dot0Adapter = new KtLintCompat0Dot50Dot0Adapter();
String text = loadAndWriteText(path, "FailsNoSemicolons.kt");
loadAndWriteText(path, "FailsNoSemicolons.kt");
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();
editorConfigOverrideMap.put("indent_style", "tab");
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");

String formatted = KtLintCompat0Dot50Dot0Adapter.format(text, filePath, false, null, editorConfigOverrideMap);
String formatted = KtLintCompat0Dot50Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ public class KtLintCompat1Dot0Dot0AdapterTest {
@Test
public void testDefaults(@TempDir Path path) throws IOException {
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter();
String text = loadAndWriteText(path, "EmptyClassBody.kt");
loadAndWriteText(path, "EmptyClassBody.kt");
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();

String formatted = KtLintCompat1Dot0Dot0Adapter.format(text, filePath, false, null, editorConfigOverrideMap);
String formatted = KtLintCompat1Dot0Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
assertEquals("class EmptyClassBody\n", formatted);
}

@Test
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter();
String text = loadAndWriteText(path, "FailsNoSemicolons.kt");
loadAndWriteText(path, "FailsNoSemicolons.kt");
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();
editorConfigOverrideMap.put("indent_style", "tab");
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");

String formatted = KtLintCompat1Dot0Dot0Adapter.format(text, filePath, false, null, editorConfigOverrideMap);
String formatted = KtLintCompat1Dot0Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ private FormatterStep createStep() {
return KtLintStep.create(
version,
provisioner(),
isScript(),
editorConfigPath,
editorConfigOverride);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public FormatterStep newFormatterStep(final FormatterStepConfig stepConfig) {
editorConfigOverride = new HashMap<>();
}

return KtLintStep.create(ktlintVersion, stepConfig.getProvisioner(), false, configPath, editorConfigOverride);
return KtLintStep.create(ktlintVersion, stepConfig.getProvisioner(), configPath, editorConfigOverride);
}
}

0 comments on commit d5ddbcf

Please sign in to comment.