public TextRange newRange(TextPointer start, TextPointer end) {
checkValid(start, "start pointer");
checkValid(end, "end pointer");
- return newRangeValidPointers(start, end);
+ return newRangeValidPointers(start, end, false);
}
@Override
public TextRange newRange(int startLine, int startLineOffset, int endLine, int endLineOffset) {
TextPointer start = newPointer(startLine, startLineOffset);
TextPointer end = newPointer(endLine, endLineOffset);
- return newRangeValidPointers(start, end);
+ return newRangeValidPointers(start, end, false);
}
@Override
public TextRange selectLine(int line) {
TextPointer startPointer = newPointer(line, 0);
TextPointer endPointer = newPointer(line, lineLength(line));
- return newRangeValidPointers(startPointer, endPointer);
+ return newRangeValidPointers(startPointer, endPointer, true);
}
public void validate(TextRange range) {
checkValid(range.end(), "end pointer");
}
- private static TextRange newRangeValidPointers(TextPointer start, TextPointer end) {
- Preconditions.checkArgument(start.compareTo(end) <= 0, "Start pointer %s should be before end pointer %s", start, end);
+ private static TextRange newRangeValidPointers(TextPointer start, TextPointer end, boolean acceptEmptyRange) {
+ Preconditions.checkArgument(acceptEmptyRange ? (start.compareTo(end) <= 0) : (start.compareTo(end) < 0),
+ "Start pointer %s should be before end pointer %s", start, end);
return new DefaultTextRange(start, end);
}
* Create Range from global offsets. Used for backward compatibility with older API.
*/
public TextRange newRange(int startOffset, int endOffset) {
- return newRangeValidPointers(newPointer(startOffset), newPointer(endOffset));
+ return newRangeValidPointers(newPointer(startOffset), newPointer(endOffset), false);
}
public TextPointer newPointer(int globalOffset) {
@Test
public void checkValidRange() {
DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
- file.setLines(2);
- file.setOriginalLineOffsets(new int[] {0, 10});
- file.setLastValidOffset(15);
+ file.initMetadata("bla bla a\nabcde");
assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 1)).start().line()).isEqualTo(1);
// Don't fail
file.newRange(file.newPointer(1, 0), file.newPointer(1, 1));
file.newRange(file.newPointer(1, 0), file.newPointer(1, 9));
file.newRange(file.newPointer(1, 0), file.newPointer(2, 0));
assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 5))).isEqualTo(file.newRange(0, 15));
- file.newRange(file.newPointer(1, 0), file.newPointer(1, 0));
try {
- file.newRange(file.newPointer(1, 1), file.newPointer(1, 0));
+ file.newRange(file.newPointer(1, 0), file.newPointer(1, 0));
fail();
} catch (Exception e) {
- assertThat(e).hasMessage("Start pointer [line=1, lineOffset=1] should be before end pointer [line=1, lineOffset=0]");
+ assertThat(e).hasMessage("Start pointer [line=1, lineOffset=0] should be before end pointer [line=1, lineOffset=0]");
}
try {
file.newRange(file.newPointer(1, 0), file.newPointer(1, 10));
}
}
+ @Test
+ public void selectLine() {
+ DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
+ file.initMetadata("bla bla a\nabcde\n\nabc");
+ assertThat(file.selectLine(1).start().line()).isEqualTo(1);
+ assertThat(file.selectLine(1).start().lineOffset()).isEqualTo(0);
+ assertThat(file.selectLine(1).end().line()).isEqualTo(1);
+ assertThat(file.selectLine(1).end().lineOffset()).isEqualTo(9);
+
+ // Don't fail when selecting empty line
+ assertThat(file.selectLine(3).start().line()).isEqualTo(3);
+ assertThat(file.selectLine(3).start().lineOffset()).isEqualTo(0);
+ assertThat(file.selectLine(3).end().line()).isEqualTo(3);
+ assertThat(file.selectLine(3).end().lineOffset()).isEqualTo(0);
+
+ try {
+ file.selectLine(5);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).hasMessage("5 is not a valid line for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=null] has 4 line(s)");
+ }
+ }
+
@Test
public void checkValidRangeUsingGlobalOffset() {
DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");