Skip to content

Commit

Permalink
Remove unused userData param in Ktlint format step (#1891)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Nov 20, 2023
2 parents 2c2c4f7 + 7cb08d1 commit a0936e1
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ public Unit invoke(LintError lint, Boolean corrected) {
}

@Override
public String format(final String text, Path path, final boolean isScript,
Path editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
public String format(
String text,
Path path,
boolean isScript,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV2.class, RuleSetProviderV2.class.getClassLoader())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ public Unit invoke(LintError lint, Boolean corrected) {
}

@Override
public String format(final String text, Path path, final boolean isScript,
Path editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
public String format(
String text,
Path path,
boolean isScript,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ public Unit invoke(LintError lint, Boolean corrected) {
}

@Override
public String format(final String text, Path path, final boolean isScript,
Path editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
public String format(
String text,
Path path,
boolean isScript,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ public Unit invoke(LintError lint, Boolean corrected) {
}

@Override
public String format(final String text, Path path, final boolean isScript,
Path editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
public String format(
String text,
Path path,
boolean isScript,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

public interface KtLintCompatAdapter {

String format(String text, Path path, boolean isScript, Path editorConfigPath, Map<String, String> userData,
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 @@ -25,13 +25,12 @@

public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {

private final Map<String, String> userData;
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, Map<String, String> userData,
public KtlintFormatterFunc(String version, boolean isScript, FileSignature editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
String[] versions = version.split("\\.");
int majorVersion = Integer.parseInt(versions[0]);
Expand All @@ -58,7 +57,6 @@ public KtlintFormatterFunc(String version, boolean isScript, FileSignature edito
}
this.editorConfigPath = editorConfigPath;
this.editorConfigOverrideMap = editorConfigOverrideMap;
this.userData = userData;
this.isScript = isScript;
}

Expand All @@ -69,6 +67,6 @@ public String applyWithFile(String unix, File file) {
if (editorConfigPath != null) {
absoluteEditorConfigPath = editorConfigPath.getOnlyFile().toPath();
}
return adapter.format(unix, file.toPath(), isScript, absoluteEditorConfigPath, userData, editorConfigOverrideMap);
return adapter.format(unix, file.toPath(), isScript, absoluteEditorConfigPath, editorConfigOverrideMap);
}
}
16 changes: 6 additions & 10 deletions lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,23 @@ public static FormatterStep create(Provisioner provisioner) {
}

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

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

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

Expand All @@ -78,7 +77,6 @@ static final class State implements Serializable {
private final boolean isScript;
/** The jar that contains the formatter. */
final JarState jarState;
private final TreeMap<String, String> userData;
private final TreeMap<String, Object> editorConfigOverride;
private final String version;
@Nullable
Expand All @@ -88,10 +86,8 @@ static final class State implements Serializable {
Provisioner provisioner,
boolean isScript,
@Nullable FileSignature editorConfigPath,
Map<String, String> userData,
Map<String, Object> editorConfigOverride) throws IOException {
this.version = version;
this.userData = new TreeMap<>(userData);
this.editorConfigOverride = new TreeMap<>(editorConfigOverride);
this.jarState = JarState.from((version.startsWith("0.") ? MAVEN_COORDINATE_0_DOT : MAVEN_COORDINATE_1_DOT) + version,
provisioner);
Expand All @@ -103,8 +99,8 @@ 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, Map.class);
return (FormatterFunc.NeedsFile) constructor.newInstance(version, isScript, editorConfigPath, userData, editorConfigOverride);
String.class, boolean.class, FileSignature.class, Map.class);
return (FormatterFunc.NeedsFile) constructor.newInstance(version, isScript, editorConfigPath, editorConfigOverride);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ public void testDefaults(@TempDir Path path) throws IOException {
String text = loadAndWriteText(path, "empty_class_body.kt");
final Path filePath = Paths.get(path.toString(), "empty_class_body.kt");

Map<String, String> userData = new HashMap<>();

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

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

Expand All @@ -50,15 +48,13 @@ public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
String text = loadAndWriteText(path, "fails_no_semicolons.kt");
final Path filePath = Paths.get(path.toString(), "fails_no_semicolons.kt");

Map<String, String> userData = new HashMap<>();

Map<String, Object> editorConfigOverrideMap = new HashMap<>();
editorConfigOverrideMap.put("indent_style", "tab");
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");
// ktlint_filename is an invalid rule in ktlint 0.48.0
editorConfigOverrideMap.put("ktlint_filename", "disabled");

String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, filePath, false, null, userData, editorConfigOverrideMap);
String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, filePath, false, 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 @@ -36,11 +36,9 @@ public void testDefaults(@TempDir Path path) throws IOException {
String text = loadAndWriteText(path, "EmptyClassBody.kt");
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");

Map<String, String> userData = new HashMap<>();

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

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

Expand All @@ -50,13 +48,11 @@ public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
String text = loadAndWriteText(path, "FailsNoSemicolons.kt");
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");

Map<String, String> userData = new HashMap<>();

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, userData, editorConfigOverrideMap);
String formatted = ktLintCompat0Dot49Dot0Adapter.format(text, filePath, false, 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 @@ -36,11 +36,9 @@ public void testDefaults(@TempDir Path path) throws IOException {
String text = loadAndWriteText(path, "EmptyClassBody.kt");
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");

Map<String, String> userData = new HashMap<>();

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

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

Expand All @@ -50,13 +48,11 @@ public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
String text = loadAndWriteText(path, "FailsNoSemicolons.kt");
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");

Map<String, String> userData = new HashMap<>();

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, userData, editorConfigOverrideMap);
String formatted = KtLintCompat0Dot50Dot0Adapter.format(text, filePath, false, 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 @@ -36,11 +36,9 @@ public void testDefaults(@TempDir Path path) throws IOException {
String text = loadAndWriteText(path, "EmptyClassBody.kt");
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");

Map<String, String> userData = new HashMap<>();

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

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

Expand All @@ -50,13 +48,11 @@ public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
String text = loadAndWriteText(path, "FailsNoSemicolons.kt");
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");

Map<String, String> userData = new HashMap<>();

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, userData, editorConfigOverrideMap);
String formatted = KtLintCompat1Dot0Dot0Adapter.format(text, filePath, false, null, editorConfigOverrideMap);
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
}

Expand Down
3 changes: 1 addition & 2 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,8 @@ Additionally, `editorConfigOverride` options will override what's supplied in `.
```kotlin
spotless {
kotlin {
// version, userData and editorConfigOverride are all optional
// version, editorConfigPath and editorConfigOverride are all optional
ktlint("1.0.0")
.userData(mapOf("android" to "true"))
.setEditorConfigPath("$projectDir/config/.editorconfig") // sample unusual placement
.editorConfigOverride(
mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public KtlintConfig ktlint() throws IOException {

/** Adds the specified version of <a href="https://github.com/pinterest/ktlint">ktlint</a>. */
public KtlintConfig ktlint(String version) throws IOException {
return new KtlintConfig(version, Collections.emptyMap(), Collections.emptyMap());
return new KtlintConfig(version, Collections.emptyMap());
}

/** Uses the <a href="https://github.com/facebookincubator/ktfmt">ktfmt</a> jar to format source code. */
Expand Down Expand Up @@ -146,19 +146,16 @@ private void configure(Consumer<KtfmtStep.KtfmtFormattingOptions> optionsConfigu
public class KtlintConfig {
private final String version;
private FileSignature editorConfigPath;
private Map<String, String> userData;
private Map<String, Object> editorConfigOverride;

private KtlintConfig(
String version,
Map<String, String> userData,
Map<String, Object> editorConfigOverride) throws IOException {
Objects.requireNonNull(version);
File defaultEditorConfig = getProject().getRootProject().file(".editorconfig");
FileSignature editorConfigPath = defaultEditorConfig.exists() ? FileSignature.signAsList(defaultEditorConfig) : null;
this.version = version;
this.editorConfigPath = editorConfigPath;
this.userData = userData;
this.editorConfigOverride = editorConfigOverride;
addStep(createStep());
}
Expand All @@ -177,14 +174,6 @@ public KtlintConfig setEditorConfigPath(@Nullable Object editorConfigPath) throw
return this;
}

public KtlintConfig userData(Map<String, String> userData) {
// Copy the map to a sorted map because up-to-date checking is based on binary-equals of the serialized
// representation.
this.userData = ImmutableSortedMap.copyOf(userData);
replaceStep(createStep());
return this;
}

public KtlintConfig editorConfigOverride(Map<String, Object> editorConfigOverride) {
// Copy the map to a sorted map because up-to-date checking is based on binary-equals of the serialized
// representation.
Expand All @@ -199,7 +188,6 @@ private FormatterStep createStep() {
provisioner(),
isScript(),
editorConfigPath,
userData,
editorConfigOverride);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.diffplug.spotless.maven.kotlin;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -48,6 +47,6 @@ public FormatterStep newFormatterStep(final FormatterStepConfig stepConfig) {
editorConfigOverride = new HashMap<>();
}

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

0 comments on commit a0936e1

Please sign in to comment.