Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1638: Instantiate the code formatter without any detours #1864

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> 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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
391 changes: 391 additions & 0 deletions lib-extra/src/test/resources/eclipse_formatter_issue_1638.xml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
67 changes: 35 additions & 32 deletions testlib/src/main/resources/java/eclipse/AbstractType.clean
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
}

}
Loading