-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #328 from matthiasbalke/issue-326-antlr4
[WIP] Add ANTLR4 support
- Loading branch information
Showing
16 changed files
with
459 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
31 changes: 31 additions & 0 deletions
31
lib/src/main/java/com/diffplug/spotless/antlr4/Antlr4Defaults.java
This file contains 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,31 @@ | ||
/* | ||
* Copyright 2016-2020 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; | ||
|
||
public class Antlr4Defaults { | ||
private static final String LICENSE_HEADER_DELIMITER = "(grammar|lexer grammar|parser grammar)"; | ||
private static final String INCLUDES = "src/*/antlr4/**/*.g4"; | ||
|
||
private Antlr4Defaults() {} | ||
|
||
public static String licenseHeaderDelimiter() { | ||
return LICENSE_HEADER_DELIMITER; | ||
} | ||
|
||
public static String includes() { | ||
return INCLUDES; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
lib/src/main/java/com/diffplug/spotless/antlr4/Antlr4FormatterStep.java
This file contains 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-2020 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.2.1"; | ||
|
||
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 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 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-2020 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(SpotlessExtensionBase 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, provisioner()); | ||
} | ||
} | ||
|
||
@Override | ||
protected void setupTask(SpotlessTask task) { | ||
if (target == null) { | ||
target = parseTarget(Antlr4Defaults.includes()); | ||
} | ||
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 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 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-2020 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 GradleIntegrationHarness { | ||
|
||
@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.2.1')", | ||
" }", | ||
"}"}; | ||
|
||
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); | ||
} | ||
} |
This file contains 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 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
46 changes: 46 additions & 0 deletions
46
plugin-maven/src/main/java/com/diffplug/spotless/maven/antlr4/Antlr4.java
This file contains 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,46 @@ | ||
/* | ||
* Copyright 2016-2020 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.maven.antlr4; | ||
|
||
import java.util.Set; | ||
|
||
import com.diffplug.common.collect.ImmutableSet; | ||
import com.diffplug.spotless.antlr4.Antlr4Defaults; | ||
import com.diffplug.spotless.maven.FormatterFactory; | ||
import com.diffplug.spotless.maven.generic.LicenseHeader; | ||
|
||
/** | ||
* A {@link FormatterFactory} implementation that corresponds to {@code <antlr4>...</antlr4>} configuration element. | ||
* <p> | ||
* It defines a formatter for ANTLR4 source files that can execute both language agnostic (e.g. {@link LicenseHeader}) | ||
* and anltr4-specific (e.g. {@link Antlr4Formatter}) steps. | ||
*/ | ||
public class Antlr4 extends FormatterFactory { | ||
@Override | ||
public Set<String> defaultIncludes() { | ||
return ImmutableSet.of(Antlr4Defaults.includes()); | ||
} | ||
|
||
@Override | ||
public String licenseHeaderDelimiter() { | ||
return Antlr4Defaults.licenseHeaderDelimiter(); | ||
} | ||
|
||
public void addAntlr4Formatter(Antlr4Formatter antlr4Formatter) { | ||
addStepFactory(antlr4Formatter); | ||
} | ||
|
||
} |
Oops, something went wrong.