Search in sources :

Example 6 with RichCopySettings

use of com.intellij.openapi.editor.richcopy.settings.RichCopySettings in project intellij-community by JetBrains.

the class TextWithMarkupProcessor method collectTransferableData.

@NotNull
@Override
public List<RawTextWithMarkup> collectTransferableData(PsiFile file, Editor editor, int[] startOffsets, int[] endOffsets) {
    if (!RichCopySettings.getInstance().isEnabled()) {
        return Collections.emptyList();
    }
    try {
        RichCopySettings settings = RichCopySettings.getInstance();
        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();
        EditorColorsScheme schemeToUse = settings.getColorsScheme(editor.getColorsScheme());
        EditorHighlighter highlighter = HighlighterFactory.createHighlighter(file.getViewProvider().getVirtualFile(), schemeToUse, file.getProject());
        highlighter.setText(text);
        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 = new MyMarkupIterator(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);
        createResult(syntaxInfo, editor);
        return ObjectUtils.notNull(myResult, Collections.<RawTextWithMarkup>emptyList());
    } catch (Exception e) {
        // catching the exception so that the rest of copy/paste functionality can still work fine
        LOG.error(e);
    }
    return Collections.emptyList();
}
Also used : RichCopySettings(com.intellij.openapi.editor.richcopy.settings.RichCopySettings) SyntaxInfo(com.intellij.openapi.editor.richcopy.model.SyntaxInfo) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

RichCopySettings (com.intellij.openapi.editor.richcopy.settings.RichCopySettings)6 CodeInsightSettings (com.intellij.codeInsight.CodeInsightSettings)4 EditorSettingsExternalizable (com.intellij.openapi.editor.ex.EditorSettingsExternalizable)4 UISettings (com.intellij.ide.ui.UISettings)3 VcsApplicationSettings (com.intellij.openapi.vcs.VcsApplicationSettings)3 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)2 NotNull (org.jetbrains.annotations.NotNull)2 QuickDocOnMouseOverManager (com.intellij.codeInsight.documentation.QuickDocOnMouseOverManager)1 UsageDescriptor (com.intellij.internal.statistic.beans.UsageDescriptor)1 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)1 DocumentMarkupModel (com.intellij.openapi.editor.impl.DocumentMarkupModel)1 MarkupModel (com.intellij.openapi.editor.markup.MarkupModel)1 SyntaxInfo (com.intellij.openapi.editor.richcopy.model.SyntaxInfo)1 HashSet (java.util.HashSet)1