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

Support overlapping --lines ranges in google-java-format. #1093

Merged
merged 1 commit into from
Apr 6, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,6 +16,8 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import java.util.Optional;

/**
Expand Down Expand Up @@ -178,7 +180,7 @@ static Builder builder() {
static class Builder {

private final ImmutableList.Builder<String> files = ImmutableList.builder();
private final ImmutableRangeSet.Builder<Integer> lines = ImmutableRangeSet.builder();
private final RangeSet<Integer> lines = TreeRangeSet.create();
private final ImmutableList.Builder<Integer> offsets = ImmutableList.builder();
private final ImmutableList.Builder<Integer> lengths = ImmutableList.builder();
private boolean inPlace = false;
Expand All @@ -204,7 +206,7 @@ Builder inPlace(boolean inPlace) {
return this;
}

ImmutableRangeSet.Builder<Integer> linesBuilder() {
RangeSet<Integer> linesBuilder() {
return lines;
}

Expand Down Expand Up @@ -282,7 +284,7 @@ CommandLineOptions build() {
return new CommandLineOptions(
files.build(),
inPlace,
lines.build(),
ImmutableRangeSet.copyOf(lines),
offsets.build(),
lengths.build(),
aosp,
Expand Down
Expand Up @@ -18,8 +18,8 @@

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -157,7 +157,7 @@ private static String getValue(String flag, Iterator<String> it, String value) {
* number. Line numbers are {@code 1}-based, but are converted to the {@code 0}-based numbering
* used internally by google-java-format.
*/
private static void parseRangeSet(ImmutableRangeSet.Builder<Integer> result, String ranges) {
private static void parseRangeSet(RangeSet<Integer> result, String ranges) {
for (String range : COMMA_SPLITTER.split(ranges)) {
result.add(parseRange(range));
}
Expand Down
Expand Up @@ -56,7 +56,7 @@ final class UsageException extends Exception {
" --set-exit-if-changed",
" Return exit code 1 if there are any formatting changes.",
" --lines, -lines, --line, -line",
" Line range(s) to format, like 5:10 (1-based; default is all).",
" Line range(s) to format, e.g. the first 5 lines are 1:5 (1-based; default is all).",
" --offset, -offset",
" Character offset to format (0-based; default is all).",
" --length, -length",
Expand Down
Expand Up @@ -16,7 +16,6 @@

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
Expand Down Expand Up @@ -151,15 +150,13 @@ public void setExitIfChanged() {
.isTrue();
}

// TODO(cushon): consider handling this in the parser and reporting a more detailed error
@Test
public void illegalLines() {
try {
CommandLineOptionsParser.parse(Arrays.asList("-lines=1:1", "-lines=1:1"));
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageThat().contains("overlap");
}
public void mergedLines() {
assertThat(
CommandLineOptionsParser.parse(Arrays.asList("-lines=1:5", "-lines=2:8"))
.lines()
.asRanges())
.containsExactly(Range.closedOpen(0, 8));
}

@Test
Expand Down