Search in sources :

Example 16 with MarkupModel

use of com.intellij.openapi.editor.markup.MarkupModel 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 17 with MarkupModel

use of com.intellij.openapi.editor.markup.MarkupModel in project intellij-community by JetBrains.

the class UnifiedEditorRangeHighlighter method erase.

public static void erase(@Nullable Project project, @NotNull Document document) {
    MarkupModel model = DocumentMarkupModel.forDocument(document, project, true);
    model.removeAllHighlighters();
}
Also used : DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel)

Example 18 with MarkupModel

use of com.intellij.openapi.editor.markup.MarkupModel in project intellij-community by JetBrains.

the class DefaultHighlightInfoProcessor method highlightsInsideVisiblePartAreProduced.

@Override
public void highlightsInsideVisiblePartAreProduced(@NotNull final HighlightingSession session, @NotNull final List<HighlightInfo> infos, @NotNull TextRange priorityRange, @NotNull TextRange restrictRange, final int groupId) {
    final PsiFile psiFile = session.getPsiFile();
    final Project project = psiFile.getProject();
    final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
    if (document == null)
        return;
    final long modificationStamp = document.getModificationStamp();
    final TextRange priorityIntersection = priorityRange.intersection(restrictRange);
    final Editor editor = session.getEditor();
    ((HighlightingSessionImpl) session).applyInEDT(() -> {
        if (modificationStamp != document.getModificationStamp())
            return;
        if (priorityIntersection != null) {
            MarkupModel markupModel = DocumentMarkupModel.forDocument(document, project, true);
            EditorColorsScheme scheme = session.getColorsScheme();
            UpdateHighlightersUtil.setHighlightersInRange(project, document, priorityIntersection, scheme, infos, (MarkupModelEx) markupModel, groupId);
        }
        if (editor != null && !editor.isDisposed()) {
            // usability: show auto import popup as soon as possible
            if (!DumbService.isDumb(project)) {
                new ShowAutoImportPass(project, psiFile, editor).doApplyInformationToEditor();
            }
            DaemonListeners.repaintErrorStripeRenderer(editor, project);
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) ProperTextRange(com.intellij.openapi.util.ProperTextRange) TextRange(com.intellij.openapi.util.TextRange) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) Document(com.intellij.openapi.editor.Document) Editor(com.intellij.openapi.editor.Editor) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel)

Example 19 with MarkupModel

use of com.intellij.openapi.editor.markup.MarkupModel in project intellij-community by JetBrains.

the class NavigationUtil method patchAttributesColor.

/**
   * Patches attributes to be visible under debugger active line
   */
@SuppressWarnings("UseJBColor")
public static TextAttributes patchAttributesColor(TextAttributes attributes, @NotNull TextRange range, @NotNull Editor editor) {
    if (attributes.getForegroundColor() == null && attributes.getEffectColor() == null)
        return attributes;
    MarkupModel model = DocumentMarkupModel.forDocument(editor.getDocument(), editor.getProject(), false);
    if (model != null) {
        if (!((MarkupModelEx) model).processRangeHighlightersOverlappingWith(range.getStartOffset(), range.getEndOffset(), highlighter -> {
            if (highlighter.isValid() && highlighter.getTargetArea() == HighlighterTargetArea.LINES_IN_RANGE) {
                TextAttributes textAttributes = highlighter.getTextAttributes();
                if (textAttributes != null) {
                    Color color = textAttributes.getBackgroundColor();
                    return !(color != null && color.getBlue() > 128 && color.getRed() < 128 && color.getGreen() < 128);
                }
            }
            return true;
        })) {
            TextAttributes clone = attributes.clone();
            clone.setForegroundColor(Color.orange);
            clone.setEffectColor(Color.orange);
            return clone;
        }
    }
    return attributes;
}
Also used : UIUtil(com.intellij.util.ui.UIUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) GotoRelatedProvider(com.intellij.navigation.GotoRelatedProvider) ElementBase(com.intellij.psi.impl.ElementBase) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Extensions(com.intellij.openapi.extensions.Extensions) DumbService(com.intellij.openapi.project.DumbService) TextRange(com.intellij.openapi.util.TextRange) FileEditor(com.intellij.openapi.fileEditor.FileEditor) DefaultPsiElementCellRenderer(com.intellij.ide.util.DefaultPsiElementCellRenderer) HighlighterTargetArea(com.intellij.openapi.editor.markup.HighlighterTargetArea) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) HintUpdateSupply(com.intellij.ui.popup.HintUpdateSupply) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) Processor(com.intellij.util.Processor) EditSourceUtil(com.intellij.ide.util.EditSourceUtil) PopupStep(com.intellij.openapi.ui.popup.PopupStep) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer) JBColor(com.intellij.ui.JBColor) NavigationItem(com.intellij.navigation.NavigationItem) java.util(java.util) DataContext(com.intellij.openapi.actionSystem.DataContext) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PopupListElementRenderer(com.intellij.ui.popup.list.PopupListElementRenderer) ContainerUtil(com.intellij.util.containers.ContainerUtil) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) JBList(com.intellij.ui.components.JBList) StringUtil(com.intellij.openapi.util.text.StringUtil) SeparatorWithText(com.intellij.ui.SeparatorWithText) Editor(com.intellij.openapi.editor.Editor) ActionEvent(java.awt.event.ActionEvent) JBPopup(com.intellij.openapi.ui.popup.JBPopup) java.awt(java.awt) EditorHistoryManager(com.intellij.openapi.fileEditor.impl.EditorHistoryManager) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) Navigatable(com.intellij.pom.Navigatable) javax.swing(javax.swing) JBColor(com.intellij.ui.JBColor) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel)

