Skip to content

Commit 84276b5

Browse files
authored
Eclipse-WTP CSS integration (#311)
Integration of eclipse-wtp CSS formatter.
1 parent 3c132d1 commit 84276b5

File tree

14 files changed

+366
-5
lines changed

14 files changed

+366
-5
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.css;
17+
18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
/** Common utilities for CSS */
23+
public class CssDefaults {
24+
//Prevent instantiation
25+
private CssDefaults() {};
26+
27+
/**
28+
* Filter based on Eclipse-WTP <code>org.eclipse.core.contenttype.contentTypes</code>
29+
* extension <code>org.eclipse.wst.css.core.csssource</code>.
30+
*/
31+
public static final List<String> FILE_FILTER = Collections.unmodifiableList(
32+
Arrays.asList("**/*.css"));
33+
34+
/**
35+
* Match line that starts with a selector. Selection is quite broad.
36+
* Assure that multiline licenses have a proper indentation.
37+
* Assure that your has been formatted before (no whitespace before first selector).
38+
*/
39+
public static final String DELIMITER_EXPR = "[A-Za-z\\.\\#]+";
40+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@ParametersAreNonnullByDefault
2+
@ReturnValuesAreNonnullByDefault
3+
package com.diffplug.spotless.css;
4+
5+
import javax.annotation.ParametersAreNonnullByDefault;
6+
7+
import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault;

lib/src/main/java/com/diffplug/spotless/xml/XmlDefaults.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class XmlDefaults {
2727

2828
/**
2929
* Filter based on Eclipse-WTP <code>org.eclipse.core.contenttype.contentTypes</code>
30-
* extension <code>org.eclipse.wst.xml.core.xmlsource</code>
30+
* extension <code>org.eclipse.wst.xml.core.xmlsource</code>.
3131
*/
3232
public static final List<String> FILE_FILTER = Collections.unmodifiableList(
3333
Arrays.asList("xml", "xsl", "xslt", "wsdl", "xsd", "exsd", "xmi")

plugin-gradle/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Spotless can check and apply formatting to any plain-text file, using simple rul
7979

8080
* Eclipse's [CDT](#eclipse-cdt) C/C++ code formatter
8181
* Eclipse's java code formatter (including style and import ordering)
82+
* Eclipse's [WTP-CSS](#eclipse-wtp-css) CSS code formatter
8283
* Eclipse's [WTP-XML](#eclipse-wtp-xml) XML code formatter
8384
* Google's [google-java-format](https://github.com/google/google-java-format)
8485
* [Groovy Eclipse](#groovy-eclipse)'s groovy code formatter
@@ -320,6 +321,29 @@ spotless {
320321

321322
Use the Eclipse to define the *Code Style preferences* (see [Eclipse documentation](https://www.eclipse.org/documentation/)). Within the preferences *Edit...* dialog, you can export your configuration as XML file, which can be used as a `configFile`. If no `configFile` is provided, the CDT default configuration is used.
322323

324+
<a name="css-wtp"></a>
325+
326+
## Applying to CSS sources
327+
328+
```gradle
329+
spotless {
330+
css {
331+
target '**/*.css' '**/*.css2'// Change file filter. By default files with 'css' extension are supported
332+
eclipse().configFile './css-formatter.prefs' // Properties file of the Eclipse WTP formatter
333+
// Use for example eclipse('4.7.3a') to specify a specific version of Eclipse,
334+
// available versions are: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters
335+
// also supports license headers
336+
licenseHeader '<!-- Licensed under Apache-2.0 -->' // License header
337+
licenseHeaderFile './license.txt' // License header file
338+
}
339+
}
340+
```
341+
342+
<a name="eclipse-wtp-css"></a>
343+
344+
### Eclipse [WTP](https://www.eclipse.org/webtools/) CSS formatter
345+
Use Eclipse to define the *CSS Files* editor preferences (see [Eclipse documentation](http://help.eclipse.org/photon/topic/org.eclipse.wst.sse.doc.user/topics/tsrcedt025.html)) and the *Cleanup* preferences available in the *Source* menu (when editing a CSS file). The preferences are stored below your Eclipse workspace directory in `.metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs`. Note that only the differences to the default configuration are stored within the file. Omit the 'configFile' entirely to use the default Eclipse configuration.
346+
323347
<a name="xml-wtp"></a>
324348

325349
## Applying to XML sources
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import static com.diffplug.gradle.spotless.PluginGradlePreconditions.requireElementsNonNull;
19+
20+
import org.gradle.api.Project;
21+
22+
import com.diffplug.spotless.css.CssDefaults;
23+
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
24+
import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;
25+
26+
public class CssExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
27+
static final String NAME = "css";
28+
29+
public CssExtension(SpotlessExtension rootExtension) {
30+
super(rootExtension);
31+
}
32+
33+
public EclipseConfig eclipse() {
34+
return new EclipseConfig(EclipseWtpFormatterStep.defaultVersion());
35+
}
36+
37+
public EclipseConfig eclipse(String version) {
38+
return new EclipseConfig(version);
39+
}
40+
41+
public class EclipseConfig {
42+
private final EclipseBasedStepBuilder builder;
43+
44+
EclipseConfig(String version) {
45+
builder = EclipseWtpFormatterStep.createCssBuilder(GradleProvisioner.fromProject(getProject()));
46+
builder.setVersion(version);
47+
addStep(builder.build());
48+
}
49+
50+
public void configFile(Object... configFiles) {
51+
requireElementsNonNull(configFiles);
52+
Project project = getProject();
53+
builder.setPreferences(project.files(configFiles).getFiles());
54+
replaceStep(builder.build());
55+
}
56+
57+
}
58+
59+
@Override
60+
protected void setupTask(SpotlessTask task) {
61+
if (target == null) {
62+
target(CssDefaults.FILE_FILTER.toArray());
63+
}
64+
super.setupTask(task);
65+
}
66+
67+
@Override
68+
public LicenseHeaderConfig licenseHeader(String licenseHeader) {
69+
return licenseHeader(licenseHeader, CssDefaults.DELIMITER_EXPR);
70+
}
71+
72+
@Override
73+
public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) {
74+
return licenseHeaderFile(licenseHeaderFile, CssDefaults.DELIMITER_EXPR);
75+
}
76+
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public void sql(Action<SqlExtension> closure) {
118118
configure(SqlExtension.NAME, SqlExtension.class, closure);
119119
}
120120

121+
/** Configures the special css-specific extension for CSS files. */
122+
public void css(Action<CssExtension> closure) {
123+
configure(CssExtension.NAME, CssExtension.class, closure);
124+
}
125+
121126
/** Configures the special xml-specific extension for XML/XSL/... files (XHTML is excluded). */
122127
public void xml(Action<XmlExtension> closure) {
123128
configure(XmlExtension.NAME, XmlExtension.class, closure);

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/HasBuiltinDelimiterForLicenseTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public void testWithCommonInterfaceForConfiguringLicences() throws IOException {
3838
" java {",
3939
" assert (it instanceof HasBuiltinDelimiterForLicense) : \"Was `$it`\"",
4040
" }",
41+
" cpp {",
42+
" assert (it instanceof HasBuiltinDelimiterForLicense) : \"Was `$it`\"",
43+
" }",
44+
" css {",
45+
" assert (it instanceof HasBuiltinDelimiterForLicense) : \"Was `$it`\"",
46+
" }",
47+
" xml {",
48+
" assert (it instanceof HasBuiltinDelimiterForLicense) : \"Was `$it`\"",
49+
" }",
4150
"}");
4251
gradleRunner()
4352
.withGradleVersion("4.6")

plugin-maven/README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Spotless supports the following powerful formatters:
7474

7575
* Eclipse's java code formatter (including style and import ordering)
7676
* Eclipse's [CDT](https://www.eclipse.org/cdt/) C/C++ code formatter
77+
* Eclipse's [WTP](https://www.eclipse.org/webtools/) CSS and XML code formatter
7778
* Google's [google-java-format](https://github.com/google/google-java-format)
7879
* User-defined license enforcement, regex replacement, etc.
7980

@@ -189,7 +190,31 @@ By default, all files matching `src/main/cpp/**/*.<ext>` and `src/test/cpp/**/*.
189190
```
190191
Use the Eclipse to define the *Code Style preferences* (see [Eclipse documentation](https://www.eclipse.org/documentation/)). Within the preferences *Edit...* dialog, you can export your configuration as XML file, which can be used as a configuration `<file>`. If no `<file>` is provided, the CDT default configuration is used.
191192

192-
<a name="format"></a>
193+
<a name="xml"></a>
194+
195+
## Applying to CSS source
196+
197+
By default, all files matching `src/**/*.css` Ant style pattern will be formatted. Each element under `<css>` is a step, and they will be applied in the order specified. Every step is optional, and they will be applied in the order specified.
198+
199+
```xml
200+
<configuration>
201+
<css>
202+
<licenseHeader>
203+
<!-- Specify either content or file, but not both -->
204+
<content>/* Licensed under Apache-2.0 */</content>
205+
<file>${basedir}/license-header</file>
206+
</licenseHeader>
207+
<eclipse>
208+
<file>${basedir}/eclipse-fmt.pref</file>
209+
<!-- Optional, available versions: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters -->
210+
<version>4.7.3a</version>
211+
</eclipse>
212+
</css>
213+
</configuration>
214+
```
215+
Use Eclipse to define the *CSS Files* editor preferences (see [Eclipse documentation](http://help.eclipse.org/photon/topic/org.eclipse.wst.sse.doc.user/topics/tsrcedt025.html)) and the *Cleanup* preferences available in the *Source* menu (when editing a CSS file). The preferences are stored below your Eclipse workspace directory in `.metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs`. Note that only the differences to the default configuration are stored within the file. If no `<file>` is provided, the WTP default configuration is used.
216+
217+
<a name="xml"></a>
193218

194219
## Applying to XML source
195220

@@ -208,13 +233,15 @@ By default, all files matching `src/**/*.<ext>` Ant style pattern will be format
208233
<!-- Optional, available versions: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters -->
209234
<version>4.7.3a</version>
210235
</eclipse>
211-
</cpp>
236+
</xml>
212237
</configuration>
213238
```
214-
Use Eclipse to define the *XML editor preferences* (see [Eclipse documentation](https://www.eclipse.org/documentation/)). The preferences are stored below your Eclipse workspace directory in `.metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.xml.core.prefs`. Note that only the differences to the default configuration are stored within the file. If no `<file>` is provided, the WTP default configuration is used..
239+
Use Eclipse to define the *XML editor preferences* (see [Eclipse documentation](https://www.eclipse.org/documentation/)). The preferences are stored below your Eclipse workspace directory in `.metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.xml.core.prefs`. Note that only the differences to the default configuration are stored within the file. If no `<file>` is provided, the WTP default configuration is used.
215240

216241
The Eclipse WTP formatter supports DTD/XSD restrictions on white spaces. For XSD/DTD lookup, relative and absolute XSD/DTD URIs are supported. Furthermore a user catalog can be configured using the `userCatalog` property key. Add the property to the preference `<file>`.
217242

243+
<a name="format"></a>
244+
218245
## Applying to custom sources
219246

220247
By default, no Ant-Style include patterns are defined. Each element under `<format>` is a step, and they will be applied in the order specified. Every step is optional, and they will be applied in the order specified. It is possible to define multiple custom formats.

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.diffplug.spotless.LineEnding;
3838
import com.diffplug.spotless.Provisioner;
3939
import com.diffplug.spotless.maven.cpp.Cpp;
40+
import com.diffplug.spotless.maven.css.Css;
4041
import com.diffplug.spotless.maven.generic.Format;
4142
import com.diffplug.spotless.maven.generic.LicenseHeader;
4243
import com.diffplug.spotless.maven.java.Java;
@@ -94,6 +95,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
9495
@Parameter
9596
private Cpp cpp;
9697

98+
@Parameter
99+
private Css css;
100+
97101
protected abstract void process(List<File> files, Formatter formatter) throws MojoExecutionException;
98102

99103
@Override
@@ -152,7 +156,7 @@ private FileLocator getFileLocator() {
152156
}
153157

154158
private List<FormatterFactory> getFormatterFactories() {
155-
return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, xml))
159+
return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, css, xml))
156160
.filter(Objects::nonNull)
157161
.collect(toList());
158162
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.css;
17+
18+
import java.util.Set;
19+
import java.util.stream.Collectors;
20+
21+
import com.diffplug.spotless.css.CssDefaults;
22+
import com.diffplug.spotless.maven.FormatterFactory;
23+
import com.diffplug.spotless.maven.generic.LicenseHeader;
24+
25+
/**
26+
* A {@link FormatterFactory} implementation that corresponds to {@code <css>...</css>} configuration element.
27+
* <p>
28+
* It defines a formatter for CSS source files that can execute both language agnostic (e.g. {@link LicenseHeader})
29+
* and css-specific (e.g. {@link Eclipse}) steps.
30+
*/
31+
public class Css extends FormatterFactory {
32+
33+
private static final Set<String> DEFAULT_INCLUDES = CssDefaults.FILE_FILTER
34+
.stream().map(s -> "src/" + s).collect(Collectors.toSet());
35+
36+
@Override
37+
public Set<String> defaultIncludes() {
38+
return DEFAULT_INCLUDES;
39+
}
40+
41+
public void addEclipse(Eclipse eclipse) {
42+
addStepFactory(eclipse);
43+
}
44+
45+
@Override
46+
public String licenseHeaderDelimiter() {
47+
return CssDefaults.DELIMITER_EXPR;
48+
}
49+
50+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.css;
17+
18+
import java.io.File;
19+
import java.util.Arrays;
20+
21+
import org.apache.maven.plugins.annotations.Parameter;
22+
23+
import com.diffplug.spotless.FormatterStep;
24+
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
25+
import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;
26+
import com.diffplug.spotless.maven.FormatterStepConfig;
27+
import com.diffplug.spotless.maven.FormatterStepFactory;
28+
29+
public class Eclipse implements FormatterStepFactory {
30+
31+
@Parameter
32+
private String file;
33+
34+
@Parameter
35+
private String version;
36+
37+
@Override
38+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
39+
EclipseBasedStepBuilder eclipseConfig = EclipseWtpFormatterStep.createCssBuilder(stepConfig.getProvisioner());
40+
eclipseConfig.setVersion(version == null ? EclipseWtpFormatterStep.defaultVersion() : version);
41+
if (null != file) {
42+
File settingsFile = stepConfig.getFileLocator().locateFile(file);
43+
eclipseConfig.setPreferences(Arrays.asList(settingsFile));
44+
}
45+
return eclipseConfig.build();
46+
}
47+
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ protected void writePomWithCppSteps(String... steps) throws IOException {
110110
writePom(groupWithSteps("cpp", steps));
111111
}
112112

113+
protected void writePomWithCssSteps(String... steps) throws IOException {
114+
writePom(groupWithSteps("css", steps));
115+
}
116+
113117
protected void writePom(String... configuration) throws IOException {
114118
writePom(null, configuration);
115119
}

0 commit comments

Comments
 (0)