Skip to content

Commit 676a069

Browse files
authored
Instantiate the JDT code formatter without any detours (#1864 fixes #1638)
2 parents fdad2ba + b08741a commit 676a069

File tree

7 files changed

+450
-35
lines changed

7 files changed

+450
-35
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+
* Fix Eclipse JDT on some settings files. ([#1864](https://github.com/diffplug/spotless/pull/1864) fixes [#1638](https://github.com/diffplug/spotless/issues/1638))
1315
### Changes
1416
* Bump default `ktlint` version to latest `1.0.0` -> `1.0.1`. ([#1855](https://github.com/diffplug/spotless/pull/1855))
1517

lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtFormatterStepImpl.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
package com.diffplug.spotless.extra.glue.jdt;
1717

1818
import java.io.File;
19+
import java.util.HashMap;
20+
import java.util.Map;
1921
import java.util.Properties;
22+
import java.util.stream.Collectors;
2023

21-
import org.eclipse.jdt.core.ToolFactory;
2224
import org.eclipse.jdt.core.formatter.CodeFormatter;
2325
import org.eclipse.jdt.internal.compiler.env.IModule;
26+
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
2427
import org.eclipse.jface.text.Document;
2528
import org.eclipse.jface.text.IDocument;
2629
import org.eclipse.text.edits.TextEdit;
@@ -33,7 +36,12 @@ public class EclipseJdtFormatterStepImpl {
3336
private final CodeFormatter codeFormatter;
3437

3538
public EclipseJdtFormatterStepImpl(Properties settings) {
36-
this.codeFormatter = ToolFactory.createCodeFormatter(settings, ToolFactory.M_FORMAT_EXISTING);
39+
Map<String, String> options = settings.entrySet().stream().collect(Collectors.toMap(
40+
e -> String.valueOf(e.getKey()),
41+
e -> String.valueOf(e.getValue()),
42+
(prev, next) -> next,
43+
HashMap::new));
44+
this.codeFormatter = new DefaultCodeFormatter(options);
3745
}
3846

3947
/** Formatting Java string, distinguishing module-info and compilation unit by file name */

lib-extra/src/test/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStepSpecialCaseTest.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,24 @@
1515
*/
1616
package com.diffplug.spotless.extra.java;
1717

18+
import java.io.File;
19+
import java.util.List;
20+
1821
import org.junit.jupiter.api.Test;
1922

2023
import com.diffplug.spotless.StepHarness;
2124
import com.diffplug.spotless.TestProvisioner;
25+
import com.diffplug.spotless.extra.EquoBasedStepBuilder;
2226

2327
public class EclipseJdtFormatterStepSpecialCaseTest {
2428
/** https://github.com/diffplug/spotless/issues/1638 */
2529
@Test
2630
public void issue_1638() {
27-
StepHarness.forStep(EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral()).build())
31+
ClassLoader classLoader = getClass().getClassLoader();
32+
File file = new File(classLoader.getResource("eclipse_formatter_issue_1638.xml").getFile());
33+
EquoBasedStepBuilder builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral());
34+
builder.setPreferences(List.of(file));
35+
StepHarness.forStep(builder.build())
2836
.testResource("java/eclipse/AbstractType.test", "java/eclipse/AbstractType.clean");
2937
}
3038
}

lib-extra/src/test/resources/eclipse_formatter_issue_1638.xml

+391
Large diffs are not rendered by default.

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+
* Fix Eclipse JDT on some settings files. ([#1864](https://github.com/diffplug/spotless/pull/1864) fixes [#1638](https://github.com/diffplug/spotless/issues/1638))
68
### Changes
79
* Bump default `ktlint` version to latest `1.0.0` -> `1.0.1`. ([#1855](https://github.com/diffplug/spotless/pull/1855))
810
### Fixed

plugin-maven/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
77
* CompileSourceRoots and TestCompileSourceRoots are now respected as default includes. These properties are commonly set when adding extra source directories. ([#1846](https://github.com/diffplug/spotless/issues/1846))
88
### Fixed
99
* Fix crash when build dir is a softlink to another directory. ([#1859](https://github.com/diffplug/spotless/pull/1859))
10+
* Fix Eclipse JDT on some settings files. ([#1864](https://github.com/diffplug/spotless/pull/1864) fixes [#1638](https://github.com/diffplug/spotless/issues/1638))
1011
### Changes
1112
* Bump default `ktlint` version to latest `1.0.0` -> `1.0.1`. ([#1855](https://github.com/diffplug/spotless/pull/1855))
1213

testlib/src/main/resources/java/eclipse/AbstractType.clean

+35-32
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,40 @@ package test;
22

33
public abstract class AbstractType {
44

5-
private String _typeName;
6-
7-
AbstractType(String typeName) {
8-
_typeName = typeName;
9-
}
10-
11-
private String _type() {
12-
String name = getClass().getSimpleName();
13-
return name.endsWith("Type") ? name.substring(0, getClass().getSimpleName().length() - 4) : name;
14-
}
15-
16-
AbstractType argument() {
17-
throw new UnsupportedOperationException(getClass().getSimpleName());
18-
}
19-
20-
@Override
21-
public boolean equals(Object another) {
22-
if (this == another) {
23-
return true;
24-
}
25-
return another instanceof AbstractType t && _typeName.equals(t._typeName);
26-
}
27-
28-
@Override
29-
public int hashCode() {
30-
return _typeName.hashCode();
31-
}
32-
33-
@Override
34-
public String toString() {
35-
return getClass().getSimpleName() + "(typeName)";
36-
}
5+
private String _typeName;
6+
7+
AbstractType(String typeName) {
8+
_typeName = typeName;
9+
}
10+
11+
private String _type() {
12+
String name = getClass().getSimpleName();
13+
return name.endsWith("Type")
14+
? name.substring(0, getClass().getSimpleName().length() - 4)
15+
: name;
16+
}
17+
18+
AbstractType argument() {
19+
throw new UnsupportedOperationException(getClass().getSimpleName());
20+
}
21+
22+
@Override
23+
public boolean equals(Object another) {
24+
if (this == another) {
25+
return true;
26+
}
27+
return another instanceof AbstractType t
28+
&& _typeName.equals(t._typeName);
29+
}
30+
31+
@Override
32+
public int hashCode() {
33+
return _typeName.hashCode();
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return getClass().getSimpleName() + "(typeName)";
39+
}
3740

3841
}

0 commit comments

Comments
 (0)