Skip to content

Commit 0c79674

Browse files
committed
Merge branch 'main' into feat/ktlint-editor-config
2 parents 0ede8e8 + c50503e commit 0c79674

File tree

12 files changed

+266
-147
lines changed

12 files changed

+266
-147
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ VER_SLF4J=[1.6,2.0[
2727
VER_DURIAN=1.2.0
2828
VER_JGIT=5.13.1.202206130422-r
2929
VER_JUNIT=5.9.1
30-
VER_ASSERTJ=3.23.1
30+
VER_ASSERTJ=3.24.1
3131
VER_MOCKITO=4.11.0
3232

3333
# Used for Maven Plugin

plugin-maven/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* Add option `editorConfigFile` for `ktLint` [#142](https://github.com/diffplug/spotless/issues/142)
8+
### Fixed
9+
* Reduce spurious invalidations of the up-to-date index file ([#1461](https://github.com/diffplug/spotless/pull/1461))
810

911
## [2.29.0] - 2023-01-02
1012
### Added
1113
* Added support for M2E's incremental compilation ([#1414](https://github.com/diffplug/spotless/pull/1414) fixes [#1413](https://github.com/diffplug/spotless/issues/1413))
14+
* Add JSON support ([#1446](https://github.com/diffplug/spotless/pull/1446))
1215
### Fixed
1316
* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
1417
* Support `ktlint` 0.48+ ([#1432](https://github.com/diffplug/spotless/pull/1432)) fixes ([#1430](https://github.com/diffplug/spotless/issues/1430))

plugin-maven/README.md

Lines changed: 63 additions & 6 deletions
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)
@@ -128,7 +129,16 @@ To use it in your pom, just [add the Spotless dependency](https://search.maven.o
128129
Spotless consists of a list of formats (in the example above, `misc` and `java`), and each format has:
129130
- a `target` (the files to format), which you set with [`includes` and `excludes`](https://github.com/diffplug/spotless/blob/989abbecff4d8373c6111c1a98f359eadc532429/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java#L51-L55)
130131
- a list of `FormatterStep`, which are just `String -> String` functions, such as [`replace`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java), [`replaceRegex`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java), [`trimTrailingWhitespace`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java), [`indent`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java), [`prettier`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Prettier.java), [`eclipseWtp`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/EclipseWtp.java), and [`licenseHeader`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java).
131-
132+
- **order matters**, and this is good! (More info [here](https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md) and [here](https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md#how-spotless-works))
133+
- For example, `googleJavaFormat` always indents with spaces, but some wish it had a tab mode
134+
- ```xml
135+
<googleJavaFormat/> // this works
136+
<indent><tabs>true</tabs><spacesPerTab>2</spacesPerTab></indent>
137+
```
138+
- ```xml
139+
<indent><tabs>true</tabs><spacesPerTab>2</spacesPerTab></indent>
140+
<googleJavaFormat/> // the tab indentation gets overwritten
141+
```
132142

133143
### Requirements
134144

@@ -178,6 +188,10 @@ any other maven phase (i.e. compile) then it can be configured as below;
178188
<include>src/test/java/**/*.java</include>
179189
</includes>
180190

191+
<googleJavaFormat /> <!-- has its own section below -->
192+
<eclipse /> <!-- has its own section below -->
193+
<prettier /> <!-- has its own section below -->
194+
181195
<importOrder /> <!-- standard import order -->
182196
<importOrder> <!-- or a custom ordering -->
183197
<wildcardsLast>false</wildcardsLast> <!-- Optional, default false. Sort wildcard import after specific imports -->
@@ -187,10 +201,6 @@ any other maven phase (i.e. compile) then it can be configured as below;
187201

188202
<removeUnusedImports /> <!-- self-explanatory -->
189203

190-
<googleJavaFormat /> <!-- has its own section below -->
191-
<eclipse /> <!-- has its own section below -->
192-
<prettier /> <!-- has its own section below -->
193-
194204
<formatAnnotations /> <!-- fixes formatting of type annotations, see below -->
195205

196206
<licenseHeader>
@@ -437,7 +447,7 @@ Additionally, `editorConfigOverride` options will override what's supplied in `.
437447
<configuration>
438448
<cpp>
439449
<includes> <!-- You have to set the target manually -->
440-
<include>src/native/**</inclue>
450+
<include>src/native/**</include>
441451
</includes>
442452

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

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

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

711768
## Prettier

plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndex.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,7 +76,7 @@ static FileIndex read(FileIndexConfig config, Log log) {
7676
PluginFingerprint computedFingerprint = config.getPluginFingerprint();
7777
PluginFingerprint storedFingerprint = PluginFingerprint.from(firstLine);
7878
if (!computedFingerprint.equals(storedFingerprint)) {
79-
log.info("Fingerprint mismatch in the index file. Fallback to an empty index");
79+
log.info("Index file corresponds to a different configuration of the plugin. Either the plugin version or its configuration has changed. Fallback to an empty index");
8080
return emptyIndexFallback(config);
8181
} else {
8282
Content content = readIndexContent(reader, config.getProjectDir(), log);

plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/PluginFingerprint.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2022 DiffPlug
2+
* Copyright 2021-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,17 +17,21 @@
1717

1818
import java.io.IOException;
1919
import java.io.UncheckedIOException;
20-
import java.util.ArrayList;
2120
import java.util.Base64;
22-
import java.util.List;
2321
import java.util.Objects;
2422

25-
import org.apache.maven.model.Dependency;
2623
import org.apache.maven.model.Plugin;
2724
import org.apache.maven.project.MavenProject;
2825

2926
import com.diffplug.spotless.Formatter;
3027

28+
/**
29+
* Represents a particular Spotless Maven plugin setup using a Base64-encoded serialized form of:
30+
* <ol>
31+
* <li>Plugin version as configured in the POM</li>
32+
* <li>Formatter instances created according to the POM configuration</li>
33+
* </ol>
34+
*/
3135
class PluginFingerprint {
3236

3337
private static final String SPOTLESS_PLUGIN_KEY = "com.diffplug.spotless:spotless-maven-plugin";
@@ -83,22 +87,15 @@ public String toString() {
8387
}
8488

8589
private static byte[] digest(Plugin plugin, Iterable<Formatter> formatters) {
86-
// dependencies can be an unserializable org.apache.maven.model.merge.ModelMerger$MergingList
87-
// replace it with a serializable ArrayList
88-
List<Dependency> dependencies = plugin.getDependencies();
89-
plugin.setDependencies(new ArrayList<>(dependencies));
9090
try (ObjectDigestOutputStream out = ObjectDigestOutputStream.create()) {
91-
out.writeObject(plugin);
91+
out.writeObject(plugin.getVersion());
9292
for (Formatter formatter : formatters) {
9393
out.writeObject(formatter);
9494
}
9595
out.flush();
9696
return out.digest();
9797
} catch (IOException e) {
9898
throw new UncheckedIOException("Unable to serialize plugin " + plugin, e);
99-
} finally {
100-
// reset the original list
101-
plugin.setDependencies(dependencies);
10299
}
103100
}
104101
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023 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 2023 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 2023 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+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -156,6 +156,10 @@ protected void writePomWithMarkdownSteps(String... steps) throws IOException {
156156
writePom(groupWithSteps("markdown", including("**/*.md"), steps));
157157
}
158158

159+
protected void writePomWithJsonSteps(String... steps) throws IOException {
160+
writePom(groupWithSteps("json", including("**/*.json"), steps));
161+
}
162+
159163
protected void writePom(String... configuration) throws IOException {
160164
writePom(null, configuration, null);
161165
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,7 +60,7 @@ void readFallsBackToEmptyIndexOnFingerprintMismatch() throws Exception {
6060
FileIndex index = FileIndex.read(config, log);
6161

6262
assertThat(index.size()).isZero();
63-
verify(log).info("Fingerprint mismatch in the index file. Fallback to an empty index");
63+
verify(log).info("Index file corresponds to a different configuration of the plugin. Either the plugin version or its configuration has changed. Fallback to an empty index");
6464
}
6565

6666
@Test

0 commit comments

Comments
 (0)