Skip to content

Eclipse-WTP CSS integration #311

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

Merged
merged 4 commits into from
Oct 25, 2018
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
40 changes: 40 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/css/CssDefaults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.css;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/** Common utilities for CSS */
public class CssDefaults {
//Prevent instantiation
private CssDefaults() {};

/**
* Filter based on Eclipse-WTP <code>org.eclipse.core.contenttype.contentTypes</code>
* extension <code>org.eclipse.wst.css.core.csssource</code>.
*/
public static final List<String> FILE_FILTER = Collections.unmodifiableList(
Arrays.asList("**/*.css"));

/**
* Match line that starts with a selector. Selection is quite broad.
* Assure that multiline licenses have a proper indentation.
* Assure that your has been formatted before (no whitespace before first selector).
*/
public static final String DELIMITER_EXPR = "[A-Za-z\\.\\#]+";
}
7 changes: 7 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/css/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
package com.diffplug.spotless.css;

import javax.annotation.ParametersAreNonnullByDefault;

import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class XmlDefaults {

/**
* Filter based on Eclipse-WTP <code>org.eclipse.core.contenttype.contentTypes</code>
* extension <code>org.eclipse.wst.xml.core.xmlsource</code>
* extension <code>org.eclipse.wst.xml.core.xmlsource</code>.
*/
public static final List<String> FILE_FILTER = Collections.unmodifiableList(
Arrays.asList("xml", "xsl", "xslt", "wsdl", "xsd", "exsd", "xmi")
Expand Down
24 changes: 24 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Spotless can check and apply formatting to any plain-text file, using simple rul

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

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.

<a name="css-wtp"></a>

## Applying to CSS sources

```gradle
spotless {
css {
target '**/*.css' '**/*.css2'// Change file filter. By default files with 'css' extension are supported
eclipse().configFile './css-formatter.prefs' // Properties file of the Eclipse WTP formatter
// Use for example eclipse('4.7.3a') to specify a specific version of Eclipse,
// available versions are: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters
// also supports license headers
licenseHeader '<!-- Licensed under Apache-2.0 -->' // License header
licenseHeaderFile './license.txt' // License header file
}
}
```

<a name="eclipse-wtp-css"></a>

### Eclipse [WTP](https://www.eclipse.org/webtools/) CSS formatter
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.

<a name="xml-wtp"></a>

## Applying to XML sources
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.gradle.spotless;

import static com.diffplug.gradle.spotless.PluginGradlePreconditions.requireElementsNonNull;

import org.gradle.api.Project;

import com.diffplug.spotless.css.CssDefaults;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;

public class CssExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
static final String NAME = "css";

public CssExtension(SpotlessExtension rootExtension) {
super(rootExtension);
}

public EclipseConfig eclipse() {
return new EclipseConfig(EclipseWtpFormatterStep.defaultVersion());
}

public EclipseConfig eclipse(String version) {
return new EclipseConfig(version);
}

public class EclipseConfig {
private final EclipseBasedStepBuilder builder;

EclipseConfig(String version) {
builder = EclipseWtpFormatterStep.createCssBuilder(GradleProvisioner.fromProject(getProject()));
builder.setVersion(version);
addStep(builder.build());
}

public void configFile(Object... configFiles) {
requireElementsNonNull(configFiles);
Project project = getProject();
builder.setPreferences(project.files(configFiles).getFiles());
replaceStep(builder.build());
}

}

@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
target(CssDefaults.FILE_FILTER.toArray());
}
super.setupTask(task);
}

@Override
public LicenseHeaderConfig licenseHeader(String licenseHeader) {
return licenseHeader(licenseHeader, CssDefaults.DELIMITER_EXPR);
}

