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
Move lineRangesToCharRanges
  • Loading branch information
iamdanfox committed Oct 22, 2019
commit 3da614301666ca7e56662510270f28b585fa9f37
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -99,7 +100,7 @@ private static void format(Formatter formatter, SingleFileDiff diff) {
return;
}

RangeSet<Integer> charRanges = Formatter.lineRangesToCharRanges(input, diff.lineRanges);
RangeSet<Integer> charRanges = Utils.lineRangesToCharRanges(input, diff.lineRanges);

try {
System.err.println("Formatting " + diff.path);
Expand Down
7 changes: 7 additions & 0 deletions palantir-java-format-utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ apply from: rootProject.file('gradle/publish-jar.gradle')

dependencies {
implementation project(':palantir-java-format-spi')

testImplementation 'com.google.guava:guava-testlib'
testImplementation 'com.google.testing.compile:compile-testing'
testImplementation 'com.google.truth:truth'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.junit.jupiter:junit-jupiter-migrationsupport'
testImplementation 'org.junit.jupiter:junit-jupiter'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.javaformat;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.Test;

/** {@link Newlines}Test */
public class NewlinesTest {
@Test
public void offsets() {
Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\nbar\n"))).containsExactly(0, 4, 8);
Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\nbar"))).containsExactly(0, 4);

Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\rbar\r"))).containsExactly(0, 4, 8);
Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\rbar"))).containsExactly(0, 4);

Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\r\nbar\r\n"))).containsExactly(0, 5, 10);
Truth.assertThat(ImmutableList.copyOf(Newlines.lineOffsetIterator("foo\r\nbar"))).containsExactly(0, 5);
}

@Test
public void lines() {
Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\nbar\n"))).containsExactly("foo\n", "bar\n");
Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\nbar"))).containsExactly("foo\n", "bar");

Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\rbar\r"))).containsExactly("foo\r", "bar\r");
Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\rbar"))).containsExactly("foo\r", "bar");

Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\r\nbar\r\n"))).containsExactly("foo\r\n", "bar\r\n");
Truth.assertThat(ImmutableList.copyOf(Newlines.lineIterator("foo\r\nbar"))).containsExactly("foo\r\n", "bar");
}

@Test
public void terminalOffset() {
Iterator<Integer> it = Newlines.lineOffsetIterator("foo\nbar\n");
it.next();
it.next();
it.next();
try {
it.next();
Assert.fail();
} catch (NoSuchElementException e) {
// expected
}

it = Newlines.lineOffsetIterator("foo\nbar");
it.next();
it.next();
try {
it.next();
Assert.fail();
} catch (NoSuchElementException e) {
// expected
}
}

@Test
public void terminalLine() {
Iterator<String> it = Newlines.lineIterator("foo\nbar\n");
it.next();
it.next();
try {
it.next();
Assert.fail();
} catch (NoSuchElementException e) {
// expected
}

it = Newlines.lineIterator("foo\nbar");
it.next();
it.next();
try {
it.next();
Assert.fail();
} catch (NoSuchElementException e) {
// expected
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import static java.util.Comparator.comparing;

import com.google.common.collect.Iterators;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import com.palantir.javaformat.java.Replacement;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -39,4 +43,22 @@ public static String applyReplacements(String input, Collection<Replacement> rep
}
return writer.toString();
}

/** 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<>();
Iterators.addAll(lines, Newlines.lineOffsetIterator(input));
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import com.palantir.javaformat.Utils;
import java.util.concurrent.Callable;

/** Encapsulates information about a file to be formatted, including which parts of the file to format. */
Expand Down Expand Up @@ -64,7 +65,7 @@ private RangeSet<Integer> characterRanges(String input) {
return characterRanges;
}

characterRanges.addAll(Formatter.lineRangesToCharRanges(input, parameters.lines()));
characterRanges.addAll(Utils.lineRangesToCharRanges(input, parameters.lines()));

for (int i = 0; i < parameters.offsets().size(); i++) {
Integer length = parameters.lengths().get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,4 @@ public ImmutableList<Replacement> getFormatReplacements(String input, Collection
RangeSet<Integer> tokenRangeSet = javaInput.characterRangesToTokenRanges(characterRanges);
return javaOutput.getFormatReplacements(tokenRangeSet);
}

/** 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<>();
Iterators.addAll(lines, Newlines.lineOffsetIterator(input));
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;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import com.palantir.javaformat.Utils;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

/** Tests for {@link Formatter#lineRangesToCharRanges} */
/** Tests for {@link Utils#lineRangesToCharRanges} */
@Execution(ExecutionMode.CONCURRENT)
public class LineRangesToCharRangesTest {

Expand All @@ -34,7 +35,7 @@ final Set<Range<Integer>> getCharRanges(String input, Range<Integer>... ranges)
for (Range<Integer> range : ranges) {
rangeSet.add(range);
}
return Formatter.lineRangesToCharRanges(input, rangeSet).asRanges();
return Utils.lineRangesToCharRanges(input, rangeSet).asRanges();
}

@Test
Expand Down