String (@NotNull Editor editor) { CaretAndSelectionMarkup markup = new CaretAndSelectionMarkup(); // There's no guarantee on the order the carets are enumerated, // and in any case we should be prepared that something might go wrong. for (Caret caret : editor.getCaretModel().getAllCarets()) { boolean hasSelection = caret.hasSelection(); if (hasSelection) markup.addMark(caret.getSelectionStart(), SELECTION_START_TAG); markup.addMark(caret.getOffset(), CARET_TAG); if (hasSelection) markup.addMark(caret.getSelectionEnd(), SELECTION_END_TAG); } return markup.insertMarks(editor.getDocument().getCharsSequence()); }