Skip to content

Commit

Permalink
Excavator: Upgrades Baseline to the latest version (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
svc-excavator-bot authored and bulldozer-bot[bot] committed Dec 9, 2019
1 parent 9704e9f commit 8208808
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 75 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.netflix.nebula:nebula-publishing-plugin:14.1.1'
classpath 'com.netflix.nebula:gradle-info-plugin:5.2.0'
classpath 'com.palantir.baseline:gradle-baseline-java:2.39.0'
classpath 'com.palantir.baseline:gradle-baseline-java:2.41.0'
classpath 'gradle.plugin.org.inferred:gradle-processors:3.1.0'
classpath 'com.palantir.gradle.gitversion:gradle-git-version:0.12.2'
classpath 'com.palantir.gradle.consistentversions:gradle-consistent-versions:1.13.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,27 @@ public static void formatDiff(Path dirToFormat, FormatterService formatter)
/** Parses the filenames and edited ranges out of `git diff -U0`. */
@VisibleForTesting
static Stream<SingleFileDiff> parseGitDiffOutput(String gitOutput) {
return Streams.stream(Splitter.on(SEPARATOR).omitEmptyStrings().split(gitOutput)).flatMap(singleFileDiff -> {
Matcher filenameMatcher = FILENAME.matcher(singleFileDiff);
if (!filenameMatcher.find()) {
System.err.println("Failed to find filename");
return Stream.empty();
}
Path path = Paths.get(filenameMatcher.group("filename"));

RangeSet<Integer> lineRanges = TreeRangeSet.create();
Matcher hunk = HUNK.matcher(singleFileDiff);
while (hunk.find()) {
int firstLineOfHunk = Integer.parseInt(hunk.group("startLineOneIndexed")) - 1;
int hunkLength = Optional.ofNullable(hunk.group("numLines")).map(Integer::parseInt).orElse(1);
Range<Integer> rangeZeroIndexed = Range.closedOpen(firstLineOfHunk, firstLineOfHunk + hunkLength);
lineRanges.add(rangeZeroIndexed);
}

return Stream.of(new SingleFileDiff(path, lineRanges));
});
return Streams.stream(Splitter.on(SEPARATOR).omitEmptyStrings().split(gitOutput))
.flatMap(singleFileDiff -> {
Matcher filenameMatcher = FILENAME.matcher(singleFileDiff);
if (!filenameMatcher.find()) {
System.err.println("Failed to find filename");
return Stream.empty();
}
Path path = Paths.get(filenameMatcher.group("filename"));

RangeSet<Integer> lineRanges = TreeRangeSet.create();
Matcher hunk = HUNK.matcher(singleFileDiff);
while (hunk.find()) {
int firstLineOfHunk = Integer.parseInt(hunk.group("startLineOneIndexed")) - 1;
int hunkLength = Optional.ofNullable(hunk.group("numLines")).map(Integer::parseInt).orElse(1);
Range<Integer> rangeZeroIndexed =
Range.closedOpen(firstLineOfHunk, firstLineOfHunk + hunkLength);
lineRanges.add(rangeZeroIndexed);
}

return Stream.of(new SingleFileDiff(path, lineRanges));
});
}

private static void format(FormatterService formatter, SingleFileDiff diff) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ public void apply(Project rootProject) {
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())));
deps.add(rootProject
.getDependencies()
.create(ImmutableMap.of(
"group", "com.palantir.javaformat",
"name", "palantir-java-format",
"version", JavaFormatExtension.class.getPackage().getImplementationVersion())));
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
import org.junit.jupiter.api.io.TempDir;

