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

[WIP] Add ANTLR4 support #328

Merged
merged 42 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
ae9ef44
first antlr4 dev version
matthiasbalke Dec 27, 2018
66f486a
resolve antlr4 formatter lib at runtime
matthiasbalke Dec 27, 2018
40daf9a
prepare for PR (#326)
matthiasbalke Dec 28, 2018
6663bba
add option to configure Antlr4Formatter version used
matthiasbalke Dec 28, 2018
deb3a76
rename step name
matthiasbalke Dec 28, 2018
84b2cb5
add docs for gradle extension
matthiasbalke Dec 28, 2018
55364f2
remove mavenLocal development repository
matthiasbalke Dec 28, 2018
d821563
Merge remote-tracking branch 'upstream/master' into antlr4
matthiasbalke Dec 28, 2018
f436c77
add antlr4 maven support
matthiasbalke Dec 28, 2018
6dc960b
fix antlr4 includes/target docs
matthiasbalke Dec 28, 2018
77a82c9
fix antlr4 default license header separator
matthiasbalke Dec 28, 2018
be4175d
Merge remote-tracking branch 'upstream/master' into antlr4
matthiasbalke Dec 28, 2018
38e2baa
add Antlr4Formatter to maven-plugin Readme
matthiasbalke Dec 28, 2018
257b1bc
Merge remote-tracking branch 'upstream/master' into issue-326-antlr4
matthiasbalke Jan 12, 2019
8b7f53e
Merge branch 'master' into antlr4
matthiasbalke Jan 22, 2019
2167c15
Merge remote-tracking branch 'upstream/master' into issue-326-antlr4
matthiasbalke Jun 5, 2019
b351889
Merge remote-tracking branch 'upstream/master' into antlr4
matthiasbalke Jun 12, 2019
341a8af
update Antlr4Formatter Version to upcoming release 1.2.1
matthiasbalke Jun 12, 2019
9343ff4
Merge remote-tracking branch 'origin/issue-326-antlr4' into antlr4
matthiasbalke Jun 12, 2019
642cda9
Revert "update Antlr4Formatter Version to upcoming release 1.2.1"
matthiasbalke Jun 12, 2019
cebef42
Merge remote-tracking branch 'upstream/master' into issue-326-antlr4
matthiasbalke Feb 1, 2020
0e9d4aa
Merge remote-tracking branch 'upstream/master' into antlr4
matthiasbalke Mar 10, 2020
cbdc360
Merge remote-tracking branch 'origin/issue-326-antlr4' into antlr4
matthiasbalke Mar 10, 2020
e5818be
removed duplicated changelog entries
matthiasbalke Mar 10, 2020
3117cb4
fix spelling
matthiasbalke Mar 10, 2020
2ed1fad
Merge branch 'master' into issue-326-antlr4
matthiasbalke Mar 11, 2020
7c9790f
Merge remote-tracking branch 'upstream/master' into antlr4
matthiasbalke Mar 25, 2020
81c6f1c
update Antlr4Formatter lib to fixed version 1.2.1
matthiasbalke Mar 25, 2020
cacc0ac
Merge branch 'main' into antlr4
nedtwigg Jun 24, 2020
d5bb7a6
Update license headers.
nedtwigg Jun 24, 2020
83e1a02
Add the `antlr4` extension to SpotlessExtensionBase.
nedtwigg Jun 24, 2020
8b261b4
Move the changelog entries to their correct location at `[Unreleased]`.
nedtwigg Jun 25, 2020
b4530a1
Fix the maven and gradle integration tests.
nedtwigg Jun 25, 2020
ecf1861
Remove the unnecessary gradle-internals test dedicated to Antlr4.
nedtwigg Jun 25, 2020
f111f2e
Fix Antlr4Extension.
nedtwigg Jun 25, 2020
3887435
fix expected format
Jul 1, 2020
289af25
fix expected format
Jul 1, 2020
62e52f6
Merge remote-tracking branch 'upstream/main' into issue-rebased
Jul 1, 2020
d946b3f
use a more generic default pattern
Jul 1, 2020
2bd12d7
Move Antlr4Extension from the deprecated `GradleProvisioner` to the n…
nedtwigg Jul 1, 2020
837df92
Revert all README changes - I will put them back later.
nedtwigg Jul 1, 2020
60b9805
Minor refactor of the antlr4 defaults.
nedtwigg Jul 1, 2020
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.18.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/))

