Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: palantir-java-format now reflows strings #982

Merged
merged 5 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-982.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: For users who opted into palantir-java-format, we now reflow strings
and reorder imports.
links:
- https://github.com/palantir/gradle-baseline/pull/982
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@

import com.diffplug.gradle.spotless.SpotlessExtension;
import com.diffplug.spotless.FormatterFunc;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Range;
import com.palantir.javaformat.java.FormatterService;
import com.palantir.javaformat.java.Formatter;
import com.palantir.javaformat.java.JavaFormatterOptions;
import com.palantir.javaformat.java.JavaFormatterOptions.Style;
import com.palantir.javaformat.java.JavaOutput;
import com.palantir.javaformat.java.Replacement;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ServiceLoader;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
Expand Down Expand Up @@ -128,14 +122,10 @@ static Path eclipseConfigFile(Project project) {
private static class PalantirJavaFormatterFunc implements FormatterFunc {
private static final JavaFormatterOptions OPTIONS =
JavaFormatterOptions.builder().style(Style.PALANTIR).build();
private final FormatterService formatterService =
Iterables.getOnlyElement(ServiceLoader.load(FormatterService.class));

@Override
public String apply(String input) throws Exception {
ImmutableList<Replacement> replacements = formatterService.getFormatReplacements(
OPTIONS, input, ImmutableList.of(Range.closedOpen(0, input.length())));
return JavaOutput.applyReplacements(input, replacements);
return Formatter.createFormatter(OPTIONS).formatSourceAndFixImports(input);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so we're just bypassing the palantir-java-format-api, but I guess we were already because we used the JavaOutput class.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest {

def 'eclipse formatter integration test'() {
def inputDir = new File("src/test/resources/com/palantir/baseline/formatter-in")
def expectedDir = new File("src/test/resources/com/palantir/baseline/formatter-expected")
def expectedDir = new File("src/test/resources/com/palantir/baseline/eclipse-formatter-expected")

def testedDir = new File(projectDir, "src/main/java")
FileUtils.copyDirectory(inputDir, testedDir)
Expand Down Expand Up @@ -133,36 +133,6 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest {
assertThatFilesAreTheSame(testedDir, expectedDir)
}

def 'eclipse formatter googlejavaformat test cases'() {
def excludedFiles = [
"B19996259.java", // this causes an OOM
]

def inputDir = new File("src/test/resources/com/palantir/baseline/googlejavaformat-in")
def expectedDir = new File("src/test/resources/com/palantir/baseline/googlejavaformat-expected")

def testedDir = new File(projectDir, "src/main/java")
FileUtils.copyDirectory(inputDir, testedDir, new NotFileFilter(new NameFileFilter(excludedFiles)))

buildFile << """
plugins {
id 'java'
id 'com.palantir.baseline-format'
}
""".stripIndent()
file('gradle.properties') << """
com.palantir.baseline-format.eclipse=true
""".stripIndent()

when:
BuildResult result = with(':format').build()
result.task(":format").outcome == TaskOutcome.SUCCESS
result.task(":spotlessApply").outcome == TaskOutcome.SUCCESS

then:
assertThatFilesAreTheSame(testedDir, expectedDir)
}

private static void assertThatFilesAreTheSame(File outputDir, File expectedDir) throws IOException {
Collection<File> files = listJavaFilesRecursively(outputDir)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class StringReflowing {
byte[] bytes =
("one long incredibly unbroken sentence moving from topic to topic so that no-one had a chance to interrupt at all")
.getBytes();

String foo = callRandomMethod(
"one long incredibly unbroken sentence moving from topic to topic so that no-one had a chance to interrupt at all");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class StringReflowing {
byte[] bytes =
("one long incredibly unbroken sentence moving from topic to topic so that no-one had a chance to interrupt at all").getBytes();

String foo = callRandomMethod("one long incredibly unbroken sentence moving from topic to topic so that no-one had a chance to interrupt at all");
}
Loading