Skip to content

Commit 4ee53aa

Browse files
authored
Support .editorconfig for ktlint (#1442)
2 parents b2778cd + 6cb3ec5 commit 4ee53aa

File tree

19 files changed

+211
-86
lines changed

19 files changed

+211
-86
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1111

1212
## [Unreleased]
1313
### Added
14+
* Add option `editorConfigFile` for `ktLint` [#142](https://github.com/diffplug/spotless/issues/142)
1415
* Added `skipLinesMatching` option to `licenseHeader` to support formats where license header cannot be immediately added to the top of the file (e.g. xml, sh). ([#1441](https://github.com/diffplug/spotless/pull/1441)).
1516
### Fixed
1617
* Support `ktlint` 0.48+ new rule disabling syntax ([#1456](https://github.com/diffplug/spotless/pull/1456)) fixes ([#1444](https://github.com/diffplug/spotless/issues/1444))

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

+4-3
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.
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18+
import java.nio.file.Path;
1819
import java.util.ArrayList;
1920
import java.util.List;
2021
import java.util.Map;
@@ -41,9 +42,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
4142
}
4243

4344
@Override
44-
public String format(final String text, final String name, final boolean isScript,
45+
public String format(final String text, Path path, final boolean isScript,
4546
final boolean useExperimental,
46-
final Map<String, String> userData,
47+
Path editorConfigPath, final Map<String, String> userData,
4748
final Map<String, Object> editorConfigOverrideMap) {
4849
final FormatterCallback formatterCallback = new FormatterCallback();
4950

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

+4-3
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.
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18+
import java.nio.file.Path;
1819
import java.util.ArrayList;
1920
import java.util.List;
2021
import java.util.Map;
@@ -41,9 +42,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
4142
}
4243

4344
@Override
44-
public String format(final String text, final String name, final boolean isScript,
45+
public String format(final String text, Path path, final boolean isScript,
4546
final boolean useExperimental,
46-
final Map<String, String> userData,
47+
Path editorConfigPath, final Map<String, String> userData,
4748
final Map<String, Object> editorConfigOverrideMap) {
4849
final FormatterCallback formatterCallback = new FormatterCallback();
4950

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

+6-5
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.
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18+
import java.nio.file.Path;
1819
import java.util.ArrayList;
1920
import java.util.List;
2021
import java.util.Map;
@@ -41,9 +42,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
4142
}
4243

4344
@Override
44-
public String format(final String text, final String name, final boolean isScript,
45+
public String format(final String text, Path path, final boolean isScript,
4546
final boolean useExperimental,
46-
final Map<String, String> userData,
47+
Path editorConfigPath, final Map<String, String> userData,
4748
final Map<String, Object> editorConfigOverrideMap) {
4849
final FormatterCallback formatterCallback = new FormatterCallback();
4950

@@ -55,13 +56,13 @@ public String format(final String text, final String name, final boolean isScrip
5556
}
5657

5758
return KtLint.INSTANCE.format(new KtLint.Params(
58-
name,
59+
path.toFile().getAbsolutePath(),
5960
text,
6061
rulesets,
6162
userData,
6263
formatterCallback,
6364
isScript,
64-
null,
65+
editorConfigPath.toFile().getAbsolutePath(),
6566
false));
6667
}
6768
}

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

+6-5
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.
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18+
import java.nio.file.Path;
1819
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.List;
@@ -49,9 +50,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
4950
}
5051

5152
@Override
52-
public String format(final String text, final String name, final boolean isScript,
53+
public String format(final String text, Path path, final boolean isScript,
5354
final boolean useExperimental,
54-
final Map<String, String> userData,
55+
Path editorConfigPath, final Map<String, String> userData,
5556
final Map<String, Object> editorConfigOverrideMap) {
5657
final FormatterCallback formatterCallback = new FormatterCallback();
5758

@@ -70,13 +71,13 @@ public String format(final String text, final String name, final boolean isScrip
7071
}
7172

7273
return KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
73-
name,
74+
path.toFile().getAbsolutePath(),
7475
text,
7576
rulesets,
7677
userData,
7778
formatterCallback,
7879
isScript,
79-
null,
80+
editorConfigPath.toFile().getAbsolutePath(),
8081
false,
8182
editorConfigOverride,
8283
false));

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

+6-5
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.
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18+
import java.nio.file.Path;
1819
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.List;
@@ -49,9 +50,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
4950
}
5051

5152
@Override
52-
public String format(final String text, final String name, final boolean isScript,
53+
public String format(final String text, Path path, final boolean isScript,
5354
final boolean useExperimental,
54-
final Map<String, String> userData,
55+
Path editorConfigPath, final Map<String, String> userData,
5556
final Map<String, Object> editorConfigOverrideMap) {
5657
final FormatterCallback formatterCallback = new FormatterCallback();
5758

@@ -70,13 +71,13 @@ public String format(final String text, final String name, final boolean isScrip
7071
}
7172

7273
return KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
73-
name,
74+
path.toFile().getAbsolutePath(),
7475
text,
7576
rulesets,
7677
userData,
7778
formatterCallback,
7879
isScript,
79-
null,
80+
editorConfigPath.toFile().getAbsolutePath(),
8081
false,
8182
editorConfigOverride,
8283
false));

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

+15-6
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.nio.file.Files;
21+
import java.nio.file.Path;
2022
import java.util.ArrayList;
2123
import java.util.LinkedHashSet;
2224
import java.util.List;
@@ -54,9 +56,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
5456
}
5557

5658
@Override
57-
public String format(final String text, final String name, final boolean isScript,
59+
public String format(final String text, Path path, final boolean isScript,
5860
final boolean useExperimental,
59-
final Map<String, String> userData,
61+
Path editorConfigPath, final Map<String, String> userData,
6062
final Map<String, Object> editorConfigOverrideMap) {
6163
final FormatterCallback formatterCallback = new FormatterCallback();
6264

@@ -68,16 +70,23 @@ 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
}
7880

81+
EditorConfigDefaults editorConfig;
82+
if (editorConfigPath == null || !Files.exists(editorConfigPath)) {
83+
editorConfig = EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults();
84+
} else {
85+
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath);
86+
}
87+
7988
return KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
80-
name,
89+
path.toFile().getAbsolutePath(),
8190
text,
8291
emptySet(),
8392
allRuleProviders,
@@ -86,7 +95,7 @@ public String format(final String text, final String name, final boolean isScrip
8695
isScript,
8796
null,
8897
false,
89-
EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults(),
98+
editorConfig,
9099
editorConfigOverride,
91100
false));
92101
}

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

+15-12
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.nio.file.Files;
19+
import java.nio.file.Path;
1820
import java.util.ArrayList;
1921
import java.util.Collections;
2022
import java.util.LinkedHashSet;
@@ -25,7 +27,7 @@
2527
import java.util.stream.Collectors;
2628
import java.util.stream.Stream;
2729

28-
import com.pinterest.ktlint.core.KtLint;
30+
import com.pinterest.ktlint.core.KtLintRuleEngine;
2931
import com.pinterest.ktlint.core.LintError;
3032
import com.pinterest.ktlint.core.Rule;
3133
import com.pinterest.ktlint.core.RuleProvider;
@@ -76,9 +78,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
7678
}
7779

7880
@Override
79-
public String format(final String text, final String name, final boolean isScript,
81+
public String format(final String text, Path path, final boolean isScript,
8082
final boolean useExperimental,
81-
final Map<String, String> userData,
83+
Path editorConfigPath, final Map<String, String> userData,
8284
final Map<String, Object> editorConfigOverrideMap) {
8385
final FormatterCallback formatterCallback = new FormatterCallback();
8486

@@ -97,18 +99,19 @@ public String format(final String text, final String name, final boolean isScrip
9799
Collectors.toList()),
98100
editorConfigOverrideMap);
99101
}
102+
EditorConfigDefaults editorConfig;
103+
if (editorConfigPath == null || !Files.exists(editorConfigPath)) {
104+
editorConfig = EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS();
105+
} else {
106+
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath);
107+
}
100108

