String (@NotNull CharSequence text, @NotNull LineOffsets lineOffsets, @NotNull CharSequence otherText, @NotNull LineOffsets otherLineOffsets, @NotNull List<? extends Range> ranges) { return new Object() { private final StringBuilder stringBuilder = new StringBuilder(); private boolean isEmpty = true; @NotNull public String execute() { int lastLine = 0; for (Range range : ranges) { CharSequence newChunkContent = DiffRangeUtil.getLinesContent(otherText, otherLineOffsets, range.start2, range.end2); appendOriginal(lastLine, range.start1); append(newChunkContent, range.end2 - range.start2); lastLine = range.end1; } appendOriginal(lastLine, lineOffsets.getLineCount()); return stringBuilder.toString(); } private void appendOriginal(int start, int end) { append(DiffRangeUtil.getLinesContent(text, lineOffsets, start, end), end - start); } private void append(CharSequence content, int lineCount) { if (lineCount > 0 && !isEmpty) { stringBuilder.append('\n'); } stringBuilder.append(content); isEmpty &= lineCount == 0; } }.execute(); }