Skip to content

Commit

Permalink
Make lineRangesToCharRanges public
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=122772680
  • Loading branch information
cushon committed May 21, 2016
1 parent ef2c2da commit 5c027f1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
Expand Up @@ -14,16 +14,13 @@


package com.google.googlejavaformat.java; package com.google.googlejavaformat.java;


import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableRangeSet; import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.Range; import com.google.common.collect.Range;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet; import com.google.common.collect.TreeRangeSet;
import com.google.googlejavaformat.java.JavaFormatterOptions.SortImports; import com.google.googlejavaformat.java.JavaFormatterOptions.SortImports;


import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;


Expand Down Expand Up @@ -70,8 +67,6 @@ public String call() throws FormatterException {
.formatSource(inputString, characterRanges(inputString).asRanges()); .formatSource(inputString, characterRanges(inputString).asRanges());
} }


static final CharMatcher NEWLINE = CharMatcher.is('\n');

private RangeSet<Integer> characterRanges(String input) { private RangeSet<Integer> characterRanges(String input) {
final RangeSet<Integer> characterRanges = TreeRangeSet.create(); final RangeSet<Integer> characterRanges = TreeRangeSet.create();


Expand All @@ -80,36 +75,12 @@ private RangeSet<Integer> characterRanges(String input) {
return characterRanges; return characterRanges;
} }


characterRanges.addAll(lineRangesToCharRanges(input, lineRanges)); characterRanges.addAll(Formatter.lineRangesToCharRanges(input, lineRanges));


for (int i = 0; i < offsets.size(); i++) { for (int i = 0; i < offsets.size(); i++) {
characterRanges.add(Range.closedOpen(offsets.get(i), offsets.get(i) + lengths.get(i))); characterRanges.add(Range.closedOpen(offsets.get(i), offsets.get(i) + lengths.get(i)));
} }


return characterRanges; return characterRanges;
} }

@VisibleForTesting
static RangeSet<Integer> lineRangesToCharRanges(String input, RangeSet<Integer> lineRanges) {
List<Integer> lines = new ArrayList<>();
lines.add(0);
int idx = NEWLINE.indexIn(input);
while (idx >= 0) {
lines.add(idx + 1);
idx = NEWLINE.indexIn(input, idx + 1);
}
lines.add(input.length() + 1);

final RangeSet<Integer> characterRanges = TreeRangeSet.create();
for (Range<Integer> lineRange :
lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) {
int lineStart = lines.get(lineRange.lowerEndpoint());
// Exclude the trailing newline. This isn't strictly necessary, but handling blank lines
// as empty ranges is convenient.
int lineEnd = lines.get(lineRange.upperEndpoint()) - 1;
Range<Integer> range = Range.closedOpen(lineStart, lineEnd);
characterRanges.add(range);
}
return characterRanges;
}
} }
32 changes: 32 additions & 0 deletions core/src/main/java/com/google/googlejavaformat/java/Formatter.java
Expand Up @@ -14,9 +14,11 @@


package com.google.googlejavaformat.java; package com.google.googlejavaformat.java;


import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range; import com.google.common.collect.Range;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import com.google.common.io.CharSink; import com.google.common.io.CharSink;
import com.google.common.io.CharSource; import com.google.common.io.CharSource;
import com.google.errorprone.annotations.Immutable; import com.google.errorprone.annotations.Immutable;
Expand Down Expand Up @@ -203,4 +205,34 @@ public ImmutableList<Replacement> getFormatReplacements(
RangeSet<Integer> tokenRangeSet = javaInput.characterRangesToTokenRanges(characterRanges); RangeSet<Integer> tokenRangeSet = javaInput.characterRangesToTokenRanges(characterRanges);
return javaOutput.getFormatReplacements(tokenRangeSet); return javaOutput.getFormatReplacements(tokenRangeSet);
} }

static final CharMatcher NEWLINE = CharMatcher.is('\n');

/**
* Converts zero-indexed, [closed, open) line ranges in the given source file to character
* ranges.
*/
public static RangeSet<Integer> lineRangesToCharRanges(
String input, RangeSet<Integer> lineRanges) {
List<Integer> lines = new ArrayList<>();
lines.add(0);
int idx = NEWLINE.indexIn(input);
while (idx >= 0) {
lines.add(idx + 1);
idx = NEWLINE.indexIn(input, idx + 1);
}
lines.add(input.length() + 1);

final RangeSet<Integer> characterRanges = TreeRangeSet.create();
for (Range<Integer> lineRange :
lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) {
int lineStart = lines.get(lineRange.lowerEndpoint());
// Exclude the trailing newline. This isn't strictly necessary, but handling blank lines
// as empty ranges is convenient.
int lineEnd = lines.get(lineRange.upperEndpoint()) - 1;
Range<Integer> range = Range.closedOpen(lineStart, lineEnd);
characterRanges.add(range);
}
return characterRanges;
}
} }
Expand Up @@ -26,17 +26,17 @@


import java.util.Set; import java.util.Set;


/** {@link FormatFileCallable}Test */ /** Tests for {@link Formatter#lineRangesToCharRanges} */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class FormatFileCallableTest { public class LineRangesToCharRangesTest {


@SafeVarargs @SafeVarargs
final Set<Range<Integer>> getCharRanges(String input, Range<Integer>... ranges) { final Set<Range<Integer>> getCharRanges(String input, Range<Integer>... ranges) {
RangeSet<Integer> rangeSet = TreeRangeSet.create(); RangeSet<Integer> rangeSet = TreeRangeSet.create();
for (Range<Integer> range : ranges) { for (Range<Integer> range : ranges) {
rangeSet.add(range); rangeSet.add(range);
} }
return FormatFileCallable.lineRangesToCharRanges(input, rangeSet).asRanges(); return Formatter.lineRangesToCharRanges(input, rangeSet).asRanges();
} }


@Test @Test
Expand Down

0 comments on commit 5c027f1

Please sign in to comment.