@Test public void offsetOf() { TextSegment segment = new TextSegment(createLines("one\r\ntwo\r\nthree four")); String text = segment.getText(); assertEquals(0, segment.offsetOf(text.indexOf("one"))); assertEquals(5, segment.offsetOf(text.indexOf("two"))); assertEquals(3, segment.offsetOf(text.indexOf("two") - 1)); assertEquals(10, segment.offsetOf(text.indexOf("three"))); assertEquals(8, segment.offsetOf(text.indexOf("three") - 1)); assertEquals(16, segment.offsetOf(text.indexOf("four"))); }
public int offsetOf(int textOffset) { checkArgument(textOffset >= 0); int textOffsetOfLine = 0; int remainder = textOffset; for (Line line : lines) { textOffsetOfLine = line.getOffset(); int linePlusSeparatorLength = line.getText().length() + 1; if (linePlusSeparatorLength > remainder) { break; } remainder -= linePlusSeparatorLength; } return textOffsetOfLine + remainder; }
TextSegment { public int offsetOf(int textOffset) { checkArgument(textOffset >= 0); int textOffsetOfLine = 0; int remainder = textOffset; for (Line line : lines) { textOffsetOfLine = line.getOffset(); int linePlusSeparatorLength = line.getText().length() + 1; if (linePlusSeparatorLength > remainder) { break; } remainder -= linePlusSeparatorLength; } return textOffsetOfLine + remainder; } }
TextSegment { public int offsetOf(int textOffset) { checkArgument(textOffset >= 0); int textOffsetOfLine = 0; int remainder = textOffset; for (Line line : lines) { textOffsetOfLine = line.getOffset(); int linePlusSeparatorLength = line.getText().length() + 1; if (linePlusSeparatorLength > remainder) { break; } remainder -= linePlusSeparatorLength; } return textOffsetOfLine + remainder; } TextSegment(Iterable<Line> lines); }
TextSegment { public int offsetOf(int textOffset) { checkArgument(textOffset >= 0); int textOffsetOfLine = 0; int remainder = textOffset; for (Line line : lines) { textOffsetOfLine = line.getOffset(); int linePlusSeparatorLength = line.getText().length() + 1; if (linePlusSeparatorLength > remainder) { break; } remainder -= linePlusSeparatorLength; } return textOffsetOfLine + remainder; } TextSegment(Iterable<Line> lines); String getText(); List<Line> getLines(); int offsetOf(int textOffset); int toTextOffset(int documentOffset); @Override String toString(); Line getLineAtOffset(int textOffset); }
TextSegment { public int offsetOf(int textOffset) { checkArgument(textOffset >= 0); int textOffsetOfLine = 0; int remainder = textOffset; for (Line line : lines) { textOffsetOfLine = line.getOffset(); int linePlusSeparatorLength = line.getText().length() + 1; if (linePlusSeparatorLength > remainder) { break; } remainder -= linePlusSeparatorLength; } return textOffsetOfLine + remainder; } TextSegment(Iterable<Line> lines); String getText(); List<Line> getLines(); int offsetOf(int textOffset); int toTextOffset(int documentOffset); @Override String toString(); Line getLineAtOffset(int textOffset);  }