List<RawTextWithMarkup> (@NotNull PsiFile file, @NotNull Editor editor, int @NotNull [] startOffsets, int @NotNull [] endOffsets) { if (!RichCopySettings.getInstance().isEnabled()) { return Collections.emptyList(); } EditorHighlighter highlighter = null; EditorColorsScheme editorColorsScheme = editor.getColorsScheme(); EditorColorsScheme schemeToUse = RichCopySettings.getInstance().getColorsScheme(editorColorsScheme); try { List<Caret> carets = editor.getCaretModel().getAllCarets(); Caret firstCaret = carets.get(0); final int indentSymbolsToStrip; final int firstLineStartOffset; if (Registry.is("editor.richcopy.strip.indents") && carets.size() == 1) { Pair<Integer, Integer> p = calcIndentSymbolsToStrip(editor.getDocument(), firstCaret.getSelectionStart(), firstCaret.getSelectionEnd()); firstLineStartOffset = p.first; indentSymbolsToStrip = p.second; } else { firstLineStartOffset = firstCaret.getSelectionStart(); indentSymbolsToStrip = 0; } logInitial(editor, startOffsets, endOffsets, indentSymbolsToStrip, firstLineStartOffset); CharSequence text = editor.getDocument().getCharsSequence(); highlighter = editor.getHighlighter(); if (editorColorsScheme != schemeToUse) { highlighter.setColorScheme(schemeToUse); } MarkupModel markupModel = DocumentMarkupModel.forDocument(editor.getDocument(), file.getProject(), false); Context context = new Context(text, schemeToUse, indentSymbolsToStrip); int endOffset = 0; Caret prevCaret = null; for (Caret caret : carets) { int caretSelectionStart = caret.getSelectionStart(); int caretSelectionEnd = caret.getSelectionEnd(); int startOffsetToUse; int additionalShift = 0; if (caret == firstCaret) { startOffsetToUse = firstLineStartOffset; } else { startOffsetToUse = caretSelectionStart; assert prevCaret != null; String prevCaretSelectedText = prevCaret.getSelectedText(); // Block selection fills short lines by white spaces int fillStringLength = prevCaretSelectedText == null ? 0 : prevCaretSelectedText.length() - (prevCaret.getSelectionEnd() - prevCaret.getSelectionStart()); context.addCharacter(endOffset + fillStringLength); additionalShift = fillStringLength + 1; } context.reset(endOffset - caretSelectionStart + additionalShift); endOffset = caretSelectionEnd; prevCaret = caret; if (endOffset <= startOffsetToUse) { continue; } MyMarkupIterator markupIterator = createMarkupIterator(highlighter, text, schemeToUse, markupModel, startOffsetToUse, endOffset); try { context.iterate(markupIterator, endOffset); } finally { markupIterator.dispose(); } } SyntaxInfo syntaxInfo = context.finish(); logSyntaxInfo(syntaxInfo); createResult(syntaxInfo, editor); return ObjectUtils.notNull(myResult, Collections.emptyList()); } catch (Throwable t) { // catching the exception so that the rest of copy/paste functionality can still work fine LOG.error("Error generating text with markup", t, new Attachment("highlighter.txt", String.valueOf(highlighter))); } finally { if (highlighter != null && editorColorsScheme != schemeToUse) { highlighter.setColorScheme(editorColorsScheme); } } return Collections.emptyList(); }