Skip to content

Commit 4978dca

Browse files
authored
Merge pull request #71 from openjavaformat/overlapping-lines-ranges
Merge --lines ranges that overlap instead of rejecting them
2 parents 98a80c6 + a5c9010 commit 4978dca

4 files changed

Lines changed: 22 additions & 14 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/CommandLineOptions.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import com.google.common.collect.ImmutableList;
1818
import com.google.common.collect.ImmutableRangeSet;
19+
import com.google.common.collect.RangeSet;
20+
import com.google.common.collect.TreeRangeSet;
1921
import java.util.Optional;
2022

2123
/**
@@ -183,7 +185,8 @@ static Builder builder() {
183185
static final class Builder {
184186

185187
private final ImmutableList.Builder<String> files = ImmutableList.builder();
186-
private final ImmutableRangeSet.Builder<Integer> lines = ImmutableRangeSet.builder();
188+
// A TreeRangeSet merges ranges that touch or overlap, which ImmutableRangeSet.Builder rejects
189+
private final RangeSet<Integer> lines = TreeRangeSet.create();
187190
private final ImmutableRangeSet.Builder<Integer> characterRanges = ImmutableRangeSet.builder();
188191
private final ImmutableList.Builder<Integer> offsets = ImmutableList.builder();
189192
private final ImmutableList.Builder<Integer> lengths = ImmutableList.builder();
@@ -212,7 +215,7 @@ Builder inPlace(boolean inPlace) {
212215
return this;
213216
}
214217

215-
ImmutableRangeSet.Builder<Integer> linesBuilder() {
218+
RangeSet<Integer> linesBuilder() {
216219
return lines;
217220
}
218221

@@ -294,7 +297,7 @@ CommandLineOptions build() {
294297
return new CommandLineOptions(
295298
files.build(),
296299
inPlace,
297-
lines.build(),
300+
ImmutableRangeSet.copyOf(lines),
298301
characterRanges.build(),
299302
offsets.build(),
300303
lengths.build(),

‎open-java-format/src/main/java/com/palantir/javaformat/java/CommandLineOptionsParser.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.common.base.Splitter;
1919
import com.google.common.collect.ImmutableRangeSet;
2020
import com.google.common.collect.Range;
21+
import com.google.common.collect.RangeSet;
2122
import java.io.IOException;
2223
import java.io.UncheckedIOException;
2324
import java.nio.file.Files;
@@ -212,7 +213,7 @@ private static Range<Integer> parseCharacterRange(String range) {
212213
* --lines flags or separated by commas. A single line can be set by a single number. Line numbers are
213214
* {@code 1}-based, but are converted to the {@code 0}-based numbering used internally by google-java-format.
214215
*/
215-
private static void parseRangeSet(ImmutableRangeSet.Builder<Integer> result, String ranges) {
216+
private static void parseRangeSet(RangeSet<Integer> result, String ranges) {
216217
for (String range : COMMA_SPLITTER.split(ranges)) {
217218
result.add(parseRange(range));
218219
}

‎open-java-format/src/main/java/com/palantir/javaformat/java/UsageException.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ final class UsageException extends Exception {
5252
" --set-exit-if-changed",
5353
" Return exit code 1 if there are any formatting changes.",
5454
" --lines, -lines, --line, -line",
55-
" Line range(s) to format, like 5:10 (1-based; default is all).",
55+
" Line range(s) to format, e.g. the first 5 lines are 1:5 (1-based; default is all).",
5656
" --character-ranges, -character-ranges, --character-range, -character-range",
5757
" character range(s) to format, like 5:10 (0-based; default is all).",
5858
" --offset, -offset",

‎open-java-format/src/test/java/com/palantir/javaformat/java/CommandLineOptionsParserTest.java‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static java.nio.charset.StandardCharsets.UTF_8;
1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20-
import static org.assertj.core.api.Assertions.fail;
2120

2221
import com.google.common.collect.Range;
2322
import java.io.IOException;
@@ -150,15 +149,20 @@ public void setExitIfChanged() {
150149
.isTrue();
151150
}
152151

153-
// TODO(cushon): consider handling this in the parser and reporting a more detailed error
154152
@Test
155-
public void illegalLines() {
156-
try {
157-
CommandLineOptionsParser.parse(Arrays.asList("-lines=1:1", "-lines=1:1"));
158-
fail("fail");
159-
} catch (IllegalArgumentException e) {
160-
assertThat(e.getMessage()).contains("overlap");
161-
}
153+
public void mergedLines() {
154+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-lines=1:5", "-lines=2:8"))
155+
.lines()
156+
.asRanges())
157+
.containsExactly(Range.closedOpen(0, 8));
158+
}
159+
160+
@Test
161+
public void repeatedLines() {
162+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-lines=1:1", "-lines=1:1"))
163+
.lines()
164+
.asRanges())
165+
.containsExactly(Range.closedOpen(0, 1));
162166
}
163167

164168
@Test

0 commit comments

Comments
 (0)