Skip to content

Commit 561aac8

Browse files
authored
Merge branch 'main' into removeUnusedImports_jdk17StringBlock
2 parents 0fdf2d0 + 4ca8189 commit 561aac8

File tree

53 files changed

+1041
-95
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1041
-95
lines changed

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ indent_style = space
2525
[*.{yml,yaml}]
2626
indent_style = space
2727
indent_size = 2
28+
29+
# Prevent unexpected automatic indentation when crafting test-cases
30+
[/testlib/src/main/resources/**]
31+
charset = unset
32+
end_of_line = unset
33+
insert_final_newline = unset
34+
trim_trailing_whitespace = unset
35+
indent_style = unset
36+
indent_size = unset
37+
ij_formatter_enabled = false

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Added
1414
* Support configuration of mirrors for P2 repositories in `EquoBasedStepBuilder` ([#1629](https://github.com/diffplug/spotless/issues/1629)).
15+
* The `style` option in Palantir Java Format ([#1654](https://github.com/diffplug/spotless/pull/1654)).
16+
* Added formatter for Gherkin feature files ([#1649](https://github.com/diffplug/spotless/issues/1649)).
1517
### Changes
1618
* **POTENTIALLY BREAKING** Converted `googleJavaFormat` to a compile-only dependency and drop support for versions < `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
1719
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{yes}}
7777
lib('antlr4.Antlr4FormatterStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
7878
lib('cpp.ClangFormatStep') +'{{yes}} | {{no}} | {{no}} | {{no}} |',
7979
extra('cpp.EclipseFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
80+
lib('gherkin.GherkinUtilsStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
8081
extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
8182
lib('java.GoogleJavaFormatStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
8283
lib('java.ImportOrderStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
@@ -125,6 +126,7 @@ lib('yaml.JacksonYamlStep') +'{{yes}} | {{yes}}
125126
| [`antlr4.Antlr4FormatterStep`](lib/src/main/java/com/diffplug/spotless/antlr4/Antlr4FormatterStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
126127
| [`cpp.ClangFormatStep`](lib/src/main/java/com/diffplug/spotless/cpp/ClangFormatStep.java) | :+1: | :white_large_square: | :white_large_square: | :white_large_square: |
127128
| [`cpp.EclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/cpp/EclipseFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
129+
| [`gherkin.GherkinUtilsStep`](lib/src/main/java/com/diffplug/spotless/gherkin/GherkinUtilsStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
128130
| [`groovy.GrEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
129131
| [`java.GoogleJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
130132
| [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :+1: | :+1: | :white_large_square: |

SECURITY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
For security disclosures, please send an email to spotless-security@diffplug.com

lib/build.gradle

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def NEEDS_GLUE = [
1818
'scalafmt',
1919
'jackson',
2020
'gson',
21-
'cleanthat'
21+
'cleanthat',
22+
'gherkin'
2223
]
2324
for (glue in NEEDS_GLUE) {
2425
sourceSets.register(glue) {
@@ -71,7 +72,7 @@ dependencies {
7172

7273
palantirJavaFormatCompileOnly 'com.palantir.javaformat:palantir-java-format:1.1.0' // this version needs to stay compilable against Java 8 for CI Job testNpm
7374

74-
googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.8' // minimum required version due to api changes before then
75+
googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.16.0' // minimum required version due to api changes before then
7576

7677
// used jackson-based formatters
7778
jacksonCompileOnly 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
@@ -116,6 +117,9 @@ dependencies {
116117
String VER_CLEANTHAT="2.8"
117118
cleanthatCompileOnly "io.github.solven-eu.cleanthat:java:$VER_CLEANTHAT"
118119
compatCleanthat2Dot1CompileAndTestOnly "io.github.solven-eu.cleanthat:java:$VER_CLEANTHAT"
120+
121+
gherkinCompileOnly 'io.cucumber:gherkin-utils:8.0.2'
122+
gherkinCompileOnly 'org.slf4j:slf4j-api:2.0.0'
119123
}
120124

121125
// we'll hold the core lib to a high standard
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.glue.gherkin;
17+
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import com.diffplug.spotless.FormatterFunc;
22+
import com.diffplug.spotless.gherkin.GherkinUtilsConfig;
23+
24+
import io.cucumber.gherkin.GherkinParser;
25+
import io.cucumber.gherkin.utils.pretty.Pretty;
26+
import io.cucumber.gherkin.utils.pretty.Syntax;
27+
import io.cucumber.messages.types.Envelope;
28+
import io.cucumber.messages.types.GherkinDocument;
29+
import io.cucumber.messages.types.Source;
30+
import io.cucumber.messages.types.SourceMediaType;
31+
32+
public class GherkinUtilsFormatterFunc implements FormatterFunc {
33+
private static final Logger LOGGER = LoggerFactory.getLogger(GherkinUtilsFormatterFunc.class);
34+
35+
private final GherkinUtilsConfig gherkinSimpleConfig;
36+
37+
public GherkinUtilsFormatterFunc(GherkinUtilsConfig gherkinSimpleConfig) {
38+
this.gherkinSimpleConfig = gherkinSimpleConfig;
39+
}
40+
41+
// Follows https://github.com/cucumber/gherkin-utils/blob/main/java/src/test/java/io/cucumber/gherkin/utils/pretty/PrettyTest.java
42+
private GherkinDocument parse(String gherkin) {
43+
GherkinParser parser = GherkinParser
44+
.builder()
45+
.includeSource(false)
46+
.build();
47+
return parser.parse(Envelope.of(new Source("test.feature", gherkin, SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN)))
48+
.findFirst()
49+
.orElseThrow(() -> new IllegalArgumentException("No envelope"))
50+
.getGherkinDocument()
51+
.orElseThrow(() -> new IllegalArgumentException("No gherkin document"));
52+
}
53+
54+
@Override
55+
public String apply(String inputString) {
56+
GherkinDocument gherkinDocument = parse(inputString);
57+
58+
return Pretty.prettyPrint(gherkinDocument, Syntax.gherkin);
59+
}
60+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.gherkin;
17+
18+
import java.io.Serializable;
19+
20+
public class GherkinUtilsConfig implements Serializable {
21+
private static final long serialVersionUID = 1L;
22+
23+
public static int defaultIndentSpaces() {
24+
// https://cucumber.io/docs/gherkin/reference/
25+
// Recommended indentation is 2 spaces
26+
return 2;
27+
}
28+
29+
final int indentSpaces;
30+
31+
public GherkinUtilsConfig(int indentSpaces) {
32+
this.indentSpaces = indentSpaces;
33+
}
34+
35+
public int getIndentSpaces() {
36+
return indentSpaces;
37+
}
38+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021-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.gherkin;
17+
18+
import java.io.IOException;
19+
import java.io.Serializable;
20+
import java.lang.reflect.Constructor;
21+
import java.lang.reflect.InvocationTargetException;
22+
import java.util.Objects;
23+
24+
import com.diffplug.spotless.FormatterFunc;
25+
import com.diffplug.spotless.FormatterStep;
26+
import com.diffplug.spotless.JarState;
27+
import com.diffplug.spotless.Provisioner;
28+
29+
public class GherkinUtilsStep {
30+
private static final String MAVEN_COORDINATE = "io.cucumber:gherkin-utils:";
31+
private static final String DEFAULT_VERSION = "8.0.2";
32+
33+
public static String defaultVersion() {
34+
return DEFAULT_VERSION;
35+
}
36+
37+
public static FormatterStep create(GherkinUtilsConfig gherkinSimpleConfig,
38+
String formatterVersion, Provisioner provisioner) {
39+
Objects.requireNonNull(provisioner, "provisioner cannot be null");
40+
return FormatterStep.createLazy("gherkin", () -> new GherkinUtilsStep.State(gherkinSimpleConfig, formatterVersion, provisioner), GherkinUtilsStep.State::toFormatter);
41+
}
42+
43+
private static final class State implements Serializable {
44+
private static final long serialVersionUID = 1L;
45+
46+
private final GherkinUtilsConfig gherkinSimpleConfig;
47+
private final JarState jarState;
48+
49+
private State(GherkinUtilsConfig gherkinSimpleConfig, String formatterVersion, Provisioner provisioner) throws IOException {
50+
this.gherkinSimpleConfig = gherkinSimpleConfig;
51+
this.jarState = JarState.from(MAVEN_COORDINATE + formatterVersion, provisioner);
52+
}
53+
54+
FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
55+
InstantiationException, IllegalAccessException {
56+
Class<?> formatterFunc = jarState.getClassLoader().loadClass("com.diffplug.spotless.glue.gherkin.GherkinUtilsFormatterFunc");
57+
Constructor<?> constructor = formatterFunc.getConstructor(GherkinUtilsConfig.class);
58+
return (FormatterFunc) constructor.newInstance(gherkinSimpleConfig);
59+
}
60+
}
61+
62+
private GherkinUtilsStep() {
63+
// cannot be directly instantiated
64+
}
65+
}

lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java

Lines changed: 22 additions & 4 deletions
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.
@@ -27,6 +27,7 @@ public class PalantirJavaFormatStep {
2727
// prevent direct instantiation
2828
private PalantirJavaFormatStep() {}
2929

30+
private static final String DEFAULT_STYLE = "PALANTIR";
3031
private static final String NAME = "palantir-java-format";
3132
private static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:";
3233
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.1.0").add(11, "2.28.0");
@@ -38,11 +39,17 @@ public static FormatterStep create(Provisioner provisioner) {
3839

3940
/** Creates a step which formats everything - code, import order, and unused imports. */
4041
public static FormatterStep create(String version, Provisioner provisioner) {
42+
return create(version, defaultStyle(), provisioner);
43+
}
44+
45+
/** Creates a step which formats everything - code, import order, and unused imports. And with the style input. */
46+
public static FormatterStep create(String version, String style, Provisioner provisioner) {
4147
Objects.requireNonNull(version, "version");
48+
Objects.requireNonNull(style, "style");
4249
Objects.requireNonNull(provisioner, "provisioner");
4350

4451
return FormatterStep.createLazy(NAME,
45-
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version),
52+
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version, style),
4653
State::createFormat);
4754
}
4855