101-
return KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
102-
name,
103-
text,
109+
return new KtLintRuleEngine(
104110
allRuleProviders,
105-
userData,
106-
formatterCallback,
107-
isScript,
108-
false,
109-
EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS(),
111+
editorConfig,
110112
editorConfigOverride,
111-
false));
113+
false)
114+
.format(path, formatterCallback);
112115
}
113116

114117
/**

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

+3-2
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.
@@ -15,10 +15,11 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18+
import java.nio.file.Path;
1819
import java.util.Map;
1920

2021
public interface KtLintCompatAdapter {
2122

22-
String format(String text, String name, boolean isScript, boolean useExperimental, Map<String, String> userData,
23+
String format(String text, Path path, boolean isScript, boolean useExperimental, Path editorConfigPath, Map<String, String> userData,
2324
Map<String, Object> editorConfigOverrideMap);
2425
}

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,14 @@
1616
package com.diffplug.spotless.glue.ktlint;
1717

1818
import java.io.File;
19+
import java.nio.file.Path;
1920
import java.util.Map;
2021

2122
import org.jetbrains.annotations.NotNull;
2223

24+
import com.diffplug.spotless.FileSignature;
2325
import com.diffplug.spotless.FormatterFunc;
24-
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot31Dot0Adapter;
25-
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot32Dot0Adapter;
26-
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot34Dot2Adapter;
27-
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot45Dot2Adapter;
28-
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot46Dot0Adapter;
29-
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot47Dot0Adapter;
30-
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot48Dot0Adapter;
31-
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompatAdapter;
26+
import com.diffplug.spotless.glue.ktlint.compat.*;
3227

3328
public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {
3429

@@ -37,9 +32,10 @@ public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {
3732
@NotNull
3833
private final KtLintCompatAdapter adapter;
3934
private final boolean useExperimental;
35+
private final FileSignature editorConfigPath;
4036
private final Map<String, Object> editorConfigOverrideMap;
4137

42-
public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, Map<String, String> userData,
38+
public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, FileSignature editorConfigPath, Map<String, String> userData,
4339
Map<String, Object> editorConfigOverrideMap) {
4440
int minorVersion = Integer.parseInt(version.split("\\.")[1]);
4541
if (minorVersion >= 48) {
@@ -64,14 +60,20 @@ public KtlintFormatterFunc(String version, boolean isScript, boolean useExperime
6460
// the OG
6561
this.adapter = new KtLintCompat0Dot31Dot0Adapter();
6662
}
63+
this.editorConfigPath = editorConfigPath;
6764
this.useExperimental = useExperimental;
6865
this.editorConfigOverrideMap = editorConfigOverrideMap;
6966
this.userData = userData;
7067
this.isScript = isScript;
7168
}
7269

7370
@Override
74-
public String applyWithFile(String unix, File file) throws Exception {
75-
return adapter.format(unix, file.getName(), isScript, useExperimental, userData, editorConfigOverrideMap);
71+
public String applyWithFile(String unix, File file) {
72+
73+
Path absoluteEditorConfigPath = null;
74+
if (editorConfigPath != null) {
75+
absoluteEditorConfigPath = editorConfigPath.getOnlyFile().toPath();
76+
}
77+
return adapter.format(unix, file.toPath(), isScript, useExperimental, absoluteEditorConfigPath, userData, editorConfigOverrideMap);
7678
}
7779
}

0 commit comments

Comments
 (0)