public void iterate(MarkupIterator iterator, int endOffset) { while (!iterator.atEnd()) { iterator.advance(); int startOffset = iterator.getStartOffset(); if (startOffset >= endOffset) { break; } if (myStartOffset < 0) { myStartOffset = startOffset; } boolean whiteSpacesOnly = CharArrayUtil.isEmptyOrSpaces(myText, startOffset, iterator.getEndOffset()); processBackground(startOffset, iterator.getBackgroundColor()); if (!whiteSpacesOnly) { processForeground(startOffset, iterator.getForegroundColor()); processFontFamilyName(startOffset, iterator.getFontFamilyName()); processFontStyle(startOffset, iterator.getFontStyle()); } } addTextIfPossible(endOffset); }
@Nullable @Override public TextBlockTransferableData collectTransferableData( PsiFile file, Editor editor, int[] startOffsets, int[] endOffsets) { if (!Registry.is("editor.richcopy.enable")) { return null; } try { for (TextWithMarkupBuilder builder : myBuilders) { builder.reset(); } SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasBlockSelection()) { return null; // unsupported legacy mode } RichCopySettings settings = RichCopySettings.getInstance(); List<Caret> carets = editor.getCaretModel().getAllCarets(); Caret firstCaret = carets.get(0); final int indentSymbolsToStrip; final int firstLineStartOffset; if (settings.isStripIndents() && 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(); EditorColorsScheme schemeToUse = settings.getColorsScheme(editor.getColorsScheme()); EditorHighlighter highlighter = HighlighterFactory.createHighlighter( file.getVirtualFile(), schemeToUse, file.getProject()); highlighter.setText(text); MarkupModel markupModel = DocumentMarkupModel.forDocument(editor.getDocument(), file.getProject(), false); Context context = new Context(text, schemeToUse, indentSymbolsToStrip); int shift = 0; int endOffset = 0; Caret prevCaret = null; for (Caret caret : carets) { int caretSelectionStart = caret.getSelectionStart(); int caretSelectionEnd = caret.getSelectionEnd(); int startOffsetToUse; 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()); int endLineOffset = endOffset + shift + fillStringLength; context.builder.addText(endLineOffset, endLineOffset + 1); shift++; // Block selection ends '\n' at line end shift += fillStringLength; } shift += endOffset - caretSelectionStart; endOffset = caretSelectionEnd; context.reset(shift); prevCaret = caret; if (endOffset <= startOffsetToUse) { continue; } MarkupIterator markupIterator = new MarkupIterator( text, new CompositeRangeIterator( schemeToUse, new HighlighterRangeIterator(highlighter, startOffsetToUse, endOffset), new MarkupModelRangeIterator( markupModel, schemeToUse, startOffsetToUse, endOffset)), schemeToUse); try { context.iterate(markupIterator, endOffset); } finally { markupIterator.dispose(); } } SyntaxInfo syntaxInfo = context.finish(); logSyntaxInfo(syntaxInfo); for (TextWithMarkupBuilder builder : myBuilders) { builder.build(syntaxInfo); } } catch (Exception e) { // catching the exception so that the rest of copy/paste functionality can still work fine LOG.error(e); } return null; }