@@ -51,25 +58,36 @@ public static String defaultVersion() {
5158
return JVM_SUPPORT.getRecommendedFormatterVersion();
5259
}
5360

61+
/** Get default style */
62+
public static String defaultStyle() {
63+
return DEFAULT_STYLE;
64+
}
65+
5466
private static final class State implements Serializable {
5567
private static final long serialVersionUID = 1L;
5668

5769
/** The jar that contains the formatter. */
5870
private final JarState jarState;
5971
/** Version of the formatter jar. */
6072
private final String formatterVersion;
73+
private final String style;
6174

6275
State(JarState jarState, String formatterVersion) {
76+
this(jarState, formatterVersion, DEFAULT_STYLE);
77+
}
78+
79+
State(JarState jarState, String formatterVersion, String style) {
6380
ModuleHelper.doOpenInternalPackagesIfRequired();
6481
this.jarState = jarState;
6582
this.formatterVersion = formatterVersion;
83+
this.style = style;
6684
}
6785

6886
FormatterFunc createFormat() throws Exception {
6987
final ClassLoader classLoader = jarState.getClassLoader();
7088
final Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc");
71-
final Constructor<?> constructor = formatterFunc.getConstructor();
72-
return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance());
89+
final Constructor<?> constructor = formatterFunc.getConstructor(String.class); // style
90+
return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance(style));
7391
}
7492
}
7593
}

lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-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.
@@ -26,16 +26,19 @@ public class PalantirJavaFormatFormatterFunc implements FormatterFunc {
2626

2727
private final Formatter formatter;
2828

29-
public PalantirJavaFormatFormatterFunc() {
29+
private final JavaFormatterOptions.Style formatterStyle;
30+
31+
public PalantirJavaFormatFormatterFunc(String style) {
32+
this.formatterStyle = JavaFormatterOptions.Style.valueOf(style);
3033
formatter = Formatter.createFormatter(JavaFormatterOptions.builder()
31-
.style(JavaFormatterOptions.Style.PALANTIR)
34+
.style(formatterStyle)
3235
.build());
3336
}
3437

3538
@Override
3639
public String apply(String input) throws Exception {
3740
String source = input;
38-
source = ImportOrderer.reorderImports(source, JavaFormatterOptions.Style.PALANTIR);
41+
source = ImportOrderer.reorderImports(source, formatterStyle);
3942
source = RemoveUnusedImports.removeUnusedImports(source);
4043
return formatter.formatSource(source);
4144
}

plugin-gradle/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1717
```
1818
Mirrors are selected by prefix match, for example `https://download.eclipse.org/eclipse/updates/4.26/` will be redirected to `https://some.internal.mirror/eclipse/eclipse/updates/4.26/`.
1919
The same configuration exists for `greclipse` and `eclipseCdt`.
20+
* The `style` option in Palantir Java Format ([#1654](https://github.com/diffplug/spotless/pull/1654)).
21+
* Added support for Gherkin feature files ([#1649](https://github.com/diffplug/spotless/issues/1649)).
2022
### Changes
2123
* **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions &lt; `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
2224
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
25+
### Fixed
26+
* Stop using deprecated conventions when used in Gradle >= `7.1`. ([#1618](https://github.com/diffplug/spotless/pull/1618))
2327

2428
## [6.17.0] - 2023-03-13
2529
### Added

0 commit comments

Comments
 (0)