Skip to content

Commit

Permalink
Improvement: palantir-java-format now reflows strings (#982)
Browse files Browse the repository at this point in the history
For users who opted into palantir-java-format, we now reflow strings and reorder imports.
  • Loading branch information
dansanduleac authored and bulldozer-bot[bot] committed Oct 17, 2019
1 parent c2d7979 commit 81d346a
Show file tree
Hide file tree
Showing 342 changed files with 33 additions and 11,138 deletions.
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);
}
}
}
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

0 comments on commit 81d346a

Please sign in to comment.