diff --git a/CHANGES.md b/CHANGES.md index 6e6da03181..93e693ebf5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* Fix Eclipse JDT on some settings files. ([#1864](https://github.com/diffplug/spotless/pull/1864) fixes [#1638](https://github.com/diffplug/spotless/issues/1638)) ### Changes * Bump default `ktlint` version to latest `1.0.0` -> `1.0.1`. ([#1855](https://github.com/diffplug/spotless/pull/1855)) diff --git a/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtFormatterStepImpl.java b/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtFormatterStepImpl.java index 40c02d8f3a..b394b6278d 100644 --- a/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtFormatterStepImpl.java +++ b/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtFormatterStepImpl.java @@ -16,11 +16,14 @@ package com.diffplug.spotless.extra.glue.jdt; import java.io.File; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; +import java.util.stream.Collectors; -import org.eclipse.jdt.core.ToolFactory; import org.eclipse.jdt.core.formatter.CodeFormatter; import org.eclipse.jdt.internal.compiler.env.IModule; +import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import org.eclipse.text.edits.TextEdit; @@ -33,7 +36,12 @@ public class EclipseJdtFormatterStepImpl { private final CodeFormatter codeFormatter; public EclipseJdtFormatterStepImpl(Properties settings) { - this.codeFormatter = ToolFactory.createCodeFormatter(settings, ToolFactory.M_FORMAT_EXISTING); + Map options = settings.entrySet().stream().collect(Collectors.toMap( + e -> String.valueOf(e.getKey()), + e -> String.valueOf(e.getValue()), + (prev, next) -> next, + HashMap::new)); + this.codeFormatter = new DefaultCodeFormatter(options); } /** Formatting Java string, distinguishing module-info and compilation unit by file name */ diff --git a/lib-extra/src/test/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStepSpecialCaseTest.java b/lib-extra/src/test/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStepSpecialCaseTest.java index eb93cf1f09..21ec889ed6 100644 --- a/lib-extra/src/test/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStepSpecialCaseTest.java +++ b/lib-extra/src/test/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStepSpecialCaseTest.java @@ -15,16 +15,24 @@ */ package com.diffplug.spotless.extra.java; +import java.io.File; +import java.util.List; + import org.junit.jupiter.api.Test; import com.diffplug.spotless.StepHarness; import com.diffplug.spotless.TestProvisioner; +import com.diffplug.spotless.extra.EquoBasedStepBuilder; public class EclipseJdtFormatterStepSpecialCaseTest { /** https://github.com/diffplug/spotless/issues/1638 */ @Test public void issue_1638() { - StepHarness.forStep(EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral()).build()) + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("eclipse_formatter_issue_1638.xml").getFile()); + EquoBasedStepBuilder builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral()); + builder.setPreferences(List.of(file)); + StepHarness.forStep(builder.build()) .testResource("java/eclipse/AbstractType.test", "java/eclipse/AbstractType.clean"); } } diff --git a/lib-extra/src/test/resources/eclipse_formatter_issue_1638.xml b/lib-extra/src/test/resources/eclipse_formatter_issue_1638.xml new file mode 100644 index 0000000000..583df63026 --- /dev/null +++ b/lib-extra/src/test/resources/eclipse_formatter_issue_1638.xml @@ -0,0 +1,391 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 2d9788576a..ec26cf5c63 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Fixed +* Fix Eclipse JDT on some settings files. ([#1864](https://github.com/diffplug/spotless/pull/1864) fixes [#1638](https://github.com/diffplug/spotless/issues/1638)) ### Changes * Bump default `ktlint` version to latest `1.0.0` -> `1.0.1`. ([#1855](https://github.com/diffplug/spotless/pull/1855)) ### Fixed diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index e07eadad4c..e941297aa3 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * 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)) ### Fixed * Fix crash when build dir is a softlink to another directory. ([#1859](https://github.com/diffplug/spotless/pull/1859)) +* Fix Eclipse JDT on some settings files. ([#1864](https://github.com/diffplug/spotless/pull/1864) fixes [#1638](https://github.com/diffplug/spotless/issues/1638)) ### Changes * Bump default `ktlint` version to latest `1.0.0` -> `1.0.1`. ([#1855](https://github.com/diffplug/spotless/pull/1855)) diff --git a/testlib/src/main/resources/java/eclipse/AbstractType.clean b/testlib/src/main/resources/java/eclipse/AbstractType.clean index 475b1186fd..314dbac1ac 100644 --- a/testlib/src/main/resources/java/eclipse/AbstractType.clean +++ b/testlib/src/main/resources/java/eclipse/AbstractType.clean @@ -2,37 +2,40 @@ package test; public abstract class AbstractType { - private String _typeName; - - AbstractType(String typeName) { - _typeName = typeName; - } - - private String _type() { - String name = getClass().getSimpleName(); - return name.endsWith("Type") ? name.substring(0, getClass().getSimpleName().length() - 4) : name; - } - - AbstractType argument() { - throw new UnsupportedOperationException(getClass().getSimpleName()); - } - - @Override - public boolean equals(Object another) { - if (this == another) { - return true; - } - return another instanceof AbstractType t && _typeName.equals(t._typeName); - } - - @Override - public int hashCode() { - return _typeName.hashCode(); - } - - @Override - public String toString() { - return getClass().getSimpleName() + "(typeName)"; - } + private String _typeName; + + AbstractType(String typeName) { + _typeName = typeName; + } + + private String _type() { + String name = getClass().getSimpleName(); + return name.endsWith("Type") + ? name.substring(0, getClass().getSimpleName().length() - 4) + : name; + } + + AbstractType argument() { + throw new UnsupportedOperationException(getClass().getSimpleName()); + } + + @Override + public boolean equals(Object another) { + if (this == another) { + return true; + } + return another instanceof AbstractType t + && _typeName.equals(t._typeName); + } + + @Override + public int hashCode() { + return _typeName.hashCode(); + } + + @Override + public String toString() { + return getClass().getSimpleName() + "(typeName)"; + } }