From 4cceef8ca649f697ee00f2783c200a2f4be5fbcf Mon Sep 17 00:00:00 2001 From: iamdanfox Date: Tue, 22 Oct 2019 17:20:27 +0100 Subject: [PATCH] Standalone gradle plugins (#48) The standalone `com.palantir.java-format` plugin provides a `formatDiff` task. IntelliJ setup has been moved to a `com.palantir.java-format-idea` plugin. --- build.gradle | 2 +- changelog/@unreleased/pr-48.v2.yml | 6 ++ gradle-palantir-java-format/build.gradle | 34 ++++++++- .../ConfigureExternalDependenciesXml.java | 16 ++++ .../gradle/ConfigureJavaFormatterXml.groovy | 16 ++++ .../ConfigurePalantirJavaFormatXml.java | 16 ++++ ...java => PalantirJavaFormatIdeaPlugin.java} | 36 +++------ .../gradle/UpdateIntellijXmlTask.java | 16 ++++ .../javaformat/gradle}/FormatDiff.java | 57 +++++++++++--- .../gradle/JavaFormatExtension.java | 49 +++++++++--- .../javaformat/gradle/LineOffsetIterator.java | 71 ++++++++++++++++++ .../gradle/PalantirJavaFormatPlugin.java | 53 +++++++++++++ .../PalantirJavaFormatProviderPlugin.java | 50 +++++++++++++ .../ConfigureJavaFormatterXmlTest.groovy | 17 ++++- .../javaformat/gradle}/FormatDiffTest.java | 9 ++- ...> PalantirJavaFormatIdeaPluginTest.groovy} | 25 +++++-- .../PalantirJavaFormatPluginTest.groovy | 75 +++++++++++++++++++ .../java/FormatDiffCliTest/example1.patch | 0 palantir-java-format/build.gradle | 4 +- .../java/com/palantir/javaformat/Utils.java | 64 ++++++++++++++++ .../javaformat/java/FormatFileCallable.java | 3 +- .../palantir/javaformat/java/Formatter.java | 25 +------ .../palantir/javaformat/java/JavaOutput.java | 15 ---- .../com/palantir/javaformat/NewlinesTest.java | 55 +++++++------- .../java/LineRangesToCharRangesTest.java | 5 +- settings.gradle | 6 +- versions.lock | 7 +- 27 files changed, 597 insertions(+), 135 deletions(-) create mode 100644 changelog/@unreleased/pr-48.v2.yml rename gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/{JavaFormatPlugin.java => PalantirJavaFormatIdeaPlugin.java} (68%) rename {palantir-java-format/src/main/java/com/palantir/javaformat/java => gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle}/FormatDiff.java (68%) create mode 100644 gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/LineOffsetIterator.java create mode 100644 gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatPlugin.java create mode 100644 gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatProviderPlugin.java rename {palantir-java-format/src/test/java/com/palantir/javaformat/java => gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle}/FormatDiffTest.java (94%) rename gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/{JavaFormatPluginTest.groovy => PalantirJavaFormatIdeaPluginTest.groovy} (57%) create mode 100644 gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatPluginTest.groovy rename {palantir-java-format => gradle-palantir-java-format}/src/test/resources/com/palantir/javaformat/java/FormatDiffCliTest/example1.patch (100%) create mode 100644 palantir-java-format/src/main/java/com/palantir/javaformat/Utils.java diff --git a/build.gradle b/build.gradle index 2ae346390..359f26801 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ apply plugin: 'com.palantir.git-version' apply plugin: 'com.palantir.baseline' apply plugin: 'com.palantir.consistent-versions' -version = gitVersion() +version System.env.CIRCLE_TAG ?: gitVersion() allprojects { group = 'com.palantir.javaformat' diff --git a/changelog/@unreleased/pr-48.v2.yml b/changelog/@unreleased/pr-48.v2.yml new file mode 100644 index 000000000..71bf72fd1 --- /dev/null +++ b/changelog/@unreleased/pr-48.v2.yml @@ -0,0 +1,6 @@ +type: break +break: + description: The standalone `com.palantir.java-format` plugin provides a `formatDiff` + task. IntelliJ setup has been moved to a `com.palantir.java-format-idea` plugin. + links: + - https://github.com/palantir/palantir-java-format/pull/48 diff --git a/gradle-palantir-java-format/build.gradle b/gradle-palantir-java-format/build.gradle index 8f7956944..77bed29bc 100644 --- a/gradle-palantir-java-format/build.gradle +++ b/gradle-palantir-java-format/build.gradle @@ -6,8 +6,13 @@ dependencies { implementation gradleApi() implementation 'com.google.guava:guava' implementation 'gradle.plugin.org.jetbrains.gradle.plugin.idea-ext:gradle-idea-ext' + implementation project(':palantir-java-format-spi') testImplementation 'com.netflix.nebula:nebula-test' + testImplementation 'org.junit.jupiter:junit-jupiter' + testImplementation 'org.junit.vintage:junit-vintage-engine' + testImplementation 'org.assertj:assertj-core' + testImplementation project(':palantir-java-format') } gradlePlugin { @@ -15,9 +20,19 @@ gradlePlugin { plugins { palantirJavaFormat { id = 'com.palantir.java-format' - implementationClass = 'com.palantir.javaformat.gradle.JavaFormatPlugin' + implementationClass = 'com.palantir.javaformat.gradle.PalantirJavaFormatPlugin' + description = 'A modern, lambda-friendly, 120 character Java formatter.' + } + palantirJavaFormatIdea { + id = 'com.palantir.java-format-idea' + implementationClass = 'com.palantir.javaformat.gradle.PalantirJavaFormatIdeaPlugin' description = 'Plugin to configure the PalantirJavaFormat IDEA plugin based on an optional implementation version of the formatter.' } + palantirJavaFormatProvider { + id = 'com.palantir.java-format-provider' + implementationClass = 'com.palantir.javaformat.gradle.PalantirJavaFormatProviderPlugin' + description = 'Exposes a configuration containing the palantir-java-format jarss' + } } } @@ -26,3 +41,20 @@ idea { sourceDirs += sourceSets.main.groovy.srcDirs } } + +configurations { + impl +} + +dependencies { + impl project(':palantir-java-format') +} + +task writeImplClasspath { + dependsOn configurations.impl + doLast { + file("$buildDir/impl.classpath").text = configurations.impl.asPath + } +} + +test.dependsOn tasks.writeImplClasspath diff --git a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureExternalDependenciesXml.java b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureExternalDependenciesXml.java index 72cd43b08..a14526da2 100644 --- a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureExternalDependenciesXml.java +++ b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureExternalDependenciesXml.java @@ -1,3 +1,19 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat.gradle; import groovy.util.Node; diff --git a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXml.groovy b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXml.groovy index d3f75f3f7..e4cc96614 100644 --- a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXml.groovy +++ b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXml.groovy @@ -1,3 +1,19 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat.gradle class ConfigureJavaFormatterXml { diff --git a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigurePalantirJavaFormatXml.java b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigurePalantirJavaFormatXml.java index 6b41e5c76..4e8f17410 100644 --- a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigurePalantirJavaFormatXml.java +++ b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigurePalantirJavaFormatXml.java @@ -1,3 +1,19 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat.gradle; import groovy.util.Node; diff --git a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/JavaFormatPlugin.java b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPlugin.java similarity index 68% rename from gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/JavaFormatPlugin.java rename to gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPlugin.java index 1d56e0452..8fd3e2319 100644 --- a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/JavaFormatPlugin.java +++ b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPlugin.java @@ -17,7 +17,6 @@ package com.palantir.javaformat.gradle; import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableMap; import java.io.File; import java.net.URI; import java.util.List; @@ -31,37 +30,22 @@ import org.gradle.plugins.ide.idea.model.IdeaModel; import org.jetbrains.gradle.ext.TaskTriggersConfig; -public class JavaFormatPlugin implements Plugin { - - private static final String EXTENSION_NAME = "palantirJavaFormat"; +public final class PalantirJavaFormatIdeaPlugin implements Plugin { @Override - public void apply(Project project) { + public void apply(Project rootProject) { Preconditions.checkState( - project == project.getRootProject(), "May only apply com.palantir.java-format to the root project"); - - JavaFormatExtension extension = - project.getExtensions().create(EXTENSION_NAME, JavaFormatExtension.class, project); + rootProject == rootProject.getRootProject(), + "May only apply com.palantir.java-format-idea to the root project"); - Configuration implConfiguration = project.getConfigurations().create("palantirJavaFormat", conf -> { - conf.setDescription("Internal configuration for resolving the palantirJavaFormat implementation"); - conf.setVisible(false); - conf.setCanBeConsumed(false); - // Using addLater instead of afterEvaluate, in order to delay reading the extension until after the user - // has configured it. - conf.defaultDependencies(deps -> deps.addLater(project.provider(() -> { - String version = extension.getImplementationVersion().get(); + rootProject.getPlugins().apply(PalantirJavaFormatProviderPlugin.class); - return project.getDependencies().create(ImmutableMap.of( - "group", "com.palantir.javaformat", - "name", "palantir-java-format", - "version", version)); - }))); - }); + rootProject.getPluginManager().withPlugin("idea", ideaPlugin -> { + Configuration implConfiguration = + rootProject.getConfigurations().getByName(PalantirJavaFormatProviderPlugin.CONFIGURATION_NAME); - project.getPluginManager().withPlugin("idea", ideaPlugin -> { - configureLegacyIdea(project, implConfiguration); - configureIntelliJImport(project, implConfiguration); + configureLegacyIdea(rootProject, implConfiguration); + configureIntelliJImport(rootProject, implConfiguration); }); } diff --git a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/UpdateIntellijXmlTask.java b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/UpdateIntellijXmlTask.java index 3b8667f0d..cc0b74c92 100644 --- a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/UpdateIntellijXmlTask.java +++ b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/UpdateIntellijXmlTask.java @@ -1,3 +1,19 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat.gradle; import com.google.common.collect.ImmutableMap; diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/FormatDiff.java b/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/FormatDiff.java similarity index 68% rename from palantir-java-format/src/main/java/com/palantir/javaformat/java/FormatDiff.java rename to gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/FormatDiff.java index 2bb06570f..76f7dd47f 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/FormatDiff.java +++ b/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/FormatDiff.java @@ -14,30 +14,39 @@ * limitations under the License. */ -package com.palantir.javaformat.java; +package com.palantir.javaformat.gradle; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Comparator.comparing; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterators; import com.google.common.collect.Range; import com.google.common.collect.RangeSet; import com.google.common.collect.Streams; import com.google.common.collect.TreeRangeSet; import com.google.common.io.ByteStreams; +import com.palantir.javaformat.java.FormatterException; +import com.palantir.javaformat.java.FormatterService; +import com.palantir.javaformat.java.Replacement; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Stream; -public final class FormatDiff { +final class FormatDiff { // each section in the git diff output starts like this private static final Pattern SEPARATOR = Pattern.compile("diff --git"); @@ -48,10 +57,8 @@ public final class FormatDiff { private static final Pattern HUNK = Pattern.compile("^@@.*\\+(?\\d+)(,(?\\d+))?", Pattern.MULTILINE); - public static void formatDiff(Path dirToFormat) throws IOException, InterruptedException { - Formatter formatter = Formatter.createFormatter( - JavaFormatterOptions.builder().style(JavaFormatterOptions.Style.PALANTIR).build()); - + public static void formatDiff(Path dirToFormat, FormatterService formatter) + throws IOException, InterruptedException { String gitOutput = gitDiff(dirToFormat); Path gitTopLevelDir = gitTopLevelDir(dirToFormat); @@ -86,7 +93,7 @@ static Stream parseGitDiffOutput(String gitOutput) { }); } - private static void format(Formatter formatter, SingleFileDiff diff) { + private static void format(FormatterService formatter, SingleFileDiff diff) { String input; try { input = new String(Files.readAllBytes(diff.path), UTF_8); @@ -96,11 +103,12 @@ private static void format(Formatter formatter, SingleFileDiff diff) { return; } - RangeSet charRanges = Formatter.lineRangesToCharRanges(input, diff.lineRanges); + RangeSet charRanges = lineRangesToCharRanges(input, diff.lineRanges); try { System.err.println("Formatting " + diff.path); - String output = formatter.formatSource(input, charRanges.asRanges()); + ImmutableList replacements = formatter.getFormatReplacements(input, charRanges.asRanges()); + String output = applyReplacements(input, replacements); Files.write(diff.path, output.getBytes(UTF_8)); } catch (IOException | FormatterException e) { System.err.println("Failed to format file " + diff.path); @@ -128,6 +136,37 @@ private static String gitCommand(Path dir, String... args) throws IOException, I return new String(baos.toByteArray(), UTF_8).trim(); } + private static String applyReplacements(String input, Collection replacementsCollection) { + List replacements = new ArrayList<>(replacementsCollection); + replacements.sort(comparing((Replacement r) -> r.getReplaceRange().lowerEndpoint()).reversed()); + StringBuilder writer = new StringBuilder(input); + for (Replacement replacement : replacements) { + writer.replace( + replacement.getReplaceRange().lowerEndpoint(), + replacement.getReplaceRange().upperEndpoint(), + replacement.getReplacementString()); + } + return writer.toString(); + } + + /** Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges. */ + private static RangeSet lineRangesToCharRanges(String input, RangeSet lineRanges) { + List lines = new ArrayList<>(); + Iterators.addAll(lines, new LineOffsetIterator(input)); + lines.add(input.length() + 1); + + final RangeSet characterRanges = TreeRangeSet.create(); + for (Range lineRange : lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) { + int lineStart = lines.get(lineRange.lowerEndpoint()); + // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines + // as empty ranges is convenient. + int lineEnd = lines.get(lineRange.upperEndpoint()) - 1; + Range range = Range.closedOpen(lineStart, lineEnd); + characterRanges.add(range); + } + return characterRanges; + } + // TODO(dfox): replace this with immutables public static class SingleFileDiff { private final Path path; diff --git a/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/JavaFormatExtension.java b/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/JavaFormatExtension.java index 898249d41..0a50b50a0 100644 --- a/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/JavaFormatExtension.java +++ b/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/JavaFormatExtension.java @@ -1,19 +1,48 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat.gradle; -import org.gradle.api.Project; -import org.gradle.api.provider.Property; +import com.google.common.collect.Iterables; +import com.palantir.javaformat.java.FormatterService; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ServiceLoader; +import org.gradle.api.artifacts.Configuration; public class JavaFormatExtension { - private final Property implementationVersion; - private static final String IMPLEMENTATION_VERSION = - JavaFormatExtension.class.getPackage().getImplementationVersion(); + private final Configuration configuration; - public JavaFormatExtension(Project project) { - implementationVersion = project.getObjects().property(String.class); - implementationVersion.set(IMPLEMENTATION_VERSION); + public JavaFormatExtension(Configuration configuration) { + this.configuration = configuration; } - public final Property getImplementationVersion() { - return implementationVersion; + public FormatterService serviceLoad() { + URL[] jarUris = configuration.getFiles().stream() + .map(file -> { + try { + return file.toURI().toURL(); + } catch (MalformedURLException e) { + throw new RuntimeException("Unable to convert URI to URL: " + file, e); + } + }) + .toArray(URL[]::new); + + ClassLoader classLoader = new URLClassLoader(jarUris, FormatterService.class.getClassLoader()); + return Iterables.getOnlyElement(ServiceLoader.load(FormatterService.class, classLoader)); } } diff --git a/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/LineOffsetIterator.java b/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/LineOffsetIterator.java new file mode 100644 index 000000000..463d743b2 --- /dev/null +++ b/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/LineOffsetIterator.java @@ -0,0 +1,71 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat.gradle; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +final class LineOffsetIterator implements Iterator { + + private int curr = 0; + private int idx = 0; + private final String input; + + public LineOffsetIterator(String input) { + this.input = input; + } + + @Override + public boolean hasNext() { + return curr != -1; + } + + @Override + public Integer next() { + if (curr == -1) { + throw new NoSuchElementException(); + } + int result = curr; + advance(); + return result; + } + + private void advance() { + for (; idx < input.length(); idx++) { + char c = input.charAt(idx); + switch (c) { + case '\r': + if (idx + 1 < input.length() && input.charAt(idx + 1) == '\n') { + idx++; + } + // falls through + case '\n': + idx++; + curr = idx; + return; + default: + break; + } + } + curr = -1; + } + + @Override + public void remove() { + throw new UnsupportedOperationException("remove"); + } +} diff --git a/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatPlugin.java b/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatPlugin.java new file mode 100644 index 000000000..895eb064d --- /dev/null +++ b/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatPlugin.java @@ -0,0 +1,53 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat.gradle; + +import com.palantir.javaformat.java.FormatterService; +import java.io.IOException; +import org.gradle.api.DefaultTask; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.tasks.TaskAction; + +public final class PalantirJavaFormatPlugin implements Plugin { + + @Override + public void apply(Project project) { + project.getRootProject().getPlugins().apply(PalantirJavaFormatProviderPlugin.class); + project.getPlugins().withId("java", p -> { + project.getTasks().register("formatDiff", FormatDiffTask.class); + + // TODO(dfox): in the future we may want to offer a simple 'format' task so people don't need to use + // spotless to try out our formatter + }); + } + + public static class FormatDiffTask extends DefaultTask { + public FormatDiffTask() { + setDescription("Format only chunks of files that appear in git diff"); + setGroup("Formatting"); + } + + @TaskAction + public final void formatDiff() throws IOException, InterruptedException { + JavaFormatExtension extension = + getProject().getRootProject().getExtensions().getByType(JavaFormatExtension.class); + FormatterService formatterService = extension.serviceLoad(); + FormatDiff.formatDiff(getProject().getProjectDir().toPath(), formatterService); + } + } +} diff --git a/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatProviderPlugin.java b/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatProviderPlugin.java new file mode 100644 index 000000000..6fe503090 --- /dev/null +++ b/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatProviderPlugin.java @@ -0,0 +1,50 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat.gradle; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.artifacts.Configuration; + +public final class PalantirJavaFormatProviderPlugin implements Plugin { + + static final String CONFIGURATION_NAME = "palantirJavaFormat"; + + @Override + public void apply(Project rootProject) { + Preconditions.checkState( + rootProject == rootProject.getRootProject(), + "May only apply com.palantir.java-format-provider to the root project"); + + Configuration configuration = rootProject.getConfigurations().create(CONFIGURATION_NAME, conf -> { + conf.setDescription("Internal configuration for resolving the palantir-java-format implementation"); + conf.setVisible(false); + conf.setCanBeConsumed(false); + + conf.defaultDependencies(deps -> { + deps.add(rootProject.getDependencies().create(ImmutableMap.of( + "group", "com.palantir.javaformat", + "name", "palantir-java-format", + "version", JavaFormatExtension.class.getPackage().getImplementationVersion()))); + }); + }); + + rootProject.getExtensions().create("palantirJavaFormat", JavaFormatExtension.class, configuration); + } +} diff --git a/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXmlTest.groovy b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXmlTest.groovy index db2add4ce..4037b946e 100644 --- a/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXmlTest.groovy +++ b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXmlTest.groovy @@ -1,5 +1,20 @@ -package com.palantir.javaformat.gradle +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat.gradle import spock.lang.Specification diff --git a/palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatDiffTest.java b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/FormatDiffTest.java similarity index 94% rename from palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatDiffTest.java rename to gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/FormatDiffTest.java index 069ddbd1e..ca74c763a 100644 --- a/palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatDiffTest.java +++ b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/FormatDiffTest.java @@ -14,15 +14,15 @@ * limitations under the License. */ -package com.palantir.javaformat.java; +package com.palantir.javaformat.gradle; import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.io.ByteStreams; +import com.palantir.javaformat.java.FormatterServiceImpl; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -32,6 +32,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -47,7 +48,7 @@ void parsing_git_diff_output_works() throws IOException { List strings = FormatDiff.parseGitDiffOutput(example1).map(FormatDiff.SingleFileDiff::toString).collect( Collectors.toList()); - assertEquals( + Assertions.assertEquals( ImmutableList.of( "SingleFileDiff{path=build.gradle, lineRanges=[[24..25), [29..30)]}", "SingleFileDiff{path=tracing/src/test/java/com/palantir/tracing/TracersTest.java, " @@ -73,7 +74,7 @@ void reformat_a_subpath_of_a_git_directory_for_only_changed_lines() throws IOExc runCommandInRepo("git", "add", "-N", "."); - FormatDiff.formatDiff(subdir); + FormatDiff.formatDiff(subdir, new FormatterServiceImpl()); assertThat(reformatMe).hasContent("class ReformatMe {}"); assertThat(dontTouchMe).hasContent(" class DontTouchMe {}"); diff --git a/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/JavaFormatPluginTest.groovy b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPluginTest.groovy similarity index 57% rename from gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/JavaFormatPluginTest.groovy rename to gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPluginTest.groovy index c64c7d19f..9e45db5c2 100644 --- a/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/JavaFormatPluginTest.groovy +++ b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPluginTest.groovy @@ -1,10 +1,24 @@ -package com.palantir.javaformat.gradle +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat.gradle import nebula.test.IntegrationTestKitSpec -class JavaFormatPluginTest extends IntegrationTestKitSpec { - private static def PLUGIN_ID = "com.palantir.java-format" +class PalantirJavaFormatIdeaPluginTest extends IntegrationTestKitSpec { void setup() { // Note: this deprecation is due to gradle-idea-ext-plugin 0.5, however they fixed the issue in master, which @@ -14,7 +28,7 @@ class JavaFormatPluginTest extends IntegrationTestKitSpec { buildFile << """ plugins { - id "${PLUGIN_ID}" + id 'com.palantir.java-format-idea' } apply plugin: 'idea' @@ -25,9 +39,6 @@ class JavaFormatPluginTest extends IntegrationTestKitSpec { } def "idea_configuresIpr"() { - buildFile << """ - """.stripIndent() - when: runTasks('idea') diff --git a/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatPluginTest.groovy b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatPluginTest.groovy new file mode 100644 index 000000000..8205fa92c --- /dev/null +++ b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatPluginTest.groovy @@ -0,0 +1,75 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat.gradle + +import nebula.test.IntegrationTestKitSpec + +class PalantirJavaFormatPluginTest extends IntegrationTestKitSpec { + + /** ./gradlew writeImplClasspath generates this file. */ + private static final CLASSPATH_FILE = new File("build/impl.classpath").absolutePath + + void setup() { + buildFile << """ + plugins { + id 'java' + id 'com.palantir.java-format' + } + + dependencies { + palantirJavaFormat files(file("${CLASSPATH_FILE}").text.split(':')) + } + apply plugin: 'idea' + """.stripIndent() + } + + def 'formatDiff updates only lines changed in git diff'() { + when: + "git init".execute(Collections.emptyList(), projectDir).waitFor() + "git config user.name Foo".execute(Collections.emptyList(), projectDir).waitFor() + "git config user.email foo@bar.com".execute(Collections.emptyList(), projectDir).waitFor() + + file('src/main/java/Main.java') << ''' + class Main { + public static void crazyExistingFormatting ( String... args) { + + } + } + '''.stripIndent() + + "git add .".execute(Collections.emptyList(), projectDir).waitFor() + "git commit -m Commit".execute(Collections.emptyList(), projectDir).waitFor() + + file('src/main/java/Main.java').text = ''' + class Main { + public static void crazyExistingFormatting ( String... args) { + System.out.println("Reformat me please"); + } + } + '''.stripIndent() + + then: + runTasks('formatDiff') + file('src/main/java/Main.java').text == ''' + class Main { + public static void crazyExistingFormatting ( String... args) { + System.out.println("Reformat me please"); + } + } + '''.stripIndent() + } +} diff --git a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/FormatDiffCliTest/example1.patch b/gradle-palantir-java-format/src/test/resources/com/palantir/javaformat/java/FormatDiffCliTest/example1.patch similarity index 100% rename from palantir-java-format/src/test/resources/com/palantir/javaformat/java/FormatDiffCliTest/example1.patch rename to gradle-palantir-java-format/src/test/resources/com/palantir/javaformat/java/FormatDiffCliTest/example1.patch diff --git a/palantir-java-format/build.gradle b/palantir-java-format/build.gradle index 785ab1725..3ccc668be 100644 --- a/palantir-java-format/build.gradle +++ b/palantir-java-format/build.gradle @@ -3,7 +3,7 @@ apply from: rootProject.file('gradle/publish-jar.gradle') mainClassName = 'com.palantir.javaformat.java.Main' -description = 'Google Java Format' +description = 'Palantir Java Format' dependencies { api project(':palantir-java-format-spi') @@ -37,4 +37,4 @@ tasks.test { // Run all classes and tests in parallel // https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution systemProperty 'junit.jupiter.execution.parallel.mode.default', 'concurrent' -} \ No newline at end of file +} diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/Utils.java b/palantir-java-format/src/main/java/com/palantir/javaformat/Utils.java new file mode 100644 index 000000000..e00fa07e1 --- /dev/null +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/Utils.java @@ -0,0 +1,64 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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.palantir.javaformat; + +import static java.util.Comparator.comparing; + +import com.google.common.collect.Iterators; +import com.google.common.collect.Range; +import com.google.common.collect.RangeSet; +import com.google.common.collect.TreeRangeSet; +import com.palantir.javaformat.java.Replacement; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public final class Utils { + + private Utils() {} + + public static String applyReplacements(String input, Collection replacementsCollection) { + List replacements = new ArrayList<>(replacementsCollection); + replacements.sort(comparing((Replacement r) -> r.getReplaceRange().lowerEndpoint()).reversed()); + StringBuilder writer = new StringBuilder(input); + for (Replacement replacement : replacements) { + writer.replace( + replacement.getReplaceRange().lowerEndpoint(), + replacement.getReplaceRange().upperEndpoint(), + replacement.getReplacementString()); + } + return writer.toString(); + } + + /** Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges. */ + public static RangeSet lineRangesToCharRanges(String input, RangeSet lineRanges) { + List lines = new ArrayList<>(); + Iterators.addAll(lines, Newlines.lineOffsetIterator(input)); + lines.add(input.length() + 1); + + final RangeSet characterRanges = TreeRangeSet.create(); + for (Range lineRange : lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) { + int lineStart = lines.get(lineRange.lowerEndpoint()); + // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines + // as empty ranges is convenient. + int lineEnd = lines.get(lineRange.upperEndpoint()) - 1; + Range range = Range.closedOpen(lineStart, lineEnd); + characterRanges.add(range); + } + return characterRanges; + } +} diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java index 7e6c3af7a..9aea99a82 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java @@ -17,6 +17,7 @@ import com.google.common.collect.Range; import com.google.common.collect.RangeSet; import com.google.common.collect.TreeRangeSet; +import com.palantir.javaformat.Utils; import java.util.concurrent.Callable; /** Encapsulates information about a file to be formatted, including which parts of the file to format. */ @@ -64,7 +65,7 @@ private RangeSet characterRanges(String input) { return characterRanges; } - characterRanges.addAll(Formatter.lineRangesToCharRanges(input, parameters.lines())); + characterRanges.addAll(Utils.lineRangesToCharRanges(input, parameters.lines())); for (int i = 0; i < parameters.offsets().size(); i++) { Integer length = parameters.lengths().get(i); diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java index 3a8657185..77faeecba 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java @@ -18,10 +18,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import com.google.common.collect.Iterators; import com.google.common.collect.Range; import com.google.common.collect.RangeSet; -import com.google.common.collect.TreeRangeSet; import com.google.common.io.CharSink; import com.google.common.io.CharSource; import com.google.errorprone.annotations.Immutable; @@ -29,15 +27,14 @@ import com.palantir.javaformat.Newlines; import com.palantir.javaformat.Op; import com.palantir.javaformat.OpsBuilder; +import com.palantir.javaformat.Utils; import com.palantir.javaformat.doc.Doc; import com.palantir.javaformat.doc.DocBuilder; import com.palantir.javaformat.doc.State; import java.io.IOError; import java.io.IOException; import java.net.URI; -import java.util.ArrayList; import java.util.Collection; -import java.util.List; import org.openjdk.javax.tools.Diagnostic; import org.openjdk.javax.tools.DiagnosticCollector; import org.openjdk.javax.tools.DiagnosticListener; @@ -227,7 +224,7 @@ public String formatSourceAndFixImports(String input) throws FormatterException * @throws FormatterException if the input string cannot be parsed */ public String formatSource(String input, Collection> characterRanges) throws FormatterException { - return JavaOutput.applyReplacements(input, getFormatReplacements(input, characterRanges)); + return Utils.applyReplacements(input, getFormatReplacements(input, characterRanges)); } /** @@ -258,22 +255,4 @@ public ImmutableList getFormatReplacements(String input, Collection RangeSet tokenRangeSet = javaInput.characterRangesToTokenRanges(characterRanges); return javaOutput.getFormatReplacements(tokenRangeSet); } - - /** Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges. */ - public static RangeSet lineRangesToCharRanges(String input, RangeSet lineRanges) { - List lines = new ArrayList<>(); - Iterators.addAll(lines, Newlines.lineOffsetIterator(input)); - lines.add(input.length() + 1); - - final RangeSet characterRanges = TreeRangeSet.create(); - for (Range lineRange : lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) { - int lineStart = lines.get(lineRange.lowerEndpoint()); - // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines - // as empty ranges is convenient. - int lineEnd = lines.get(lineRange.upperEndpoint()) - 1; - Range range = Range.closedOpen(lineStart, lineEnd); - characterRanges.add(range); - } - return characterRanges; - } } diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaOutput.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaOutput.java index bd1eca4c4..019600c7d 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaOutput.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaOutput.java @@ -14,8 +14,6 @@ package com.palantir.javaformat.java; -import static java.util.Comparator.comparing; - import com.google.common.base.CharMatcher; import com.google.common.base.MoreObjects; import com.google.common.collect.DiscreteDomain; @@ -330,19 +328,6 @@ private Range expandToBreakableRegions(Range iRange) { return Range.closedOpen(loTok, hiTok + 1); } - public static String applyReplacements(String input, List replacements) { - replacements = new ArrayList<>(replacements); - replacements.sort(comparing((Replacement r) -> r.getReplaceRange().lowerEndpoint()).reversed()); - StringBuilder writer = new StringBuilder(input); - for (Replacement replacement : replacements) { - writer.replace( - replacement.getReplaceRange().lowerEndpoint(), - replacement.getReplaceRange().upperEndpoint(), - replacement.getReplacementString()); - } - return writer.toString(); - } - /** The earliest position of any Tok in the Token, including leading whitespace. */ public static int startPosition(Token token) { int min = token.getTok().getPosition(); diff --git a/palantir-java-format/src/test/java/com/palantir/javaformat/NewlinesTest.java b/palantir-java-format/src/test/java/com/palantir/javaformat/NewlinesTest.java index 1f18bb1b1..13a8125ab 100644 --- a/palantir-java-format/src/test/java/com/palantir/javaformat/NewlinesTest.java +++ b/palantir-java-format/src/test/java/com/palantir/javaformat/NewlinesTest.java @@ -1,51 +1,52 @@ /* - * Copyright 2016 Google Inc. + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. * - * 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 + * 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. + * 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.palantir.javaformat; -import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.fail; - import com.google.common.collect.ImmutableList; +import com.google.common.truth.Truth; import java.util.Iterator; import java.util.NoSuchElementException; +import org.junit.Assert; import org.junit.jupiter.api.Test; -/** {@link Newlines}Test */ public class NewlinesTest { @Test public void offsets() { - assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\nbar\n"))).containsExactly(0, 4, 8); - assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\nbar"))).containsExactly(0, 4); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\nbar\n"))).containsExactly(0, 4, 8); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\nbar"))).containsExactly(0, 4); - assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\rbar\r"))).containsExactly(0, 4, 8); - assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\rbar"))).containsExactly(0, 4); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\rbar\r"))).containsExactly(0, 4, 8); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\rbar"))).containsExactly(0, 4); - assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\r\nbar\r\n"))).containsExactly(0, 5, 10); - assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\r\nbar"))).containsExactly(0, 5); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\r\nbar\r\n"))).containsExactly(0, 5, 10); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\r\nbar"))).containsExactly(0, 5); } @Test public void lines() { - assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\nbar\n"))).containsExactly("foo\n", "bar\n"); - assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\nbar"))).containsExactly("foo\n", "bar"); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\nbar\n"))).containsExactly("foo\n", "bar\n"); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\nbar"))).containsExactly("foo\n", "bar"); - assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\rbar\r"))).containsExactly("foo\r", "bar\r"); - assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\rbar"))).containsExactly("foo\r", "bar"); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\rbar\r"))).containsExactly("foo\r", "bar\r"); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\rbar"))).containsExactly("foo\r", "bar"); - assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\r\nbar\r\n"))).containsExactly("foo\r\n", "bar\r\n"); - assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\r\nbar"))).containsExactly("foo\r\n", "bar"); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\r\nbar\r\n"))) + .containsExactly("foo\r\n", "bar\r\n"); + Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\r\nbar"))).containsExactly("foo\r\n", "bar"); } @Test @@ -56,7 +57,7 @@ public void terminalOffset() { it.next(); try { it.next(); - fail(); + Assert.fail(); } catch (NoSuchElementException e) { // expected } @@ -66,7 +67,7 @@ public void terminalOffset() { it.next(); try { it.next(); - fail(); + Assert.fail(); } catch (NoSuchElementException e) { // expected } @@ -79,7 +80,7 @@ public void terminalLine() { it.next(); try { it.next(); - fail(); + Assert.fail(); } catch (NoSuchElementException e) { // expected } @@ -89,7 +90,7 @@ public void terminalLine() { it.next(); try { it.next(); - fail(); + Assert.fail(); } catch (NoSuchElementException e) { // expected } diff --git a/palantir-java-format/src/test/java/com/palantir/javaformat/java/LineRangesToCharRangesTest.java b/palantir-java-format/src/test/java/com/palantir/javaformat/java/LineRangesToCharRangesTest.java index 81d35f3ec..1d8da1d42 100644 --- a/palantir-java-format/src/test/java/com/palantir/javaformat/java/LineRangesToCharRangesTest.java +++ b/palantir-java-format/src/test/java/com/palantir/javaformat/java/LineRangesToCharRangesTest.java @@ -19,12 +19,13 @@ import com.google.common.collect.Range; import com.google.common.collect.RangeSet; import com.google.common.collect.TreeRangeSet; +import com.palantir.javaformat.Utils; import java.util.Set; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; -/** Tests for {@link Formatter#lineRangesToCharRanges} */ +/** Tests for {@link Utils#lineRangesToCharRanges} */ @Execution(ExecutionMode.CONCURRENT) public class LineRangesToCharRangesTest { @@ -34,7 +35,7 @@ final Set> getCharRanges(String input, Range... ranges) for (Range range : ranges) { rangeSet.add(range); } - return Formatter.lineRangesToCharRanges(input, rangeSet).asRanges(); + return Utils.lineRangesToCharRanges(input, rangeSet).asRanges(); } @Test diff --git a/settings.gradle b/settings.gradle index 16700c851..b4dc51c24 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,9 +1,9 @@ rootProject.name = 'palantir-java-format-parent' -include ':palantir-java-format' -include ':palantir-java-format-spi' -include ':idea-plugin' include ':gradle-palantir-java-format' +include ':idea-plugin' +include ':palantir-java-format-spi' +include ':palantir-java-format' boolean isCiServer = System.getenv().containsKey('CI') buildCache { diff --git a/versions.lock b/versions.lock index 479e001ac..f1495bf03 100644 --- a/versions.lock +++ b/versions.lock @@ -27,8 +27,8 @@ com.google.truth.extensions:truth-java8-extension:0.37 (1 constraints: ef11ffe8) com.googlecode.java-diff-utils:diffutils:1.3.0 (1 constraints: 5c0a19b9) com.netflix.nebula:nebula-test:7.4.0 (1 constraints: 0d051a36) commons-io:commons-io:2.5 (1 constraints: eb0c8d0a) -junit:junit:4.12 (6 constraints: f14f0a8f) -org.apiguardian:apiguardian-api:1.1.0 (6 constraints: 18697c5a) +junit:junit:4.12 (7 constraints: 3f605391) +org.apiguardian:apiguardian-api:1.1.0 (7 constraints: 8f790357) org.assertj:assertj-core:3.13.2 (1 constraints: 3b05403b) org.checkerframework:checker-compat-qual:2.5.5 (1 constraints: 640a29b9) org.hamcrest:hamcrest-core:1.3 (1 constraints: cc05fe3f) @@ -38,7 +38,8 @@ org.junit.jupiter:junit-jupiter-engine:5.5.2 (1 constraints: 090ed63b) org.junit.jupiter:junit-jupiter-migrationsupport:5.5.2 (1 constraints: 0e051536) org.junit.jupiter:junit-jupiter-params:5.5.2 (1 constraints: 090ed63b) org.junit.platform:junit-platform-commons:1.5.2 (2 constraints: d720664a) -org.junit.platform:junit-platform-engine:1.5.2 (1 constraints: a8101eb4) +org.junit.platform:junit-platform-engine:1.5.2 (2 constraints: 23213173) +org.junit.vintage:junit-vintage-engine:5.5.0 (1 constraints: 0c051336) org.objenesis:objenesis:2.4 (1 constraints: ea0c8c0a) org.opentest4j:opentest4j:1.2.0 (2 constraints: cd205b49) org.spockframework:spock-core:1.3-groovy-2.4 (1 constraints: 7c10f3af)