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

Feature/gherkin #1649

Merged
merged 18 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ indent_style = space
[*.{yml,yaml}]
indent_style = space
indent_size = 2

# Prevent unexpected automatic indentation when crafting test-cases
[/testlib/src/main/resources/**]
nedtwigg marked this conversation as resolved.
Show resolved Hide resolved
charset = unset
end_of_line = unset
insert_final_newline = unset
trim_trailing_whitespace = unset
indent_style = unset
indent_size = unset
ij_formatter_enabled = false
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* `eclipse-jdt` from `4.19` to `4.20`
* `eclipse-wtp` from `4.18` to `4.20`

### Added
* Added formatter for [Gherkin feature files](https://github.com/diffplug/spotless/issues/928)
* Added Gradle configuration for Gherkin feature files

## [2.16.0] - 2021-09-04
### Added
* Added support for `google-java-format`'s `skip-reflowing-long-strings` option ([#929](https://github.com/diffplug/spotless/pull/929))
Expand Down
6 changes: 5 additions & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def NEEDS_GLUE = [
'scalafmt',
'jackson',
'gson',
'cleanthat'
'cleanthat',
'gherkin'
]
for (glue in NEEDS_GLUE) {
sourceSets.register(glue) {
Expand Down Expand Up @@ -115,6 +116,9 @@ dependencies {

cleanthatCompileOnly 'io.github.solven-eu.cleanthat:java:2.6'
compatCleanthat2Dot1CompileAndTestOnly 'io.github.solven-eu.cleanthat:java:2.6'

gherkinCompileOnly 'io.cucumber:gherkin-utils:8.0.2'
gherkinCompileOnly 'org.slf4j:slf4j-api:2.0.0'
}

// we'll hold the core lib to a high standard
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2023 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.glue.gherkin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.gherkin.GherkinSimpleConfig;

import io.cucumber.gherkin.GherkinParser;
import io.cucumber.gherkin.utils.pretty.Pretty;
import io.cucumber.gherkin.utils.pretty.Syntax;
import io.cucumber.messages.types.Envelope;
import io.cucumber.messages.types.GherkinDocument;
import io.cucumber.messages.types.Source;
import io.cucumber.messages.types.SourceMediaType;

public class GherkinFormatterFunc implements FormatterFunc {
private static final Logger LOGGER = LoggerFactory.getLogger(GherkinFormatterFunc.class);

private final GherkinSimpleConfig gherkinSimpleConfig;

public GherkinFormatterFunc(GherkinSimpleConfig gherkinSimpleConfig) {
this.gherkinSimpleConfig = gherkinSimpleConfig;
}

// Follows https://github.com/cucumber/gherkin-utils/blob/main/java/src/test/java/io/cucumber/gherkin/utils/pretty/PrettyTest.java
private GherkinDocument parse(String gherkin) {
GherkinParser parser = GherkinParser
.builder()
.includeSource(false)
.build();
return parser.parse(Envelope.of(new Source("test.feature", gherkin, SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN)))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("No envelope"))
.getGherkinDocument()
.orElseThrow(() -> new IllegalArgumentException("No gherkin document"));
}

@Override
public String apply(String inputString) {
GherkinDocument gherkinDocument = parse(inputString);

return Pretty.prettyPrint(gherkinDocument, Syntax.gherkin);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2023 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.gherkin;

import java.io.Serializable;

public class GherkinSimpleConfig implements Serializable {
public static int defaultIndentSpaces() {
// https://cucumber.io/docs/gherkin/reference/
// Recommended indentation is 2 spaces
return 2;
}

final int indentSpaces;

public GherkinSimpleConfig(int indentSpaces) {
this.indentSpaces = indentSpaces;
}

public int getIndentSpaces() {
return indentSpaces;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2021-2023 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.gherkin;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;

public class GherkinSimpleStep {
private static final String MAVEN_COORDINATE = "io.cucumber:gherkin-utils:";
private static final String DEFAULT_VERSION = "8.0.2";

public static String defaultVersion() {
return DEFAULT_VERSION;
}

public static FormatterStep create(GherkinSimpleConfig gherkinSimpleConfig,
String formatterVersion, Provisioner provisioner) {
Objects.requireNonNull(provisioner, "provisioner cannot be null");
return FormatterStep.createLazy("gherkin", () -> new GherkinSimpleStep.State(gherkinSimpleConfig, formatterVersion, provisioner), GherkinSimpleStep.State::toFormatter);
}

private static final class State implements Serializable {
private static final long serialVersionUID = 1L;

private final GherkinSimpleConfig gherkinSimpleConfig;
private final JarState jarState;

private State(GherkinSimpleConfig gherkinSimpleConfig, String formatterVersion, Provisioner provisioner) throws IOException {
this.gherkinSimpleConfig = gherkinSimpleConfig;
this.jarState = JarState.from(MAVEN_COORDINATE + formatterVersion, provisioner);
}

FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
InstantiationException, IllegalAccessException {
Class<?> formatterFunc = jarState.getClassLoader().loadClass("com.diffplug.spotless.glue.gherkin.GherkinFormatterFunc");
Constructor<?> constructor = formatterFunc.getConstructor(GherkinSimpleConfig.class);
return (FormatterFunc) constructor.newInstance(gherkinSimpleConfig);
}
}

private GherkinSimpleStep() {
// cannot be directly instantiated
}
}
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
```
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/`.
The same configuration exists for `greclipse` and `eclipseCdt`.
* Added support for Gherkin feature files (#???(???))
### Changes
* **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions &lt; `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
Expand Down
29 changes: 28 additions & 1 deletion plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Spotless supports all of Gradle's built-in performance features (incremental bui
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier), [ESLint](#eslint-typescript))
- [Javascript](#javascript) ([prettier](#prettier), [ESLint](#eslint-javascript))
- [JSON](#json)
- [YAML](#yaml)
- [Gherkin](#gherkin)
- Multiple languages
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection), [caching `npm install` results](#caching-results-of-npm-install))
- javascript, jsx, angular, vue, flow, typescript, css, less, scss, html, json, graphql, markdown, ymaml
Expand Down Expand Up @@ -850,7 +852,32 @@ spotless {
}
```

<a name="applying-prettier-to-javascript--flow--typescript--css--scss--less--jsx--graphql--yaml--etc"></a>
## Gherkin

- `com.diffplug.gradle.spotless.GherkinExtension` [javadoc](https://javadoc.io/doc/com.diffplug.spotless/spotless-plugin-gradle/6.17.0/com/diffplug/gradle/spotless/GherkinExtension.html), [code](https://github.com/diffplug/spotless/blob/main/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GherkinExtension.java)

```gradle
spotless {
gherkin {
target 'src/**/*.feature' // you have to set the target manually
simple() // has its own section below
}
}
```

### simple

Uses a Gherkin pretty-printer that optionally allows configuring the number of spaces that are used to pretty print objects:
blacelle marked this conversation as resolved.
Show resolved Hide resolved

```gradle
spotless {
gherkin {
target 'src/**/*.feature' // required to be set explicitly
simple()
.version('8.0.2') // optional: custom version of 'io.cucumber:gherkin-utils'
}
}
```

## Prettier

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2016-2023 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 javax.inject.Inject;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.gherkin.GherkinSimpleConfig;
import com.diffplug.spotless.gherkin.GherkinSimpleStep;

public class GherkinExtension extends FormatExtension {
static final String NAME = "gherkin";

@Inject
public GherkinExtension(SpotlessExtension spotless) {
super(spotless);
}

@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
throw noDefaultTargetException();
}
super.setupTask(task);
}

public SimpleConfig simple() {
return new SimpleConfig(GherkinSimpleConfig.defaultIndentSpaces());
}

public class SimpleConfig {
private String version;
private int indent;

public SimpleConfig() {
this.version = GherkinSimpleStep.defaultVersion();
this.indent = GherkinSimpleConfig.defaultIndentSpaces();
addStep(createStep());
}

public SimpleConfig(int indent) {
this.indent = indent;
addStep(createStep());
}

public void indentWithSpaces(int indent) {
this.indent = indent;
replaceStep(createStep());
}

private FormatterStep createStep() {
return GherkinSimpleStep.create(new GherkinSimpleConfig(indent), version, provisioner());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ public void yaml(Action<YamlExtension> closure) {
format(YamlExtension.NAME, YamlExtension.class, closure);
}

/** Configures the special Gherkin-specific extension. */
public void gherkin(Action<GherkinExtension> closure) {
requireNonNull(closure);
format(GherkinExtension.NAME, GherkinExtension.class, closure);
}

/** Configures a custom extension. */
public void format(String name, Action<FormatExtension> closure) {
requireNonNull(name, "name");
Expand Down
Loading