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

Html/JS/Json integration #315

Closed
wants to merge 4 commits into from
Closed
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 @@ -7,6 +7,8 @@ You might be looking for:

### Version 1.17.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/))

* Minor support for plugin-gradle and plugin-maven HTML, JS and JSON plugins ([#315](https://github.com/diffplug/spotless/pull/315)).

### Version 1.16.0 - October 30th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.16.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.16.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))

* Minor support for plugin-gradle and plugin-maven CSS plugins ([#311](https://github.com/diffplug/spotless/pull/311)).
Expand Down
44 changes: 44 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/html/HtmlDefaults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.html;

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

import com.diffplug.spotless.xml.XmlDefaults;

/** Common utilities for HTML */
public class HtmlDefaults {
//Prevent instantiation
private HtmlDefaults() {};

/**
* Filter based on Eclipse-WTP <code>org.eclipse.core.contenttype.contentTypes</code>
* extension <code>org.eclipse.wst.html.core.internal</code>.
*/
public static final List<String> FILE_FILTER = Collections.unmodifiableList(
Arrays.asList("html", "htm", "xhtml", "htpl", "wml", "shtml", "shtm")
.stream().map(s -> {
return "**/*." + s;
}).collect(Collectors.toList()));

/**
* XML delimiter is also valid for HTML.
*/
public static final String DELIMITER_EXPR = XmlDefaults.DELIMITER_EXPR;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all of these XxxDefaults classes, it would be better if their constants were public static String delimiterExpr() {} rather than public static final String DELIMITER_EXPR = .... See https://wiki.sei.cmu.edu/confluence/display/java/DCL59-J.+Do+not+apply+public+final+to+constants+whose+value+might+change+in+later+releases for details.

We've had to change quite a few of our DELIMITER_EXPR so far, I'd expect these might need to be revised in the future as well...

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
package com.diffplug.spotless.html;

import javax.annotation.ParametersAreNonnullByDefault;

import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault;
39 changes: 39 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/js/JsDefaults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.js;

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

/** Common utilities for JavaScript */
public class JsDefaults {
//Prevent instantiation
private JsDefaults() {};

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

/**
* Valid JS should start with variable/function definitions, closures or no-ops.
* Initial comment (one or multi-line) is considered header.
*/
public static final String DELIMITER_EXPR = "[A-Za-z\\{;]+";
}
7 changes: 7 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/js/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
package com.diffplug.spotless.js;

import javax.annotation.ParametersAreNonnullByDefault;

import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault;
41 changes: 41 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/json/JsonDefaults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.json;

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

/** Common utilities for Json */
public class JsonDefaults {
//Prevent instantiation
private JsonDefaults() {};

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

/**
* JSON itself does not accept comments. However, commenting JSON
* sources is good practice and projects like JSON.minify allow commenting.
* Hence also headers can be supported. Everything before the first object
* is considered to be part of the header.
*/
public static final String DELIMITER_EXPR = "\\{";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
package com.diffplug.spotless.json;

import javax.annotation.ParametersAreNonnullByDefault;

import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault;
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Version 3.17.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/))

* Added `html`, `js` and `json` support ([#315](https://github.com/diffplug/spotless/issues/315)) using formatter of Eclipse WTP 3.9.5 ([#241](https://github.com/diffplug/spotless/pull/241)).
* Updated default eclipse-jdt from 4.7.3a to 4.9.0 ([#316](https://github.com/diffplug/spotless/pull/316)). New version addresses enum-tab formatting bug in 4.8 ([#314](https://github.com/diffplug/spotless/issues/314)).

### Version 3.16.0 - October 30th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.16.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.16.0))
Expand Down
79 changes: 76 additions & 3 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ 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-HTML](#eclipse-wtp-html) (X)HTML code formatter
* Eclipse's [WTP-JS](#eclipse-wtp-js) JavaScript code formatter
* Eclipse's [WTP-JSON](#eclipse-wtp-json) JSON data 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 @@ -328,12 +331,12 @@ Use the Eclipse to define the *Code Style preferences* (see [Eclipse documentati
```gradle
spotless {
css {
target '**/*.css' '**/*.css2'// Change file filter. By default files with 'css' extension are supported
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
licenseHeader '/* Licensed under Apache-2.0 */' // License header
licenseHeaderFile './license.txt' // License header file
}
}
Expand All @@ -342,7 +345,77 @@ spotless {
<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.
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="html-wtp"></a>

## Applying to HTML sources

```gradle
spotless {
html {
target '**/*.html', '**/*.xhtml'// Change file filter. By default files with 'html','htm','xhtml','htpl','wml','shtml' and 'shtm' extension are supported
eclipse().configFile './html.prefs', './css.prefs', './js.xml' // Properties files 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-html"></a>

### Eclipse [WTP](https://www.eclipse.org/webtools/) HTML formatter
Use Eclipse to define the *HTML file 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 HTML file). The preferences are stored below your Eclipse workspace directory in `.metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.html.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. Furthermore the HTML code formatter takes into account the configuration of the [WTP-CSS](#eclipse-wtp-css) and [WTP-JS](#eclipse-wtp-js) formatter. The configuration can be provided within one or within separate files.

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

## Applying to JavaScript sources

```gradle
spotless {
js {
target '**/*js', '**/*.JS'// Change file filter. By default files with 'js' extension are supported
eclipse().configFile './js-formatter.xml' // Properties files 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-js"></a>

### Eclipse [WTP](https://www.eclipse.org/webtools/) JavaScript formatter
Use the Eclipse to define the *Code Style Formatter* (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 default configuration is used.

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

## Applying to JSON sources

```gradle
spotless {
js {
target '**/*json', '**/*.JSON'// Change file filter. By default files with 'json' extension are supported
eclipse().configFile './json-formatter.prefs' // Properties files 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 (for example in combination with JSON minify)
licenseHeader '// Licensed under Apache-2.0' // License header
licenseHeaderFile './license.txt' // License header file
}
}
```

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

### Eclipse [WTP](https://www.eclipse.org/webtools/) JSON formatter
Use Eclipse to define the *JSON editing 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/.settings/org.eclipse.wst.json.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>

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.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;
import com.diffplug.spotless.html.HtmlDefaults;

public class HtmlExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
static final String NAME = "html";

public HtmlExtension(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.createHtmlBuilder(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(HtmlDefaults.FILE_FILTER.toArray());
}
super.setupTask(task);
}

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

@Override
public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) {
return licenseHeaderFile(licenseHeaderFile, HtmlDefaults.DELIMITER_EXPR);
}
}
Loading