Search in sources :

Example 1 with SyntaxInfo

use of com.intellij.openapi.editor.richcopy.model.SyntaxInfo 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)

Example 2 with SyntaxInfo

use of com.intellij.openapi.editor.richcopy.model.SyntaxInfo in project intellij-community by JetBrains.

the class SyntaxInfoConstructionTest method getSyntaxInfo.

private static String getSyntaxInfo(Editor editor, PsiFile psiFile) {
    final StringBuilder builder = new StringBuilder();
    SelectionModel selectionModel = editor.getSelectionModel();
    String selectedText = selectionModel.getSelectedText(true);
    assertNotNull(selectedText);
    final String text = StringUtil.convertLineSeparators(selectedText);
    TextWithMarkupProcessor processor = new TextWithMarkupProcessor() {

        @Override
        void createResult(SyntaxInfo syntaxInfo, Editor editor) {
            final ColorRegistry colorRegistry = syntaxInfo.getColorRegistry();
            assertEquals(JBColor.BLACK, colorRegistry.dataById(syntaxInfo.getDefaultForeground()));
            assertEquals(JBColor.WHITE, colorRegistry.dataById(syntaxInfo.getDefaultBackground()));
            assertEquals((float) editor.getColorsScheme().getEditorFontSize(), syntaxInfo.getFontSize(), 0.01f);
            syntaxInfo.processOutputInfo(new MarkupHandler() {

                @Override
                public void handleText(int startOffset, int endOffset) throws Exception {
                    builder.append("text=").append(text.substring(startOffset, endOffset)).append('\n');
                }

                @Override
                public void handleForeground(int foregroundId) throws Exception {
                    builder.append("foreground=").append(colorRegistry.dataById(foregroundId)).append(',');
                }

                @Override
                public void handleBackground(int backgroundId) throws Exception {
                    builder.append("background=").append(colorRegistry.dataById(backgroundId)).append(',');
                }

                @Override
                public void handleFont(int fontNameId) throws Exception {
                    assertEquals(1, fontNameId);
                }

                @Override
                public void handleStyle(int style) throws Exception {
                    builder.append("fontStyle=").append(style).append(',');
                }

                @Override
                public boolean canHandleMore() {
                    return true;
                }
            });
        }
    };
    processor.collectTransferableData(psiFile, editor, selectionModel.getBlockSelectionStarts(), selectionModel.getBlockSelectionEnds());
    return builder.toString();
}
Also used : SyntaxInfo(com.intellij.openapi.editor.richcopy.model.SyntaxInfo) MarkupHandler(com.intellij.openapi.editor.richcopy.model.MarkupHandler) ColorRegistry(com.intellij.openapi.editor.richcopy.model.ColorRegistry)

Aggregations

SyntaxInfo (com.intellij.openapi.editor.richcopy.model.SyntaxInfo)2 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)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 ColorRegistry (com.intellij.openapi.editor.richcopy.model.ColorRegistry)1 MarkupHandler (com.intellij.openapi.editor.richcopy.model.MarkupHandler)1 RichCopySettings (com.intellij.openapi.editor.richcopy.settings.RichCopySettings)1 NotNull (org.jetbrains.annotations.NotNull)1