Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
I think everything compiles
  • Loading branch information
iamdanfox committed Oct 22, 2019
commit 3775fac1c262ebd6fb0826743b5ee8feaa0211f8
1 change: 1 addition & 0 deletions gradle-palantir-java-format/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.junit.vintage:junit-vintage-engine'
testImplementation 'org.assertj:assertj-core'
testImplementation project(':palantir-java-format')
}

gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
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.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.Utils;
import com.palantir.javaformat.java.Formatter;
import com.palantir.javaformat.java.FormatterException;
import com.palantir.javaformat.java.JavaFormatterOptions;
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;
Expand All @@ -52,10 +53,8 @@ public final class FormatDiff {
private static final Pattern HUNK =
Pattern.compile("^@@.*\\+(?<startLineOneIndexed>\\d+)(,(?<numLines>\\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);

Expand Down Expand Up @@ -90,7 +89,7 @@ static Stream<SingleFileDiff> 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);
Expand All @@ -104,7 +103,8 @@ private static void format(Formatter formatter, SingleFileDiff diff) {

try {
System.err.println("Formatting " + diff.path);
String output = formatter.formatSource(input, charRanges.asRanges());
ImmutableList<Replacement> replacements = formatter.getFormatReplacements(input, charRanges.asRanges());
String output = Utils.applyReplacements(input, replacements);
Files.write(diff.path, output.getBytes(UTF_8));
} catch (IOException | FormatterException e) {
System.err.println("Failed to format file " + diff.path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

package com.palantir.javaformat.gradle;

import com.google.common.collect.Iterables;
import com.palantir.javaformat.java.FormatterService;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ServiceLoader;
import org.gradle.api.DefaultTask;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
Expand All @@ -43,7 +48,20 @@ public static class FormatDiffTask extends DefaultTask {

@TaskAction
public final void formatDiff() throws IOException, InterruptedException {
FormatDiff.formatDiff(getProject().getProjectDir().toPath());
URL[] jarUris = getProject()
.getRootProject()
.getConfigurations()
.getByName(PalantirJavaFormatProviderPlugin.CONFIGURATION_NAME)
.getFiles()
.stream()
.map(file -> file.toURI())
.toArray(URL[]::new);

ClassLoader classLoader = new URLClassLoader(jarUris, PalantirJavaFormatPlugin.class.getClassLoader());
FormatterService formatter =
Iterables.getOnlyElement(ServiceLoader.load(FormatterService.class, classLoader));

FormatDiff.formatDiff(getProject().getProjectDir().toPath(), formatter);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -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 {}");
Expand Down