@Override
public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) {
return licenseHeaderFile(licenseHeaderFile, CssDefaults.DELIMITER_EXPR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public void sql(Action<SqlExtension> closure) {
configure(SqlExtension.NAME, SqlExtension.class, closure);
}

/** Configures the special css-specific extension for CSS files. */
public void css(Action<CssExtension> closure) {
configure(CssExtension.NAME, CssExtension.class, closure);
}

/** Configures the special xml-specific extension for XML/XSL/... files (XHTML is excluded). */
public void xml(Action<XmlExtension> closure) {
configure(XmlExtension.NAME, XmlExtension.class, closure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public void testWithCommonInterfaceForConfiguringLicences() throws IOException {
" java {",
" assert (it instanceof HasBuiltinDelimiterForLicense) : \"Was `$it`\"",
" }",
" cpp {",
" assert (it instanceof HasBuiltinDelimiterForLicense) : \"Was `$it`\"",
" }",
" css {",
" assert (it instanceof HasBuiltinDelimiterForLicense) : \"Was `$it`\"",
" }",
" xml {",
" assert (it instanceof HasBuiltinDelimiterForLicense) : \"Was `$it`\"",
" }",
"}");
gradleRunner()
.withGradleVersion("4.6")
Expand Down
33 changes: 30 additions & 3 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Spotless supports the following powerful formatters:

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

Expand Down Expand Up @@ -189,7 +190,31 @@ By default, all files matching `src/main/cpp/**/*.<ext>` and `src/test/cpp/**/*.
```
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.

<a name="format"></a>
<a name="xml"></a>

## Applying to CSS source

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.

```xml
<configuration>
<css>
<licenseHeader>
<!-- Specify either content or file, but not both -->
<content>/* Licensed under Apache-2.0 */</content>
<file>${basedir}/license-header</file>
</licenseHeader>
<eclipse>
<file>${basedir}/eclipse-fmt.pref</file>
<!-- Optional, available versions: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters -->
<version>4.7.3a</version>
</eclipse>
</css>
</configuration>
```
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.

<a name="xml"></a>

## Applying to XML source

Expand All @@ -208,13 +233,15 @@ By default, all files matching `src/**/*.<ext>` Ant style pattern will be format
<!-- Optional, available versions: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters -->
<version>4.7.3a</version>
</eclipse>
</cpp>
</xml>
</configuration>
```
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..
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.

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>`.

<a name="format"></a>

## Applying to custom sources

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.maven.cpp.Cpp;
import com.diffplug.spotless.maven.css.Css;
import com.diffplug.spotless.maven.generic.Format;
import com.diffplug.spotless.maven.generic.LicenseHeader;
import com.diffplug.spotless.maven.java.Java;
Expand Down Expand Up @@ -94,6 +95,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
@Parameter
private Cpp cpp;

@Parameter
private Css css;

protected abstract void process(List<File> files, Formatter formatter) throws MojoExecutionException;

@Override
Expand Down Expand Up @@ -152,7 +156,7 @@ private FileLocator getFileLocator() {
}

private List<FormatterFactory> getFormatterFactories() {
return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, xml))
return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, css, xml))
.filter(Objects::nonNull)
.collect(toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.maven.css;

import java.util.Set;
import java.util.stream.Collectors;

import com.diffplug.spotless.css.CssDefaults;
import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;

/**
* A {@link FormatterFactory} implementation that corresponds to {@code <css>...</css>} configuration element.
* <p>
* It defines a formatter for CSS source files that can execute both language agnostic (e.g. {@link LicenseHeader})
* and css-specific (e.g. {@link Eclipse}) steps.
*/
public class Css extends FormatterFactory {

private static final Set<String> DEFAULT_INCLUDES = CssDefaults.FILE_FILTER
.stream().map(s -> "src/" + s).collect(Collectors.toSet());

@Override
public Set<String> defaultIncludes() {
return DEFAULT_INCLUDES;
}

public void addEclipse(Eclipse eclipse) {
addStepFactory(eclipse);
}

@Override
public String licenseHeaderDelimiter() {
return CssDefaults.DELIMITER_EXPR;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.maven.css;

import java.io.File;
import java.util.Arrays;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class Eclipse implements FormatterStepFactory {

@Parameter
private String file;

@Parameter
private String version;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
EclipseBasedStepBuilder eclipseConfig = EclipseWtpFormatterStep.createCssBuilder(stepConfig.getProvisioner());
eclipseConfig.setVersion(version == null ? EclipseWtpFormatterStep.defaultVersion() : version);
if (null != file) {
File settingsFile = stepConfig.getFileLocator().locateFile(file);
eclipseConfig.setPreferences(Arrays.asList(settingsFile));
}
return eclipseConfig.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ protected void writePomWithCppSteps(String... steps) throws IOException {
writePom(groupWithSteps("cpp", steps));
}

protected void writePomWithCssSteps(String... steps) throws IOException {
writePom(groupWithSteps("css", steps));
}

protected void writePom(String... configuration) throws IOException {
writePom(null, configuration);
}
Expand Down
Loading