-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
24 changed files
with
545 additions
and
4 deletions.
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
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
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
74 changes: 74 additions & 0 deletions
74
lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.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-2022 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.java; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.Constructor; | ||
import java.util.Objects; | ||
|
||
import com.diffplug.spotless.*; | ||
|
||
/** Wraps up <a href="https://github.com/palantir/palantir-java-format">palantir-java-format</a> fork of | ||
* <a href="https://github.com/google/google-java-format">google-java-format</a> as a FormatterStep. */ | ||
public class PalantirJavaFormatStep { | ||
// prevent direct instantiation | ||
private PalantirJavaFormatStep() {} | ||
|
||
private static final String NAME = "palantir-java-format"; | ||
private static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:"; | ||
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.1.0").add(11, "2.10.0"); | ||
|
||
/** Creates a step which formats everything - code, import order, and unused imports. */ | ||
public static FormatterStep create(Provisioner provisioner) { | ||
return create(defaultVersion(), provisioner); | ||
} | ||
|
||
/** Creates a step which formats everything - code, import order, and unused imports. */ | ||
public static FormatterStep create(String version, Provisioner provisioner) { | ||
Objects.requireNonNull(version, "version"); | ||
Objects.requireNonNull(provisioner, "provisioner"); | ||
|
||
return FormatterStep.createLazy(NAME, | ||
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version), | ||
State::createFormat); | ||
} | ||
|
||
/** Get default formatter version */ | ||
public static String defaultVersion() { | ||
return JVM_SUPPORT.getRecommendedFormatterVersion(); | ||
} | ||
|
||
private static final class State implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** The jar that contains the formatter. */ | ||
private final JarState jarState; | ||
/** Version of the formatter jar. */ | ||
private final String formatterVersion; | ||
|
||
State(JarState jarState, String formatterVersion) { | ||
this.jarState = jarState; | ||
this.formatterVersion = formatterVersion; | ||
} | ||
|
||
FormatterFunc createFormat() throws Exception { | ||
final ClassLoader classLoader = jarState.getClassLoader(); | ||
final Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc"); | ||
final Constructor<?> constructor = formatterFunc.getConstructor(); | ||
return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance()); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...lantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.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,47 @@ | ||
/* | ||
* Copyright 2022 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.pjf; | ||
|
||
import com.palantir.javaformat.java.Formatter; | ||
import com.palantir.javaformat.java.ImportOrderer; | ||
import com.palantir.javaformat.java.JavaFormatterOptions; | ||
import com.palantir.javaformat.java.RemoveUnusedImports; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
|
||
public class PalantirJavaFormatFormatterFunc implements FormatterFunc { | ||
|
||
private final Formatter formatter; | ||
|
||
public PalantirJavaFormatFormatterFunc() { | ||
formatter = Formatter.createFormatter(JavaFormatterOptions.builder() | ||
.style(JavaFormatterOptions.Style.PALANTIR) | ||
.build()); | ||
} | ||
|
||
@Override | ||
public String apply(String input) throws Exception { | ||
String source = input; | ||
source = ImportOrderer.reorderImports(source, JavaFormatterOptions.Style.PALANTIR); | ||
source = RemoveUnusedImports.removeUnusedImports(source); | ||
return formatter.formatSource(source); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PalantirJavaFormatFormatterFunc{formatter=" + formatter + '}'; | ||
} | ||
} |
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
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
48 changes: 48 additions & 0 deletions
48
...-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.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,48 @@ | ||
/* | ||
* Copyright 2022 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.jupiter.api.Test; | ||
|
||
class PalantirJavaFormatIntegrationTest extends GradleIntegrationHarness { | ||
@Test | ||
void integration() throws IOException { | ||
setFile("build.gradle").toLines( | ||
"plugins {", | ||
" id 'com.diffplug.spotless'", | ||
"}", | ||
"repositories { mavenCentral() }", | ||
"", | ||
"spotless {", | ||
" java {", | ||
" target file('test.java')", | ||
" palantirJavaFormat('1.1.0')", | ||
" }", | ||
"}"); | ||
|
||
setFile("test.java").toResource("java/palantirjavaformat/JavaCodeUnformatted.test"); | ||
gradleRunner().withArguments("spotlessApply").build(); | ||
assertFile("test.java").sameAsResource("java/palantirjavaformat/JavaCodeFormatted.test"); | ||
|
||
checkRunsThenUpToDate(); | ||
replace("build.gradle", | ||
"palantirJavaFormat('1.1.0')", | ||
"palantirJavaFormat('1.0.1')"); | ||
checkRunsThenUpToDate(); | ||
} | ||
} |
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
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
Oops, something went wrong.