-
Notifications
You must be signed in to change notification settings - Fork 473
[WIP] Add ANTLR4 support #328
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
ae9ef44
first antlr4 dev version
matthiasbalke 66f486a
resolve antlr4 formatter lib at runtime
matthiasbalke 40daf9a
prepare for PR (#326)
matthiasbalke 6663bba
add option to configure Antlr4Formatter version used
matthiasbalke deb3a76
rename step name
matthiasbalke 84b2cb5
add docs for gradle extension
matthiasbalke 55364f2
remove mavenLocal development repository
matthiasbalke d821563
Merge remote-tracking branch 'upstream/master' into antlr4
matthiasbalke f436c77
add antlr4 maven support
matthiasbalke 6dc960b
fix antlr4 includes/target docs
matthiasbalke 77a82c9
fix antlr4 default license header separator
matthiasbalke be4175d
Merge remote-tracking branch 'upstream/master' into antlr4
matthiasbalke 38e2baa
add Antlr4Formatter to maven-plugin Readme
matthiasbalke 257b1bc
Merge remote-tracking branch 'upstream/master' into issue-326-antlr4
matthiasbalke 8b7f53e
Merge branch 'master' into antlr4
matthiasbalke 2167c15
Merge remote-tracking branch 'upstream/master' into issue-326-antlr4
matthiasbalke b351889
Merge remote-tracking branch 'upstream/master' into antlr4
matthiasbalke 341a8af
update Antlr4Formatter Version to upcoming release 1.2.1
matthiasbalke 9343ff4
Merge remote-tracking branch 'origin/issue-326-antlr4' into antlr4
matthiasbalke 642cda9
Revert "update Antlr4Formatter Version to upcoming release 1.2.1"
matthiasbalke cebef42
Merge remote-tracking branch 'upstream/master' into issue-326-antlr4
matthiasbalke 0e9d4aa
Merge remote-tracking branch 'upstream/master' into antlr4
matthiasbalke cbdc360
Merge remote-tracking branch 'origin/issue-326-antlr4' into antlr4
matthiasbalke e5818be
removed duplicated changelog entries
matthiasbalke 3117cb4
fix spelling
matthiasbalke 2ed1fad
Merge branch 'master' into issue-326-antlr4
matthiasbalke 7c9790f
Merge remote-tracking branch 'upstream/master' into antlr4
matthiasbalke 81c6f1c
update Antlr4Formatter lib to fixed version 1.2.1
matthiasbalke cacc0ac
Merge branch 'main' into antlr4
nedtwigg d5bb7a6
Update license headers.
nedtwigg 83e1a02
Add the `antlr4` extension to SpotlessExtensionBase.
nedtwigg 8b261b4
Move the changelog entries to their correct location at `[Unreleased]`.
nedtwigg b4530a1
Fix the maven and gradle integration tests.
nedtwigg ecf1861
Remove the unnecessary gradle-internals test dedicated to Antlr4.
nedtwigg f111f2e
Fix Antlr4Extension.
nedtwigg 3887435
fix expected format
289af25
fix expected format
62e52f6
Merge remote-tracking branch 'upstream/main' into issue-rebased
d946b3f
use a more generic default pattern
2bd12d7
Move Antlr4Extension from the deprecated `GradleProvisioner` to the n…
nedtwigg 837df92
Revert all README changes - I will put them back later.
nedtwigg 60b9805
Minor refactor of the antlr4 defaults.
nedtwigg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
lib/src/main/java/com/diffplug/spotless/antlr4/Antlr4Defaults.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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|lexer grammar|parser grammar)"; | ||
|
||
private static final List<String> defaultIncludes = Arrays.asList("src/main/antlr4/**/*.g4", "src/test/antlr4/**/*.g4"); | ||
|
||
private Antlr4Defaults() {} | ||
|
||
public static String licenseHeaderDelimiter() { | ||
return LICENSE_HEADER_DELIMITER; | ||
} | ||
|
||
public static List<String> defaultIncludes() { | ||
return defaultIncludes; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
lib/src/main/java/com/diffplug/spotless/antlr4/Antlr4FormatterStep.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
plugin-gradle/src/main/java/com/diffplug/gradle/spotless/Antlr4Extension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Antlr4ExtensionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.