Runnable (@NotNull Collection<? extends Line> lines) { final Runnable command = () -> { Document document = myEditor.getDocument(); StringBuilder buffer = new StringBuilder(); for (Line line : lines) { buffer.append(line.getValue()); } int endBefore = document.getTextLength(); document.insertString(endBefore, buffer.toString()); int endBeforeLine = endBefore; for (Line line : lines) { myEditor.getMarkupModel() .addRangeHighlighter(endBeforeLine, Math.min(document.getTextLength(), endBeforeLine + line.getValue().length()), HighlighterLayer.ADDITIONAL_SYNTAX, line.getAttributes(), HighlighterTargetArea.EXACT_RANGE); endBeforeLine += line.getValue().length(); if (endBeforeLine > document.getTextLength()) break; } shiftCursorToTheEndOfDocument(); }; return () -> CommandProcessor.getInstance().executeCommand(myProject, command, "", null, UndoConfirmationPolicy.DEFAULT, myEditor.getDocument()); }