LineOffsets (@NotNull CharSequence text) { IntList ends = new IntArrayList(); int index = 0; while (true) { int lineEnd = Strings.indexOf(text, '\n', index); if (lineEnd != -1) { ends.add(lineEnd); index = lineEnd + 1; } else { ends.add(text.length()); break; } } return new LineOffsetsImpl(ends.toIntArray(), text.length()); }