Example 20 with MarkupModel

use of com.intellij.openapi.editor.markup.MarkupModel in project intellij-community by JetBrains.

the class ParameterNameHintsConfigurable method highlightErrorLines.

private static void highlightErrorLines(@NotNull List<Integer> lines, @NotNull EditorTextField editorTextField) {
    Editor editor = editorTextField.getEditor();
    if (editor == null)
        return;
    final TextAttributes attributes = editor.getColorsScheme().getAttributes(ERRORS_ATTRIBUTES);
    final Document document = editor.getDocument();
    final int totalLines = document.getLineCount();
    MarkupModel model = editor.getMarkupModel();
    model.removeAllHighlighters();
    lines.stream().filter((current) -> current < totalLines).forEach((line) -> model.addLineHighlighter(line, HighlighterLayer.ERROR, attributes));
}
Also used : Language(com.intellij.lang.Language) java.util(java.util) SelectionModel(com.intellij.openapi.editor.SelectionModel) Document(com.intellij.openapi.editor.Document) Option(com.intellij.codeInsight.hints.Option) ItemListener(java.awt.event.ItemListener) ERRORS_ATTRIBUTES(com.intellij.openapi.editor.colors.CodeInsightColors.ERRORS_ATTRIBUTES) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) ContainerUtil(com.intellij.util.containers.ContainerUtil) JBLabel(com.intellij.ui.components.JBLabel) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) HighlighterLayer(com.intellij.openapi.editor.markup.HighlighterLayer) InlayParameterHintsProvider(com.intellij.codeInsight.hints.InlayParameterHintsProvider) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) FileTypes(com.intellij.openapi.fileTypes.FileTypes) HintUtilsKt(com.intellij.codeInsight.hints.HintUtilsKt) ComboBox(com.intellij.openapi.ui.ComboBox) EditorTextField(com.intellij.ui.EditorTextField) ItemEvent(java.awt.event.ItemEvent) JBCheckBox(com.intellij.ui.components.JBCheckBox) StringUtil(com.intellij.openapi.util.text.StringUtil) InlayParameterHintsExtension(com.intellij.codeInsight.hints.InlayParameterHintsExtension) ListCellRendererWrapper(com.intellij.ui.ListCellRendererWrapper) Editor(com.intellij.openapi.editor.Editor) Collectors(java.util.stream.Collectors) ListComboBoxModel(org.jdesktop.swingx.combobox.ListComboBoxModel) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) IdeBorderFactory(com.intellij.ui.IdeBorderFactory) EditorFactory(com.intellij.openapi.editor.EditorFactory) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel)

Aggregations

MarkupModel (com.intellij.openapi.editor.markup.MarkupModel)23 DocumentMarkupModel (com.intellij.openapi.editor.impl.DocumentMarkupModel)12 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)10 Editor (com.intellij.openapi.editor.Editor)8 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)8 Project (com.intellij.openapi.project.Project)8 TextRange (com.intellij.openapi.util.TextRange)8 List (java.util.List)6 NotNull (org.jetbrains.annotations.NotNull)5 Document (com.intellij.openapi.editor.Document)4 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)4 ArrayList (java.util.ArrayList)4 javax.swing (javax.swing)4 Segment (com.intellij.openapi.util.Segment)3 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)2 EditorFactory (com.intellij.openapi.editor.EditorFactory)2 EditorEx (com.intellij.openapi.editor.ex.EditorEx)2 MarkupModelEx (com.intellij.openapi.editor.ex.MarkupModelEx)2 RangeHighlighterEx (com.intellij.openapi.editor.ex.RangeHighlighterEx)2 EditorUtil (com.intellij.openapi.editor.ex.util.EditorUtil)2