Skip to content

Commit 9df255e

Browse files
committed
Add editor config file option to maven and gradle
1 parent 86404f1 commit 9df255e

File tree

13 files changed

+74
-40
lines changed

13 files changed

+74
-40
lines changed

lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ public Unit invoke(LintError lint, Boolean corrected) {
4343
@Override
4444
public String format(final String text, final String name, final boolean isScript,
4545
final boolean useExperimental,
46-
final Map<String, String> userData,
46+
String editorConfigPath, final Map<String, String> userData,
4747
final Map<String, Object> editorConfigOverrideMap) {
4848
final FormatterCallback formatterCallback = new FormatterCallback();
4949

lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ public Unit invoke(LintError lint, Boolean corrected) {
4343
@Override
4444
public String format(final String text, final String name, final boolean isScript,
4545
final boolean useExperimental,
46-
final Map<String, String> userData,
46+
String editorConfigPath, final Map<String, String> userData,
4747
final Map<String, Object> editorConfigOverrideMap) {
4848
final FormatterCallback formatterCallback = new FormatterCallback();
4949

lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ public Unit invoke(LintError lint, Boolean corrected) {
4343
@Override
4444
public String format(final String text, final String name, final boolean isScript,
4545
final boolean useExperimental,
46-
final Map<String, String> userData,
46+
String editorConfigPath, final Map<String, String> userData,
4747
final Map<String, Object> editorConfigOverrideMap) {
4848
final FormatterCallback formatterCallback = new FormatterCallback();
4949

@@ -61,7 +61,7 @@ public String format(final String text, final String name, final boolean isScrip
6161
userData,
6262
formatterCallback,
6363
isScript,
64-
null,
64+
editorConfigPath,
6565
false));
6666
}
6767
}

lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@ public Unit invoke(LintError lint, Boolean corrected) {
5151
@Override
5252
public String format(final String text, final String name, final boolean isScript,
5353
final boolean useExperimental,
54-
final Map<String, String> userData,
54+
String editorConfigPath, final Map<String, String> userData,
5555
final Map<String, Object> editorConfigOverrideMap) {
5656
final FormatterCallback formatterCallback = new FormatterCallback();
5757

@@ -76,7 +76,7 @@ public String format(final String text, final String name, final boolean isScrip
7676
userData,
7777
formatterCallback,
7878
isScript,
79-
null,
79+
editorConfigPath,
8080
false,
8181
editorConfigOverride,
8282
false));

lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@ public Unit invoke(LintError lint, Boolean corrected) {
5151
@Override
5252
public String format(final String text, final String name, final boolean isScript,
5353
final boolean useExperimental,
54-
final Map<String, String> userData,
54+
String editorConfigPath, final Map<String, String> userData,
5555
final Map<String, Object> editorConfigOverrideMap) {
5656
final FormatterCallback formatterCallback = new FormatterCallback();
5757

@@ -76,7 +76,7 @@ public String format(final String text, final String name, final boolean isScrip
7676
userData,
7777
formatterCallback,
7878
isScript,
79-
null,
79+
editorConfigPath,
8080
false,
8181
editorConfigOverride,
8282
false));

lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717

1818
import static java.util.Collections.emptySet;
1919

20+
import java.io.File;
21+
import java.nio.file.Path;
2022
import java.util.ArrayList;
2123
import java.util.LinkedHashSet;
2224
import java.util.List;
@@ -56,7 +58,7 @@ public Unit invoke(LintError lint, Boolean corrected) {
5658
@Override
5759
public String format(final String text, final String name, final boolean isScript,
5860
final boolean useExperimental,
59-
final Map<String, String> userData,
61+
String editorConfigPath, final Map<String, String> userData,
6062
final Map<String, Object> editorConfigOverrideMap) {
6163
final FormatterCallback formatterCallback = new FormatterCallback();
6264

@@ -68,14 +70,19 @@ public String format(final String text, final String name, final boolean isScrip
6870

6971
EditorConfigOverride editorConfigOverride;
7072
if (editorConfigOverrideMap.isEmpty()) {
71-
editorConfigOverride = EditorConfigOverride.Companion.getEmptyEditorConfigOverride();
73+
editorConfigOverride = new EditorConfigOverride();
7274
} else {
7375
editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map(
7476
RuleProvider::createNewRuleInstance).collect(
7577
Collectors.toList()),
7678
editorConfigOverrideMap);
7779
}
78-
80+
Path editorConfigFilePath;
81+
if (editorConfigPath == null) {
82+
editorConfigFilePath = null;
83+
} else {
84+
editorConfigFilePath = new File(editorConfigPath).toPath();
85+
}
7986
return KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
8087
name,
8188
text,
@@ -86,7 +93,7 @@ public String format(final String text, final String name, final boolean isScrip
8693
isScript,
8794
null,
8895
false,
89-
EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults(),
96+
EditorConfigDefaults.Companion.load(editorConfigFilePath),
9097
editorConfigOverride,
9198
false));
9299
}

lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18+
import java.io.File;
19+
import java.nio.file.Path;
1820
import java.util.LinkedHashSet;
1921
import java.util.List;
2022
import java.util.Map;
@@ -54,7 +56,7 @@ public Unit invoke(LintError lint, Boolean corrected) {
5456
@Override
5557
public String format(final String text, final String name, final boolean isScript,
5658
final boolean useExperimental,
57-
final Map<String, String> userData,
59+
String editorConfigPath, final Map<String, String> userData,
5860
final Map<String, Object> editorConfigOverrideMap) {
5961
final FormatterCallback formatterCallback = new FormatterCallback();
6062

@@ -66,14 +68,19 @@ public String format(final String text, final String name, final boolean isScrip
6668

6769
EditorConfigOverride editorConfigOverride;
6870
if (editorConfigOverrideMap.isEmpty()) {
69-
editorConfigOverride = EditorConfigOverride.Companion.getEmptyEditorConfigOverride();
71+
editorConfigOverride = new EditorConfigOverride();
7072
} else {
7173
editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map(
7274
RuleProvider::createNewRuleInstance).collect(
7375
Collectors.toList()),
7476
editorConfigOverrideMap);
7577
}
76-
78+
Path editorConfigFilePath;
79+
if (editorConfigPath == null) {
80+
editorConfigFilePath = null;
81+
} else {
82+
editorConfigFilePath = new File(editorConfigPath).toPath();
83+
}
7784
return KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
7885
name,
7986
text,
@@ -82,7 +89,7 @@ public String format(final String text, final String name, final boolean isScrip
8289
formatterCallback,
8390
isScript,
8491
false,
85-
EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults(),
92+
EditorConfigDefaults.Companion.load(editorConfigFilePath),
8693
editorConfigOverride,
8794
false));
8895
}

lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,6 @@
1919

2020
public interface KtLintCompatAdapter {
2121

22-
String format(String text, String name, boolean isScript, boolean useExperimental, Map<String, String> userData,
22+
String format(String text, String name, boolean isScript, boolean useExperimental, String editorConfigPath, Map<String, String> userData,
2323
Map<String, Object> editorConfigOverrideMap);
2424
}

lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {
3737
@NotNull
3838
private final KtLintCompatAdapter adapter;
3939
private final boolean useExperimental;
40+
private final String editorConfigPath;
4041
private final Map<String, Object> editorConfigOverrideMap;
4142

42-
public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, Map<String, String> userData,
43+
public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, String editorConfigPath, Map<String, String> userData,
4344
Map<String, Object> editorConfigOverrideMap) {
4445
int minorVersion = Integer.parseInt(version.split("\\.")[1]);
4546
if (minorVersion >= 48) {
@@ -64,6 +65,7 @@ public KtlintFormatterFunc(String version, boolean isScript, boolean useExperime
6465
// the OG
6566
this.adapter = new KtLintCompat0Dot31Dot0Adapter();
6667
}
68+
this.editorConfigPath = editorConfigPath;
6769
this.useExperimental = useExperimental;
6870
this.editorConfigOverrideMap = editorConfigOverrideMap;
6971
this.userData = userData;
@@ -72,6 +74,6 @@ public KtlintFormatterFunc(String version, boolean isScript, boolean useExperime
7274

7375
@Override
7476
public String applyWithFile(String unix, File file) throws Exception {
75-
return adapter.format(unix, file.getName(), isScript, useExperimental, userData, editorConfigOverrideMap);
77+
return adapter.format(unix, file.getName(), isScript, useExperimental, editorConfigPath, userData, editorConfigOverrideMap);
7678
}
7779
}

lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,15 @@ public static FormatterStep createForScript(String version, Provisioner provisio
6464

6565
private static FormatterStep create(String version, Provisioner provisioner, boolean isScript, boolean useExperimental,
6666
Map<String, String> userData, Map<String, Object> editorConfigOverride) {
67+
return create(version, provisioner, useExperimental, userData, editorConfigOverride);
68+
}
69+
70+
public static FormatterStep create(String version, Provisioner provisioner, boolean isScript, boolean useExperimental,
71+
String editorConfig, Map<String, String> userData, Map<String, Object> editorConfigOverride) {
6772
Objects.requireNonNull(version, "version");
6873
Objects.requireNonNull(provisioner, "provisioner");
6974
return FormatterStep.createLazy(NAME,
70-
() -> new State(version, provisioner, isScript, useExperimental, userData, editorConfigOverride),
75+
() -> new State(version, provisioner, isScript, useExperimental, editorConfig, userData, editorConfigOverride),
7176
State::createFormat);
7277
}
7378

@@ -86,9 +91,10 @@ static final class State implements Serializable {
8691
private final TreeMap<String, String> userData;
8792
private final TreeMap<String, Object> editorConfigOverride;
8893
private final String version;
94+
private final String editorConfigPath;
8995

9096
State(String version, Provisioner provisioner, boolean isScript, boolean useExperimental,
91-
Map<String, String> userData, Map<String, Object> editorConfigOverride) throws IOException {
97+
String editorConfigPath, Map<String, String> userData, Map<String, Object> editorConfigOverride) throws IOException {
9298
this.version = version;
9399

94100
String coordinate;
@@ -104,15 +110,16 @@ static final class State implements Serializable {
104110
this.userData = new TreeMap<>(userData);
105111
this.editorConfigOverride = new TreeMap<>(editorConfigOverride);
106112
this.jarState = JarState.from(coordinate + version, provisioner);
113+
this.editorConfigPath = editorConfigPath;
107114
this.isScript = isScript;
108115
}
109116

110117
FormatterFunc createFormat() throws Exception {
111118
final ClassLoader classLoader = jarState.getClassLoader();
112119
Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.ktlint.KtlintFormatterFunc");
113120
Constructor<?> constructor = formatterFunc.getConstructor(
114-
String.class, boolean.class, boolean.class, Map.class, Map.class);
115-
return (FormatterFunc.NeedsFile) constructor.newInstance(version, isScript, useExperimental, userData, editorConfigOverride);
121+
String.class, boolean.class, boolean.class, String.class, Map.class, Map.class);
122+
return (FormatterFunc.NeedsFile) constructor.newInstance(version, isScript, useExperimental, editorConfigPath, userData, editorConfigOverride);
116123
}
117124
}
118125
}

0 commit comments

Comments
 (0)