/** * Returns whether the given range contains the given position, inclusively. */ public static boolean contains(Range range, Position position) { checkArgument(isValid(range), String.format("range is not valid: %s", range.toString())); checkArgument(isValid(position), String.format("position is not valid: %s", position.toString())); return POSITION_COMPARATOR.compare(range.getStart(), position) <= 0 && POSITION_COMPARATOR.compare(range.getEnd(), position) >= 0; }
/** * Returns whether range1 and range2 intersect. Assumes that a range is inclusive on its start and exclusive on its * end, which means that if range1 ends on the same position as range2 starts, this is not considered intersecting. */ public static boolean intersects(Range range1, Range range2) { checkArgument(isValid(range1), String.format("range1 is not valid: %s", range1.toString())); checkArgument(isValid(range2), String.format("range2 is not valid: %s", range2.toString())); Position maxStart = max(range1.getStart(), range2.getStart()); Position minEnd = min(range1.getEnd(), range2.getEnd()); return POSITION_COMPARATOR.compare(maxStart, minEnd) < 0; }
@Test public void testDidChanges_invalidRanges() throws IOException { // Should be appended to the end of the file InMemoryContentsManager writer = new InMemoryContentsManager( defaultSourcePath, "first line\nsecond line\nthird line\n"); List<TextDocumentContentChangeEvent> changes = Lists.newArrayList(); changes.add(new TextDocumentContentChangeEvent(Ranges.createRange(-1, 0, 0, 0), 3, "one")); changes.add(new TextDocumentContentChangeEvent(Ranges.createRange(0, 0, 0, 0), 3, "two")); expectedException.expect(IllegalArgumentException.class); expectedException .expectMessage(String.format("range1 is not valid: %s", Ranges.createRange(-1, 0, 0, 0).toString())); writer.applyChanges(changes); }
@Test public void testDidChanges_invalidRanges() throws IOException { // Should be appended to the end of the file Path source = addFileToFolder(sourceFolder.getRoot(), "myfile.txt", "first line\nsecond line\nthird line\n"); Path destination = destinationFolder.getRoot().toPath().resolve("myfile.txt"); FileBackedContentsManager writer = FileBackedContentsManager.of(source, destination); List<TextDocumentContentChangeEvent> changes = Lists.newArrayList(); changes.add(new TextDocumentContentChangeEvent(Ranges.createRange(-1, 0, 0, 0), 3, "one")); changes.add(new TextDocumentContentChangeEvent(Ranges.createRange(0, 0, 0, 0), 3, "two")); expectedException.expect(IllegalArgumentException.class); expectedException .expectMessage(String.format("range1 is not valid: %s", Ranges.createRange(-1, 0, 0, 0).toString())); writer.applyChanges(changes); }
@Test public void testIntersects_invalidRange1() { Range range1 = Ranges.UNDEFINED_RANGE; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(String.format("range1 is not valid: %s", range1.toString())); assertFalse(Ranges.intersects(range1, Ranges.createRange(1, 2, 1, 2))); }
@Test public void testContains_invalidRange() { Range range = Ranges.createRange(6, 6, 4, 4); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(String.format("range is not valid: %s", range.toString())); Ranges.contains(range, position(6, 7)); }
@Test public void testIntersects_invalidRange2() { Range range2 = Ranges.UNDEFINED_RANGE; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(String.format("range2 is not valid: %s", range2.toString())); assertFalse(Ranges.intersects(Ranges.createRange(1, 2, 1, 2), range2)); }