Skip to content

Commit 5e728be

Browse files
committed
Initial iteration
1 parent 11622e0 commit 5e728be

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

plugin-maven/README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ user@machine repo % mvn spotless:check
5858
- [Maven Pom](#maven-pom) ([sortPom](#sortpom))
5959
- [Markdown](#markdown) ([flexmark](#flexmark))
6060
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
61+
- [JSON](#json)
6162
- Multiple languages
6263
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection))
6364
- [eclipse web tools platform](#eclipse-web-tools-platform)
@@ -431,7 +432,7 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T
431432
<configuration>
432433
<cpp>
433434
<includes> <!-- You have to set the target manually -->
434-
<include>src/native/**</inclue>
435+
<include>src/native/**</include>
435436
</includes>
436437

437438
<eclipseCdt /> <!-- has its own section below -->
@@ -700,6 +701,53 @@ The auto-discovery of config files (up the file tree) will not work when using t
700701

701702
For details, see the [npm detection](#npm-detection) and [`.npmrc` detection](#npmrc-detection) sections of prettier, which apply also to tsfmt.
702703

704+
## JSON
705+
706+
- `com.diffplug.spotless.maven.json.Json` [javadoc](https://javadoc.io/doc/com.diffplug.spotless/spotless-plugin-gradle/6.12.1/com/diffplug/gradle/spotless/JsonExtension.html), [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Json.java)
707+
708+
```xml
709+
<configuration>
710+
<json>
711+
<includes> <!-- You have to set the target manually -->
712+
<include>src/**/*.json</include>
713+
</includes>
714+
715+
<simple /> <!-- has its own section below -->
716+
<gson /> <!-- has its own section below -->
717+
</json>
718+
</configuration>
719+
```
720+
721+
### simple
722+
723+
Uses a JSON pretty-printer that optionally allows configuring the number of spaces that are used to pretty print objects:
724+
725+
```xml
726+
<simple>
727+
<indentSpaces>4</indentSpaces> <!-- optional: specify the number of spaces to use -->
728+
</simple>
729+
```
730+
731+
### Gson
732+
733+
Uses Google Gson to also allow sorting by keys besides custom indentation - useful for i18n files.
734+
735+
```xml
736+
<gson>
737+
<indentSpaces>4</indentSpaces> <!-- optional: specify the number of spaces to use -->
738+
<sortByKeys>false</sortByKeys> <!-- optional: sort JSON by its keys -->
739+
<escapeHtml>false</indentSpaces> <!-- optional: escape HTML in values -->
740+
<version>2.8.1</version> <!-- optional: specify version -->
741+
</gson>
742+
```
743+
744+
Notes:
745+
* There's no option in Gson to leave HTML as-is (i.e. escaped HTML would remain escaped, raw would remain raw). Either
746+
all HTML characters are written escaped or none. Set `escapeHtml` if you prefer the former.
747+
* `sortByKeys` will apply lexicographic order on the keys of the input JSON. See the
748+
[javadoc of String](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#compareTo(java.lang.String))
749+
for details.
750+
703751
<a name="applying-prettier-to-javascript--flow--typescript--css--scss--less--jsx--graphql--yaml--etc"></a>
704752

705753
## Prettier
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2021 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.json;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.json.gson.GsonStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Gson implements FormatterStepFactory {
26+
private static final String DEFAULT_GSON_VERSION = "2.8.9";
27+
28+
@Parameter
29+
int indentSpaces = Json.DEFAULT_INDENTATION;
30+
31+
@Parameter
32+
boolean sortByKeys = false;
33+
34+
@Parameter
35+
boolean escapeHtml = false;
36+
37+
@Parameter
38+
String version = DEFAULT_GSON_VERSION;
39+
40+
@Override
41+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
42+
int indentSpaces = this.indentSpaces;
43+
return GsonStep.create(indentSpaces, sortByKeys, escapeHtml, version, stepConfig.getProvisioner());
44+
}
45+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2021 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.json;
17+
18+
import java.util.Collections;
19+
import java.util.Set;
20+
21+
import com.diffplug.spotless.maven.FormatterFactory;
22+
23+
/**
24+
* A {@link FormatterFactory} implementation that corresponds to {@code <json>...</json>} configuration element.
25+
*/
26+
public class Json extends FormatterFactory {
27+
public static final int DEFAULT_INDENTATION = 4;
28+
29+
@Override
30+
public Set<String> defaultIncludes() {
31+
return Collections.emptySet();
32+
}
33+
34+
@Override
35+
public String licenseHeaderDelimiter() {
36+
return null;
37+
}
38+
39+
public void addSimple(Simple simple) {
40+
addStepFactory(simple);
41+
}
42+
43+
public void addGson(Gson gson) {
44+
addStepFactory(gson);
45+
}
46+
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2021 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.json;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.json.JsonSimpleStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Simple implements FormatterStepFactory {
26+
27+
@Parameter
28+
int indentSpaces = Json.DEFAULT_INDENTATION;
29+
30+
@Override
31+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
32+
int indentSpaces = this.indentSpaces;
33+
return JsonSimpleStep.create(indentSpaces, stepConfig.getProvisioner());
34+
}
35+
}

0 commit comments

Comments
 (0)