-
Notifications
You must be signed in to change notification settings - Fork 455
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
SpotlessCache extension for Eclipse formatters #279
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/WtpEclipseFormatterStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.extra.wtp; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Properties; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.Provisioner; | ||
import com.diffplug.spotless.extra.EclipseBasedStepBuilder; | ||
|
||
/** Formatter step which calls out to the Groovy-Eclipse formatter. */ | ||
public final class WtpEclipseFormatterStep { | ||
// prevent direct instantiation | ||
private WtpEclipseFormatterStep() {} | ||
|
||
private static final String NAME = "eclipse wtp formatters"; | ||
private static final String FORMATTER_PACKAGE = "com.diffplug.spotless.extra.eclipse.wtp."; | ||
private static final String DEFAULT_VERSION = "4.7.3a"; | ||
private static final String FORMATTER_METHOD = "format"; | ||
|
||
public static String defaultVersion() { | ||
return DEFAULT_VERSION; | ||
} | ||
|
||
/** Provides default configuration for CSSformatter */ | ||
public static EclipseBasedStepBuilder createCssBuilder(Provisioner provisioner) { | ||
return new EclipseBasedStepBuilder(NAME, " - css", provisioner, state -> apply("EclipseCssFormatterStepImpl", state)); | ||
} | ||
|
||
/** Provides default configuration for HTML formatter */ | ||
public static EclipseBasedStepBuilder createHtmlBuilder(Provisioner provisioner) { | ||
return new EclipseBasedStepBuilder(NAME, " - html", provisioner, state -> apply("EclipseHtmlFormatterStepImpl", state)); | ||
} | ||
|
||
/** Provides default configuration for Java Script formatter */ | ||
public static EclipseBasedStepBuilder createJsBuilder(Provisioner provisioner) { | ||
return new EclipseBasedStepBuilder(NAME, " - js", provisioner, state -> apply("EclipseJsFormatterStepImpl", state)); | ||
} | ||
|
||
/** Provides default configuration for JSON formatter */ | ||
public static EclipseBasedStepBuilder createJsonBuilder(Provisioner provisioner) { | ||
return new EclipseBasedStepBuilder(NAME, " - json", provisioner, state -> apply("EclipseJsonFormatterStepImpl", state)); | ||
} | ||
|
||
/** Provides default configuration for XML formatter */ | ||
public static EclipseBasedStepBuilder createXmlBuilder(Provisioner provisioner) { | ||
return new EclipseBasedStepBuilder(NAME, " - xml", provisioner, state -> apply("EclipseXmlFormatterStepImpl", state)); | ||
} | ||
|
||
private static FormatterFunc apply(String className, EclipseBasedStepBuilder.State state) throws Exception { | ||
Class<?> formatterClazz = state.loadClass(FORMATTER_PACKAGE + className); | ||
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences()); | ||
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class); | ||
return input -> { | ||
try { | ||
return (String) method.invoke(formatter, input); | ||
} catch (InvocationTargetException exceptionWrapper) { | ||
Throwable throwable = exceptionWrapper.getTargetException(); | ||
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null; | ||
throw (null == exception) ? exceptionWrapper : exception; | ||
} | ||
}; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@ParametersAreNonnullByDefault | ||
@ReturnValuesAreNonnullByDefault | ||
package com.diffplug.spotless.extra.wtp; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault; |
27 changes: 27 additions & 0 deletions
27
...ra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters/v4.7.3a.lockfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Spotless formatter based on Eclipse-WTP version 3.9.5 (see https://www.eclipse.org/webtools/) | ||
com.diffplug.spotless:spotless-eclipse-wtp:3.9.5 | ||
com.diffplug.spotless:spotless-eclipse-base:3.0.0 | ||
com.google.code.findbugs:annotations:3.0.0 | ||
com.google.code.findbugs:jsr305:3.0.0 | ||
com.ibm.icu:icu4j:61.1 | ||
org.eclipse.emf:org.eclipse.emf.common:2.12.0 | ||
org.eclipse.emf:org.eclipse.emf.ecore:2.12.0 | ||
org.eclipse.platform:org.eclipse.core.commands:3.9.100 | ||
org.eclipse.platform:org.eclipse.core.contenttype:3.7.0 | ||
org.eclipse.platform:org.eclipse.core.filebuffers:3.6.200 | ||
org.eclipse.platform:org.eclipse.core.filesystem:1.7.100 | ||
org.eclipse.platform:org.eclipse.core.jobs:3.10.0 | ||
org.eclipse.platform:org.eclipse.core.resources:3.13.0 | ||
org.eclipse.platform:org.eclipse.core.runtime:3.14.0 | ||
org.eclipse.platform:org.eclipse.equinox.app:1.3.500 | ||
org.eclipse.platform:org.eclipse.equinox.common:3.10.0 | ||
org.eclipse.platform:org.eclipse.equinox.preferences:3.7.100 | ||
org.eclipse.platform:org.eclipse.equinox.registry:3.8.0 | ||
#Spotless currently loads all transitive dependencies. | ||
#jface requires platform specific JARs (not used by formatter), which are not hosted via M2. | ||
#org.eclipse.platform:org.eclipse.jface.text:3.13.0 | ||
#org.eclipse.platform:org.eclipse.jface:3.14.0 | ||
org.eclipse.platform:org.eclipse.osgi.services:3.7.0 | ||
org.eclipse.platform:org.eclipse.osgi:3.13.0 | ||
org.eclipse.platform:org.eclipse.text:3.6.300 | ||
org.eclipse.xsd:org.eclipse.xsd:2.12.0 |
141 changes: 141 additions & 0 deletions
141
lib-extra/src/test/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStepTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.extra.wtp; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.Arrays; | ||
import java.util.Properties; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameter; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.Provisioner; | ||
import com.diffplug.spotless.TestProvisioner; | ||
import com.diffplug.spotless.extra.EclipseBasedStepBuilder; | ||
import com.diffplug.spotless.extra.eclipse.EclipseCommonTests; | ||
|
||
@RunWith(value = Parameterized.class) | ||
public class EclipseWtpFormatterStepTest extends EclipseCommonTests { | ||
|
||
private enum WTP { | ||
// @formatter:off | ||
CSS( "body {\na: v; b: \nv;\n} \n", | ||
"body {\n\ta: v;\n\tb: v;\n}", | ||
WtpEclipseFormatterStep::createCssBuilder), | ||
HTML( "<!DOCTYPE html> <html>\t<head> <meta charset=\"UTF-8\"></head>\n</html> ", | ||
"<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"UTF-8\">\n</head>\n</html>\n", | ||
WtpEclipseFormatterStep::createHtmlBuilder), | ||
JS( "function f( ) {\na.b(1,\n2);}", | ||
"function f() {\n a.b(1, 2);\n}", | ||
WtpEclipseFormatterStep::createJsBuilder), | ||
JSON( "{\"a\": \"b\", \"c\": { \"d\": \"e\",\"f\": \"g\"}}", | ||
"{\n\t\"a\": \"b\",\n\t\"c\": {\n\t\t\"d\": \"e\",\n\t\t\"f\": \"g\"\n\t}\n}", | ||
WtpEclipseFormatterStep::createJsonBuilder), | ||
XML( "<a><b> c</b></a>", "<a>\n\t<b> c</b>\n</a>", | ||
WtpEclipseFormatterStep::createXmlBuilder); | ||
// @formatter:on | ||
|
||
public final String input; | ||
public final String expectation; | ||
public final Function<Provisioner, EclipseBasedStepBuilder> builderMethod; | ||
|
||
private WTP(String input, final String expectation, Function<Provisioner, EclipseBasedStepBuilder> builderMethod) { | ||
this.input = input; | ||
this.expectation = expectation; | ||
this.builderMethod = builderMethod; | ||
} | ||
} | ||
|
||
@Parameters(name = "{0}") | ||
public static Iterable<WTP> data() { | ||
//TODO: XML is excluded. How to provide base location will be addressed by separate PR. | ||
return Arrays.asList(WTP.values()).stream().filter(e -> e != WTP.XML).collect(Collectors.toList()); | ||
} | ||
|
||
@Parameter(0) | ||
public WTP wtp; | ||
|
||
@Override | ||
protected String[] getSupportedVersions() { | ||
return new String[]{"4.7.3a"}; | ||
} | ||
|
||
@Override | ||
protected String getTestInput(String version) { | ||
return wtp.input; | ||
} | ||
|
||
@Override | ||
protected String getTestExpectation(String version) { | ||
return wtp.expectation; | ||
} | ||
|
||
@Override | ||
protected FormatterStep createStep(String version) { | ||
EclipseBasedStepBuilder builder = wtp.builderMethod.apply(TestProvisioner.mavenCentral()); | ||
builder.setVersion(version); | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* Check that configuration change is supported by all WTP formatters. | ||
* Some of the formatters only support static workspace configuration. | ||
* Hence separated class loaders are required for different configurations. | ||
*/ | ||
@Test | ||
public void multipleConfigurations() throws Exception { | ||
FormatterStep tabFormatter = createStepForDefaultVersion(config -> { | ||
config.setProperty("indentationChar", "tab"); | ||
config.setProperty("indentationSize", "1"); | ||
}); | ||
FormatterStep spaceFormatter = createStepForDefaultVersion(config -> { | ||
config.setProperty("indentationChar", "space"); | ||
config.setProperty("indentationSize", "5"); | ||
}); | ||
|
||
assertThat(formatWith(tabFormatter)).as("Tab formatting output unexpected").isEqualTo(wtp.expectation); //This is the default configuration | ||
assertThat(formatWith(spaceFormatter)).as("Space formatting output unexpected").isEqualTo(wtp.expectation.replace("\t", " ")); | ||
} | ||
|
||
private String formatWith(FormatterStep formatter) throws Exception { | ||
File baseLocation = File.createTempFile("EclipseWtpFormatterStepTest-", ".xml"); //Only required for relative path lookup | ||
return formatter.format(wtp.input, baseLocation); | ||
} | ||
|
||
private FormatterStep createStepForDefaultVersion(Consumer<Properties> config) throws IOException { | ||
Properties configProps = new Properties(); | ||
config.accept(configProps); | ||
File tempFile = File.createTempFile("EclipseWtpFormatterStepTest-", ".properties"); | ||
OutputStream tempOut = new FileOutputStream(tempFile); | ||
configProps.store(tempOut, "test properties"); | ||
EclipseBasedStepBuilder builder = wtp.builderMethod.apply(TestProvisioner.mavenCentral()); | ||
builder.setVersion(WtpEclipseFormatterStep.defaultVersion()); | ||
builder.setPreferences(Arrays.asList(tempFile)); | ||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be missing something, but I think you can avoid adding the
formatterStepExt
variable to EclipseBasedStepBuilder and EclipseBasedStepBuilder.State if you apply the following change:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently I use
formatterName
to lookup the dependencies, which are equal for the different format methods (JSONS, JS, HTML...). So question remains whether I need a different name.Each format method name per extension must be unique. Actually I should have not made this change, but addressed the question instead, whether we want a distinct
FormatterExtension
.For typescript-formatter, there was the decision not to have a separate formatter extension.
But I understood that the
typescript-formatter
is one formatter for all. This is not the case for WTP. I can use the default WTP file extension settings to pick a formatter method for a file, but that should be configurable... So maybe it is worth to have dedicatedFormatterExtension
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha! That's what I was missing. In that case, this LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re:
FormatterExtension
, you means atypescript {}
extension and ajavascript {}
extension (andcss {}
andhtml {}
)? I lean towards the defaultFormatterExtension
, but I'm okay with having separate extensions instead. Here is my argument:"Convention-over-configuration" is a great idea, and gradle has strong conventions for all the JVM languages. For the JVM languages we support (
java{}
,groovy{}
,kotlin{}
,scala{}
), we can slurp the file locations from gradle's project model, and take advantage of that convention. In my gradle web projects, I've found that there aren't strong conventions for the location of web resources. Sometimes they're kinda scattered around in thesrc/main/resources
directory, but other times they're being used for a static site outside of the JVM context, and they're just in the project root. So we're not really able to use the "convention-over-configuration" to simplify setup for web resources.On the other hand, it probably helps user-discoverability to have dedicated
html {}
,css {}
, etc. blocks... The real proof will be the docs. If it's easier to documenthtml {}
,css {}
- dedicated formats, then that's what we should do.We already have a
sql{}
block that isn't able to use conventions to know where the sql files are...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll remove the name extension and aim for one
FormatExtension
per WTP formatter method.