Skip to content

Commit 4d35c32

Browse files
authored
Make ktlint formatters read the string content (#2302)
2 parents 755c22f + a8ef331 commit 4d35c32

File tree

13 files changed

+67
-29
lines changed

13 files changed

+67
-29
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Fixed
14+
* `ktlint` steps now read from the `string` instead of the `file` so they don't clobber earlier steps. (fixes [#1599](https://github.com/diffplug/spotless/issues/1599))
1315

1416
## [3.0.0.BETA3] - 2024-10-15
1517
### Added

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 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.
@@ -74,6 +74,7 @@ public Unit invoke(LintError lint, Boolean corrected) {
7474

7575
@Override
7676
public String format(
77+
String unix,
7778
Path path,
7879
Path editorConfigPath,
7980
Map<String, Object> editorConfigOverrideMap) {
@@ -105,7 +106,7 @@ public String format(
105106
editorConfig,
106107
editorConfigOverride,
107108
false)
108-
.format(path, formatterCallback);
109+
.format(unix, path, formatterCallback);
109110
}
110111

111112
/**

lib/src/compatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0Adapter.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public Unit invoke(LintError lint, Boolean corrected) {
121121

122122
@Override
123123
public String format(
124+
String unix,
124125
Path path,
125126
Path editorConfigPath,
126127
Map<String, Object> editorConfigOverrideMap) {
@@ -146,13 +147,16 @@ public String format(
146147
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders));
147148
}
148149

150+
// create Code and then set the content to match previous steps in the Spotless pipeline
151+
Code code = Code.Companion.fromPath(path);
152+
KtLintCompatAdapter.setCodeContent(code, unix);
149153
return new KtLintRuleEngine(
150154
allRuleProviders,
151155
editorConfig,
152156
editorConfigOverride,
153157
false,
154158
path.getFileSystem())
155-
.format(Code.Companion.fromPath(path), formatterCallback);
159+
.format(code, formatterCallback);
156160
}
157161

158162
/**

lib/src/compatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0Adapter.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public Unit invoke(LintError lint, Boolean corrected) {
8383

8484
@Override
8585
public String format(
86+
String unix,
8687
Path path,
8788
Path editorConfigPath,
8889
Map<String, Object> editorConfigOverrideMap) {
@@ -108,14 +109,17 @@ public String format(
108109
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders));
109110
}
110111

112+
// create Code and then set the content to match previous steps in the Spotless pipeline
113+
Code code = Code.Companion.fromPath(path);
114+
KtLintCompatAdapter.setCodeContent(code, unix);
111115
return new KtLintRuleEngine(
112116
allRuleProviders,
113117
editorConfig,
114118
editorConfigOverride,
115119
true,
116120
false,
117121
path.getFileSystem())
118-
.format(Code.Companion.fromPath(path), formatterCallback);
122+
.format(code, formatterCallback);
119123
}
120124

121125
/**

lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public Unit invoke(LintError lint, Boolean corrected) {
8282

8383
@Override
8484
public String format(
85+
String unix,
8586
Path path,
8687
Path editorConfigPath,
8788
Map<String, Object> editorConfigOverrideMap) {
@@ -103,13 +104,16 @@ public String format(
103104
editorConfigOverrideMap);
104105
}
105106

107+
// create Code and then set the content to match previous steps in the Spotless pipeline
108+
Code code = Code.Companion.fromPath(path);
109+
KtLintCompatAdapter.setCodeContent(code, unix);
106110
return new KtLintRuleEngine(
107111
allRuleProviders,
108112
editorConfig,
109113
editorConfigOverride,
110114
false,
111115
path.getFileSystem())
112-
.format(Code.Companion.fromPath(path), formatterCallback);
116+
.format(code, formatterCallback);
113117
}
114118

115119
/**

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 DiffPlug
2+
* Copyright 2022-2024 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,13 +15,31 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18+
import java.lang.reflect.Field;
1819
import java.nio.file.Path;
20+
import java.security.AccessController;
21+
import java.security.PrivilegedAction;
1922
import java.util.Map;
2023

2124
public interface KtLintCompatAdapter {
2225

2326
String format(
27+
String content,
2428
Path path,
2529
Path editorConfigPath,
26-
Map<String, Object> editorConfigOverrideMap);
30+
Map<String, Object> editorConfigOverrideMap) throws NoSuchFieldException, IllegalAccessException;
31+
32+
static void setCodeContent(Object code, String content) {
33+
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
34+
try {
35+
Field contentField = code.getClass().getDeclaredField("content");
36+
contentField.setAccessible(true);
37+
contentField.set(code, content);
38+
} catch (NoSuchFieldException | IllegalAccessException e) {
39+
// Handle exceptions as needed
40+
throw new RuntimeException("Failed to set content field", e);
41+
}
42+
return null;
43+
});
44+
}
2745
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 DiffPlug
2+
* Copyright 2021-2024 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.
@@ -60,12 +60,13 @@ public KtlintFormatterFunc(
6060
}
6161

6262
@Override
63-
public String applyWithFile(String unix, File file) {
63+
public String applyWithFile(String unix, File file) throws NoSuchFieldException, IllegalAccessException {
6464
Path absoluteEditorConfigPath = null;
6565
if (editorConfigPath != null) {
6666
absoluteEditorConfigPath = editorConfigPath.getOnlyFile().toPath();
6767
}
6868
return adapter.format(
69+
unix,
6970
file.toPath(),
7071
absoluteEditorConfigPath,
7172
editorConfigOverrideMap);

lib/src/testCompatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0AdapterTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 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.
@@ -33,19 +33,19 @@ public class KtLintCompat0Dot48Dot0AdapterTest {
3333
@Test
3434
public void testDefaults(@TempDir Path path) throws IOException {
3535
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
36-
loadAndWriteText(path, "empty_class_body.kt");
36+
var content = loadAndWriteText(path, "empty_class_body.kt");
3737
final Path filePath = Paths.get(path.toString(), "empty_class_body.kt");
3838

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

41-
String formatted = ktLintCompat0Dot48Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
41+
String formatted = ktLintCompat0Dot48Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
4242
assertEquals("class empty_class_body\n", formatted);
4343
}
4444

4545
@Test
4646
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
4747
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
48-
loadAndWriteText(path, "fails_no_semicolons.kt");
48+
var content = loadAndWriteText(path, "fails_no_semicolons.kt");
4949
final Path filePath = Paths.get(path.toString(), "fails_no_semicolons.kt");
5050

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

57-
String formatted = ktLintCompat0Dot48Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
57+
String formatted = ktLintCompat0Dot48Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
5858
assertEquals("class fails_no_semicolons {\n\tval i = 0;\n}\n", formatted);
5959
}
6060

lib/src/testCompatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0AdapterTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 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.
@@ -33,26 +33,26 @@ public class KtLintCompat0Dot49Dot0AdapterTest {
3333
@Test
3434
public void testDefaults(@TempDir Path path) throws IOException {
3535
KtLintCompat0Dot49Dot0Adapter ktLintCompat0Dot49Dot0Adapter = new KtLintCompat0Dot49Dot0Adapter();
36-
loadAndWriteText(path, "EmptyClassBody.kt");
36+
var content = loadAndWriteText(path, "EmptyClassBody.kt");
3737
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");
3838

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

41-
String formatted = ktLintCompat0Dot49Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
41+
String formatted = ktLintCompat0Dot49Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
4242
assertEquals("class EmptyClassBody\n", formatted);
4343
}
4444

4545
@Test
4646
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
4747
KtLintCompat0Dot49Dot0Adapter ktLintCompat0Dot49Dot0Adapter = new KtLintCompat0Dot49Dot0Adapter();
48-
loadAndWriteText(path, "FailsNoSemicolons.kt");
48+
var content = loadAndWriteText(path, "FailsNoSemicolons.kt");
4949
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");
5050

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

55-
String formatted = ktLintCompat0Dot49Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
55+
String formatted = ktLintCompat0Dot49Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
5656
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
5757
}
5858

lib/src/testCompatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0AdapterTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 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.
@@ -33,26 +33,26 @@ public class KtLintCompat0Dot50Dot0AdapterTest {
3333
@Test
3434
public void testDefaults(@TempDir Path path) throws IOException {
3535
KtLintCompat0Dot50Dot0Adapter KtLintCompat0Dot50Dot0Adapter = new KtLintCompat0Dot50Dot0Adapter();
36-
loadAndWriteText(path, "EmptyClassBody.kt");
36+
var content = loadAndWriteText(path, "EmptyClassBody.kt");
3737
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");
3838

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

41-
String formatted = KtLintCompat0Dot50Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
41+
String formatted = KtLintCompat0Dot50Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
4242
assertEquals("class EmptyClassBody\n", formatted);
4343
}
4444

4545
@Test
4646
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
4747
KtLintCompat0Dot50Dot0Adapter KtLintCompat0Dot50Dot0Adapter = new KtLintCompat0Dot50Dot0Adapter();
48-
loadAndWriteText(path, "FailsNoSemicolons.kt");
48+
var content = loadAndWriteText(path, "FailsNoSemicolons.kt");
4949
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");
5050

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

55-
String formatted = KtLintCompat0Dot50Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
55+
String formatted = KtLintCompat0Dot50Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
5656
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
5757
}
5858

lib/src/testCompatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0AdapterTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 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.
@@ -33,26 +33,26 @@ public class KtLintCompat1Dot0Dot0AdapterTest {
3333
@Test
3434
public void testDefaults(@TempDir Path path) throws IOException {
3535
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter();
36-
loadAndWriteText(path, "EmptyClassBody.kt");
36+
var content = loadAndWriteText(path, "EmptyClassBody.kt");
3737
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");
3838

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

41-
String formatted = KtLintCompat1Dot0Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
41+
String formatted = KtLintCompat1Dot0Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
4242
assertEquals("class EmptyClassBody\n", formatted);
4343
}
4444

4545
@Test
4646
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
4747
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter();
48-
loadAndWriteText(path, "FailsNoSemicolons.kt");
48+
var content = loadAndWriteText(path, "FailsNoSemicolons.kt");
4949
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");
5050

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

55-
String formatted = KtLintCompat1Dot0Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
55+
String formatted = KtLintCompat1Dot0Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
5656
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
5757
}
5858

plugin-gradle/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* `ktlint` steps now read from the `string` instead of the `file` so they don't clobber earlier steps. (fixes [#1599](https://github.com/diffplug/spotless/issues/1599))
68

79
## [7.0.0.BETA3] - 2024-10-15
810
### Added

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* `ktlint` steps now read from the `string` instead of the `file` so they don't clobber earlier steps. (fixes [#1599](https://github.com/diffplug/spotless/issues/1599))
68

79
## [2.44.0.BETA3] - 2024-10-15
810
### Added

0 commit comments

Comments
 (0)