Skip to content

Commit 8916121

Browse files
committed
The KtLint formatters were reading from the File instead of the String, which clobbers the work of earlier steps.
1 parent 755c22f commit 8916121

File tree

6 files changed

+39
-12
lines changed

6 files changed

+39
-12
lines changed

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

Lines changed: 3 additions & 2 deletions
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

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

18+
import java.lang.reflect.Field;
1819
import java.lang.reflect.InvocationTargetException;
1920
import java.lang.reflect.Method;
2021
import java.nio.file.Files;
@@ -121,9 +122,10 @@ public Unit invoke(LintError lint, Boolean corrected) {
121122

122123
@Override
123124
public String format(
125+
String unix,
124126
Path path,
125127
Path editorConfigPath,
126-
Map<String, Object> editorConfigOverrideMap) {
128+
Map<String, Object> editorConfigOverrideMap) throws NoSuchFieldException, IllegalAccessException {
127129
final FormatterCallback formatterCallback = new FormatterCallback();
128130

129131
Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader())
@@ -146,13 +148,19 @@ public String format(
146148
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders));
147149
}
148150

151+
// create Code and then set the content to match previous steps in the Spotless pipeline
152+
Code code = Code.Companion.fromPath(path);
153+
Field contentField = code.getClass().getDeclaredField("content");
154+
contentField.setAccessible(true);
155+
contentField.set(code, unix);
156+
149157
return new KtLintRuleEngine(
150158
allRuleProviders,
151159
editorConfig,
152160
editorConfigOverride,
153161
false,
154162
path.getFileSystem())
155-
.format(Code.Companion.fromPath(path), formatterCallback);
163+
.format(code, formatterCallback);
156164
}
157165

158166
/**

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

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

18+
import java.lang.reflect.Field;
1819
import java.nio.file.Files;
1920
import java.nio.file.Path;
2021
import java.util.List;
@@ -83,9 +84,10 @@ public Unit invoke(LintError lint, Boolean corrected) {
8384

8485
@Override
8586
public String format(
87+
String unix,
8688
Path path,
8789
Path editorConfigPath,
88-
Map<String, Object> editorConfigOverrideMap) {
90+
Map<String, Object> editorConfigOverrideMap) throws NoSuchFieldException, IllegalAccessException {
8991
final FormatterCallback formatterCallback = new FormatterCallback();
9092

9193
Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader())
@@ -108,14 +110,20 @@ public String format(
108110
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders));
109111
}
110112

113+
// create Code and then set the content to match previous steps in the Spotless pipeline
114+
Code code = Code.Companion.fromPath(path);
115+
Field contentField = code.getClass().getDeclaredField("content");
116+
contentField.setAccessible(true);
117+
contentField.set(code, unix);
118+
111119
return new KtLintRuleEngine(
112120
allRuleProviders,
113121
editorConfig,
114122
editorConfigOverride,
115123
true,
116124
false,
117125
path.getFileSystem())
118-
.format(Code.Companion.fromPath(path), formatterCallback);
126+
.format(code, formatterCallback);
119127
}
120128

121129
/**

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

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

18+
import java.lang.reflect.Field;
1819
import java.nio.file.Path;
1920
import java.util.List;
2021
import java.util.Map;
@@ -82,9 +83,10 @@ public Unit invoke(LintError lint, Boolean corrected) {
8283

8384
@Override
8485
public String format(
86+
String unix,
8587
Path path,
8688
Path editorConfigPath,
87-
Map<String, Object> editorConfigOverrideMap) {
89+
Map<String, Object> editorConfigOverrideMap) throws NoSuchFieldException, IllegalAccessException {
8890
final FormatterCallback formatterCallback = new FormatterCallback();
8991

9092
Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader())
@@ -103,13 +105,19 @@ public String format(
103105
editorConfigOverrideMap);
104106
}
105107

108+
// create Code and then set the content to match previous steps in the Spotless pipeline
109+
Code code = Code.Companion.fromPath(path);
110+
Field contentField = code.getClass().getDeclaredField("content");
111+
contentField.setAccessible(true);
112+
contentField.set(code, unix);
113+
106114
return new KtLintRuleEngine(
107115
allRuleProviders,
108116
editorConfig,
109117
editorConfigOverride,
110118
false,
111119
path.getFileSystem())
112-
.format(Code.Companion.fromPath(path), formatterCallback);
120+
.format(code, formatterCallback);
113121
}
114122

115123
/**

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

Lines changed: 3 additions & 2 deletions
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.
@@ -21,7 +21,8 @@
2121
public interface KtLintCompatAdapter {
2222

2323
String format(
24+
String content,
2425
Path path,
2526
Path editorConfigPath,
26-
Map<String, Object> editorConfigOverrideMap);
27+
Map<String, Object> editorConfigOverrideMap) throws NoSuchFieldException, IllegalAccessException;
2728
}

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

Lines changed: 3 additions & 2 deletions
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);

0 commit comments

Comments
 (0)