class FormatDiffTest {
@TempDir Path repo;
@TempDir
Path repo;

@Test
void parsing_git_diff_output_works() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ private static URL toUrlUnchecked(URI uri) {

private static URL[] listDirAsUrlsUnchecked(Path dir) {
try (Stream<Path> list = Files.list(dir)) {
return list.map(Path::toUri).map(PalantirCodeStyleManager::toUrlUnchecked).toArray(URL[]::new);
return list.map(Path::toUri)
.map(PalantirCodeStyleManager::toUrlUnchecked)
.toArray(URL[]::new);
} catch (IOException e) {
throw new RuntimeException("Couldn't list dir: " + dir.toString(), e);
}
Expand All @@ -229,8 +231,9 @@ private static FormatterService createFormatter(Optional<List<URI>> implementati
URL[] implementationUrls = implementationClassPath
.map(implementationUris -> {
log.debug("Using palantir-java-format implementation defined by URIs: {}", implementationUris);
return implementationUris.stream().map(PalantirCodeStyleManager::toUrlUnchecked).toArray(URL[]
::new);
return implementationUris.stream()
.map(PalantirCodeStyleManager::toUrlUnchecked)
.toArray(URL[]::new);
})
.orElseGet(() -> {
// Load from the jars bundled with the plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ public String toString() {
/** A conditional function, whose value depends on whether a break was taken. */
@JsonTypeName("if")
public static final class If extends Indent {
@JsonProperty private final BreakTag condition;
@JsonProperty private final Indent thenIndent;
@JsonProperty private final Indent elseIndent;
@JsonProperty
private final BreakTag condition;

@JsonProperty
private final Indent thenIndent;

@JsonProperty
private final Indent elseIndent;

private If(BreakTag condition, Indent thenIndent, Indent elseIndent) {
this.condition = condition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ public void token(
* (for example) we're guessing at an optional token.
*/
if (realOrImaginary == Token.RealOrImaginary.REAL) {
throw new FormattingError(diagnostic(String.format(
"expected token: '%s'; generated %s instead", peekToken().orElse(null), token)));
throw new FormattingError(diagnostic(
String.format("expected token: '%s'; generated %s instead", peekToken().orElse(null), token)));
}
}
}
Expand Down Expand Up @@ -523,10 +523,12 @@ public OpsOutput build() {
if (tokBefore.isNewline()) {
newlines++;
} else if (tokBefore.isComment()) {
tokOps.put(j, Break.make(
tokBefore.isSlashSlashComment() ? FillMode.FORCED : FillMode.UNIFIED,
"",
tokenOp.getPlusIndentCommentsBefore()));
tokOps.put(
j,
Break.make(
tokBefore.isSlashSlashComment() ? FillMode.FORCED : FillMode.UNIFIED,
"",
tokenOp.getPlusIndentCommentsBefore()));
tokOps.putAll(j, makeComment(tokBefore));
space = tokBefore.isSlashStarComment();
newlines = 0;
Expand Down Expand Up @@ -566,10 +568,13 @@ public OpsOutput build() {
&& tokenOp.breakAndIndentTrailingComment()
.isPresent());
if (breakAfter) {
tokOps.put(tokAfterPos, Break.make(
FillMode.FORCED,
"",
tokenOp.breakAndIndentTrailingComment().orElse(Const.ZERO)));
tokOps.put(
tokAfterPos,
Break.make(
FillMode.FORCED,
"",
tokenOp.breakAndIndentTrailingComment()
.orElse(Const.ZERO)));
} else {
tokOps.put(tokAfterPos, SPACE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public JsonNode visitComment(Comment doc) {

@Override
public JsonNode visitToken(Token doc) {
return MAPPER.createObjectNode().put("type", "token").put("flat", doc.getFlat()).put("id", doc.id());
return MAPPER.createObjectNode()
.put("type", "token")
.put("flat", doc.getFlat())
.put("id", doc.id());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,8 @@ private Optional<State> handleBreakOnlyIfInnerLevelsThenFitOnOneLine(
if (keepIndent) {
newState = newState.withIndentIncrementedBy(getPlusIndent());
}
return Optional.of(
tryToLayOutLevelOnOneLine(
commentsHelper, maxWidth, newState, memoizedSplitsBreaks.get(), explorationNode));
return Optional.of(tryToLayOutLevelOnOneLine(
commentsHelper, maxWidth, newState, memoizedSplitsBreaks.get(), explorationNode));
}
return Optional.empty();
}
Expand Down Expand Up @@ -347,12 +346,11 @@ private static Optional<State> tryBreakLastLevel_acceptInlineChain(

// Note: computeBreaks, not computeBroken, so it can try to do this logic recursively for the
// lastLevel
return Optional.of(
explorationNode
.newChildNode(lastLevel, state)
.explore("end tryBreakLastLevel chain", state, exp ->
lastLevel.computeBreaks(commentsHelper, maxWidth, state, exp))
.markAccepted());
return Optional.of(explorationNode
.newChildNode(lastLevel, state)
.explore("end tryBreakLastLevel chain", state, exp ->
lastLevel.computeBreaks(commentsHelper, maxWidth, state, exp))
.markAccepted());
}

private static Optional<State> tryBreakLastLevel_checkInner(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,15 @@ private static String opsJson(OpsOutput opsOutput) {
json.put("type", "token");
json.put(
"beforeText",
inputToken.getToksBefore().stream().map(Input.Tok::getText).collect(Collectors.joining()));
inputToken.getToksBefore().stream()
.map(Input.Tok::getText)
.collect(Collectors.joining()));
json.put("text", inputToken.getTok().getText());
json.put(
"afterText",
inputToken.getToksAfter().stream().map(Input.Tok::getText).collect(Collectors.joining()));
inputToken.getToksAfter().stream()
.map(Input.Tok::getText)
.collect(Collectors.joining()));
json.put("hue", computeHue(token));
}
if (op instanceof Break) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,8 @@ public RangeSet<Integer> characterRangesToTokenRanges(Collection<Range<Integer>>
RangeSet<Integer> tokenRangeSet = TreeRangeSet.create();
for (Range<Integer> characterRange0 : characterRanges) {
Range<Integer> characterRange = characterRange0.canonical(DiscreteDomain.integers());
tokenRangeSet.add(characterRangeToTokenRange(characterRange.lowerEndpoint(), characterRange.upperEndpoint()
- characterRange.lowerEndpoint()));
tokenRangeSet.add(characterRangeToTokenRange(
characterRange.lowerEndpoint(), characterRange.upperEndpoint() - characterRange.lowerEndpoint()));
}
return tokenRangeSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,8 @@ private static List<Long> handleStream(List<ExpressionTree> parts) {
}

private static <T> Stream<Long> indexes(Stream<T> stream, Predicate<T> predicate) {
return Streams.mapWithIndex(stream, (x, i) -> predicate.apply(x) ? i : -1).filter(x -> x != -1);
return Streams.mapWithIndex(stream, (x, i) -> predicate.apply(x) ? i : -1)
.filter(x -> x != -1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,13 @@ private static String reflow(
}

return lines.stream()
.collect(
joining(
"\""
+ separator
+ Strings.repeat(" ", (first0 ? firstLineStartColumn + 4 : textStartColumn - 2))
+ "+ \"",
"\"",
"\""));
.collect(joining(
"\""
+ separator
+ Strings.repeat(" ", (first0 ? firstLineStartColumn + 4 : textStartColumn - 2))
+ "+ \"",
"\"",
"\""));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
@Execution(ExecutionMode.CONCURRENT)
public class CommandLineOptionsParserTest {

@TempDir public Path testFolder;
@TempDir
public Path testFolder;

@Test
public void defaults() {
Expand Down Expand Up @@ -94,8 +95,10 @@ public void lines() {
.lines()
.asRanges())
.containsExactly(
Range.closedOpen(0, 2), Range.closedOpen(3, 5), Range.closedOpen(6, 8), Range.closedOpen(
9, 11));
Range.closedOpen(0, 2),
Range.closedOpen(3, 5),
Range.closedOpen(6, 8),
Range.closedOpen(9, 11));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@

@Execution(ExecutionMode.CONCURRENT)
public class DiagnosticTest {
@TempDir public Path testFolder;
@TempDir
public Path testFolder;

private Locale backupLocale;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
@Execution(ExecutionMode.CONCURRENT)
public final class FormatterTest {

@TempDir public Path testFolder;
@TempDir
public Path testFolder;

@Test
public void testFormatAosp() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
@Execution(ExecutionMode.CONCURRENT)
public class MainTest {

@TempDir public Path testFolder;
@TempDir
public Path testFolder;

// PrintWriter instances used below are hard-coded to use system-default line separator.
private final Joiner joiner = Joiner.on(System.lineSeparator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public static List<Object[]> parameters() {
return ImmutableList.copyOf(new Object[][] {{"\n"}, {"\r"}, {"\r\n"}});
}

@TempDir public Path testFolder;
@TempDir
public Path testFolder;

private final String newline;

Expand Down Expand Up @@ -595,10 +596,9 @@ public void testGetFormatReplacementRanges() throws Exception {
assertThat(ranges).hasSize(1);
Replacement replacement = ranges.get(0);
assertThat(replacement.getReplacementString())
.isEqualTo(
lines(
"", //
" void f() {}"));
.isEqualTo(lines(
"", //
" void f() {}"));
int replaceFrom = input.indexOf("void f") - newline.length();
assertThat(replacement.getReplaceRange().lowerEndpoint()).isEqualTo(replaceFrom);
}
Expand Down

0 comments on commit 8208808

Please sign in to comment.