* Added Antlr4 support ([#326](https://github.com/diffplug/spotless/issues/326))

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

* 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)).
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ lib('npm.TsFmtFormatterStep') +'{{yes}} | {{no}}
lib('scala.ScalaFmtStep') +'{{yes}} | {{yes}} | {{no}} |',
lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
extra('wtp.WtpEclipseFormatterStep') +'{{yes}} | {{yes}} | {{no}} |',
lib('antlr4.Antlr4FormatterStep') +'{{yes}} | {{yes}} | {{no}} |',
'| [(Your FormatterStep here)](CONTRIBUTING.md#how-to-add-a-new-formatterstep) | {{no}} | {{no}} | {{no}} |',
'| Fast up-to-date checking | {{yes}} | {{no}} | {{no}} |',
'| Automatic idempotency safeguard | {{yes}} | {{no}} | {{no}} |',
Expand Down Expand Up @@ -78,6 +79,7 @@ extra('wtp.WtpEclipseFormatterStep') +'{{yes}} | {{yes}}
| [`scala.ScalaFmtStep`](lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java) | :+1: | :+1: | :white_large_square: |
| [`sql.DBeaverSQLFormatterStep`](lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`wtp.WtpEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/WtpEclipseFormatterStep.java) | :+1: | :+1: | :white_large_square: |
| [`antlr4.Antlr4FormatterStep`](lib/src/main/java/com/diffplug/spotless/antlr4/Antlr4FormatterStep.java) | :+1: | :+1: | :white_large_square: |
| [(Your FormatterStep here)](CONTRIBUTING.md#how-to-add-a-new-formatterstep) | :white_large_square: | :white_large_square: | :white_large_square: |
| Fast up-to-date checking | :+1: | :white_large_square: | :white_large_square: |
| Automatic idempotency safeguard | :+1: | :white_large_square: | :white_large_square: |
Expand Down
36 changes: 36 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/antlr4/Antlr4Defaults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.antlr4;

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

public class Antlr4Defaults {

private static final String LICENSE_HEADER_DELIMITER = "grammar ";

private static final List<String> defaultIncludes = Arrays.asList("src/main/antlr4/**/*.g4", "src/test/antlr4/**/*.g4");
matthiasbalke marked this conversation as resolved.
Show resolved Hide resolved

private Antlr4Defaults() {}

public static String licenseHeaderDelimiter() {
return LICENSE_HEADER_DELIMITER;
}

public static List<String> defaultIncludes() {
return defaultIncludes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.antlr4;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.diffplug.spotless.*;

public class Antlr4FormatterStep {

public static final String NAME = "antlr4Formatter";

private Antlr4FormatterStep() {}

private static final String MAVEN_COORDINATE = "com.khubla.antlr4formatter:antlr4-formatter:";
private static final String DEFAULT_VERSION = "1.1.0";

public static FormatterStep create(Provisioner provisioner) {
return create(defaultVersion(), provisioner);
}

public static FormatterStep create(String version, Provisioner provisioner) {
return FormatterStep.createLazy(NAME, () -> new State(version, provisioner), State::createFormat);
}

public static String defaultVersion() {
return DEFAULT_VERSION;
}

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

/**
* The jar that contains the formatter.
*/
final JarState jarState;

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

FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException {
ClassLoader classLoader = jarState.getClassLoader();

// String Antlr4Formatter::format(String input)
Class<?> formatter = classLoader.loadClass("com.khubla.antlr4formatter.Antlr4Formatter");
Method formatterMethod = formatter.getMethod("format", String.class);

return input -> {
try {
return (String) formatterMethod.invoke(null, input);
} catch (InvocationTargetException e) {
throw ThrowingEx.unwrapCause(e);
}
};
}
}
}
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Version 3.18.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 Antlr4 support ([#326](https://github.com/diffplug/spotless/issues/326))

### Version 3.17.0 - December 13th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.17.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.17.0))

* 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)).
Expand Down
21 changes: 21 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Spotless can check and apply formatting to any plain-text file, using simple rul
* [DBeaver sql format](https://dbeaver.jkiss.org/)
* [Prettier: An opinionated code formatter](https://prettier.io)
* [TypeScript Formatter (tsfmt)](https://github.com/vvakame/typescript-formatter)
* [Antlr4Formatter](https://github.com/antlr/Antlr4Formatter)
* Any user-defined function which takes an unformatted string and outputs a formatted version.

Contributions are welcome, see [the contributing guide](../CONTRIBUTING.md) for development info.
Expand Down Expand Up @@ -510,6 +511,26 @@ spotless {

Spotless uses npm to install necessary packages locally. It runs prettier using [J2V8](https://github.com/eclipsesource/J2V8) internally after that.


<a name="antlr4"></a>

## Applying to ANTLR4 source

By default, all ANTLR4 sources matching `src/main/antlr4/**/*.g4` will be formatted. To change this,
matthiasbalke marked this conversation as resolved.
Show resolved Hide resolved
set the `target` parameter as described in the [Custom rules](#custom) section.

```gradle
spotless {
antlr4 {
// use default Antlr4Formatter version
antlr4Formatter()

// specify a custom Antlr4Formatter version
antlr4Formatter('1.1.0')
}
}
```

<a name="license-header"></a>

## License header options
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 java.util.Objects;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.antlr4.Antlr4Defaults;
import com.diffplug.spotless.antlr4.Antlr4FormatterStep;

public class Antlr4Extension extends FormatExtension implements HasBuiltinDelimiterForLicense {
static final String NAME = "antlr4";

public Antlr4Extension(SpotlessExtension rootExtension) {
super(rootExtension);
}

public Antlr4FormatterConfig antlr4Formatter() {
return antlr4Formatter(Antlr4FormatterStep.defaultVersion());
}

public Antlr4FormatterConfig antlr4Formatter(String version) {
return new Antlr4FormatterConfig(version);
}

public class Antlr4FormatterConfig {

private final String version;

Antlr4FormatterConfig(String version) {
this.version = Objects.requireNonNull(version);
addStep(createStep());
}

private FormatterStep createStep() {
return Antlr4FormatterStep.create(this.version, GradleProvisioner.fromProject(getProject()));
}
}

@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
target = parseTarget(Antlr4Defaults.defaultIncludes());
}
super.setupTask(task);
}

@Override
public LicenseHeaderConfig licenseHeader(String licenseHeader) {
return licenseHeader(licenseHeader, Antlr4Defaults.licenseHeaderDelimiter());
}

@Override
public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) {
return licenseHeaderFile(licenseHeaderFile, Antlr4Defaults.licenseHeaderDelimiter());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public void typescript(Action<TypescriptExtension> closure) {
configure(TypescriptExtension.NAME, TypescriptExtension.class, closure);
}

/** Configures the special antlr4-specific extension for antlr4 files. */
public void antlr4(Action<Antlr4Extension> closure) {
configure(Antlr4Extension.NAME, Antlr4Extension.class, closure);
}

/** Configures a custom extension. */
public void format(String name, Action<FormatExtension> closure) {
requireNonNull(name, "name");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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 java.io.IOException;

import org.junit.Test;

public class Antlr4ExtensionTest extends GradleIntegrationTest {

@Test
public void applyUsingDefaultVersion() throws IOException {
String[] buildScript = {
"buildscript {",
" repositories {",
" mavenCentral()",
" }",
"}",
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
"spotless {",
" antlr4 {",
" target 'src/main/antlr4/**/*.g4'",
" antlr4Formatter()",
" }",
"}"};

assertAppliedFormat(buildScript);
}

@Test
public void applyUsingCustomVersion() throws IOException {
String[] buildScript = {
"buildscript {",
" repositories {",
" mavenCentral()",
" }",
"}",
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
"spotless {",
" antlr4 {",
" target 'src/main/antlr4/**/*.g4'",
" antlr4Formatter('1.1.0')",
" }",
"}"};

assertAppliedFormat(buildScript);
}

private void assertAppliedFormat(String... buildScript) throws IOException {
String testFile = "src/main/antlr4/Hello.g4";

setFile("build.gradle").toLines(buildScript);

String unformatted = "antlr4/Hello.unformatted.g4";
String formatted = "antlr4/Hello.formatted.g4";
setFile(testFile).toResource(unformatted);

gradleRunner().withArguments("spotlessApply").build();

assertFile(testFile).sameAsResource(formatted);
}
}
Loading