Search in sources :

Example 91 with RangeHighlighter

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

the class LivePreview method clearUnusedHightlighters.

private void clearUnusedHightlighters() {
    Set<RangeHighlighter> unused = new com.intellij.util.containers.HashSet<>();
    for (RangeHighlighter highlighter : myHighlighters) {
        if (highlighter.getUserData(MARKER_USED) == null) {
            unused.add(highlighter);
        } else {
            highlighter.putUserData(MARKER_USED, null);
        }
    }
    myHighlighters.removeAll(unused);
    Project project = mySearchResults.getProject();
    if (project != null && !project.isDisposed()) {
        for (RangeHighlighter highlighter : unused) {
            HighlightManager.getInstance(project).removeSegmentHighlighter(mySearchResults.getEditor(), highlighter);
        }
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) Project(com.intellij.openapi.project.Project) HashSet(java.util.HashSet)

Example 92 with RangeHighlighter

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

the class LivePreview method dumpEditorMarkupAndSelection.

private void dumpEditorMarkupAndSelection(PrintStream dumpStream) {
    dumpStream.println(mySearchResults.getFindModel());
    if (myReplacementPreviewText != null) {
        dumpStream.println("--");
        dumpStream.println("Replacement Preview: " + myReplacementPreviewText);
    }
    dumpStream.println("--");
    Editor editor = mySearchResults.getEditor();
    RangeHighlighter[] highlighters = editor.getMarkupModel().getAllHighlighters();
    List<Pair<Integer, Character>> ranges = new ArrayList<>();
    for (RangeHighlighter highlighter : highlighters) {
        ranges.add(new Pair<>(highlighter.getStartOffset(), '['));
        ranges.add(new Pair<>(highlighter.getEndOffset(), ']'));
    }
    SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.getSelectionStart() != selectionModel.getSelectionEnd()) {
        ranges.add(new Pair<>(selectionModel.getSelectionStart(), '<'));
        ranges.add(new Pair<>(selectionModel.getSelectionEnd(), '>'));
    }
    ranges.add(new Pair<>(-1, '\n'));
    ranges.add(new Pair<>(editor.getDocument().getTextLength() + 1, '\n'));
    ContainerUtil.sort(ranges, (pair, pair2) -> {
        int res = pair.first - pair2.first;
        if (res == 0) {
            Character c1 = pair.second;
            Character c2 = pair2.second;
            if (c1 == '<' && c2 == '[') {
                return 1;
            } else if (c1 == '[' && c2 == '<') {
                return -1;
            }
            return c1.compareTo(c2);
        }
        return res;
    });
    Document document = editor.getDocument();
    for (int i = 0; i < ranges.size() - 1; ++i) {
        Pair<Integer, Character> pair = ranges.get(i);
        Pair<Integer, Character> pair1 = ranges.get(i + 1);
        dumpStream.print(pair.second + document.getText(TextRange.create(Math.max(pair.first, 0), Math.min(pair1.first, document.getTextLength()))));
    }
    dumpStream.println("\n--");
    if (NotFound) {
        dumpStream.println("Not Found");
        dumpStream.println("--");
        NotFound = false;
    }
    for (RangeHighlighter highlighter : highlighters) {
        dumpStream.println(highlighter + " : " + highlighter.getTextAttributes());
    }
    dumpStream.println("------------");
}
Also used : ArrayList(java.util.ArrayList) Document(com.intellij.openapi.editor.Document) RelativePoint(com.intellij.ui.awt.RelativePoint) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) Pair(com.intellij.openapi.util.Pair)

Example 93 with RangeHighlighter

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

the class LivePreview method updateInSelectionHighlighters.

private void updateInSelectionHighlighters() {
    if (mySearchResults.getEditor() == null)
        return;
    final SelectionModel selectionModel = mySearchResults.getEditor().getSelectionModel();
    int[] starts = selectionModel.getBlockSelectionStarts();
    int[] ends = selectionModel.getBlockSelectionEnds();
    final HashSet<RangeHighlighter> toRemove = new HashSet<>();
    Set<RangeHighlighter> toAdd = new HashSet<>();
    for (RangeHighlighter highlighter : myHighlighters) {
        if (!highlighter.isValid())
            continue;
        boolean intersectsWithSelection = false;
        for (int i = 0; i < starts.length; ++i) {
            TextRange selectionRange = new TextRange(starts[i], ends[i]);
            intersectsWithSelection = selectionRange.intersects(highlighter.getStartOffset(), highlighter.getEndOffset()) && selectionRange.getEndOffset() != highlighter.getStartOffset() && highlighter.getEndOffset() != selectionRange.getStartOffset();
            if (intersectsWithSelection)
                break;
        }
        final Object userData = highlighter.getUserData(IN_SELECTION_KEY);
        if (userData != null) {
            if (!intersectsWithSelection) {
                if (userData == IN_SELECTION2) {
                    HighlightManager.getInstance(mySearchResults.getProject()).removeSegmentHighlighter(mySearchResults.getEditor(), highlighter);
                    toRemove.add(highlighter);
                } else {
                    highlighter.putUserData(IN_SELECTION_KEY, null);
                }
            }
        } else if (intersectsWithSelection) {
            TextRange cursor = mySearchResults.getCursor();
            if (cursor != null && highlighter.getStartOffset() == cursor.getStartOffset() && highlighter.getEndOffset() == cursor.getEndOffset())
                continue;
            final RangeHighlighter toAnnotate = highlightRange(new TextRange(highlighter.getStartOffset(), highlighter.getEndOffset()), new TextAttributes(null, null, Color.WHITE, EffectType.ROUNDED_BOX, Font.PLAIN), toAdd);
            highlighter.putUserData(IN_SELECTION_KEY, IN_SELECTION1);
            toAnnotate.putUserData(IN_SELECTION_KEY, IN_SELECTION2);
        }
    }
    myHighlighters.removeAll(toRemove);
    myHighlighters.addAll(toAdd);
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) SelectionModel(com.intellij.openapi.editor.SelectionModel) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextRange(com.intellij.openapi.util.TextRange) RelativePoint(com.intellij.ui.awt.RelativePoint) HashSet(java.util.HashSet)

Example 94 with RangeHighlighter

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

the class HighlightUsagesHandler method clearHighlights.

private static void clearHighlights(Editor editor, HighlightManager highlightManager, List<TextRange> rangesToHighlight, TextAttributes attributes) {
    if (editor instanceof EditorWindow)
        editor = ((EditorWindow) editor).getDelegate();
    RangeHighlighter[] highlighters = ((HighlightManagerImpl) highlightManager).getHighlighters(editor);
    Arrays.sort(highlighters, (o1, o2) -> o1.getStartOffset() - o2.getStartOffset());
    Collections.sort(rangesToHighlight, (o1, o2) -> o1.getStartOffset() - o2.getStartOffset());
    int i = 0;
    int j = 0;
    while (i < highlighters.length && j < rangesToHighlight.size()) {
        RangeHighlighter highlighter = highlighters[i];
        TextRange highlighterRange = TextRange.create(highlighter);
        TextRange refRange = rangesToHighlight.get(j);
        if (refRange.equals(highlighterRange) && attributes.equals(highlighter.getTextAttributes()) && highlighter.getLayer() == HighlighterLayer.SELECTION - 1) {
            highlightManager.removeSegmentHighlighter(editor, highlighter);
            i++;
        } else if (refRange.getStartOffset() > highlighterRange.getEndOffset()) {
            i++;
        } else if (refRange.getEndOffset() < highlighterRange.getStartOffset()) {
            j++;
        } else {
            i++;
            j++;
        }
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextRange(com.intellij.openapi.util.TextRange) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 95 with RangeHighlighter

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

the class BreadcrumbsXmlWrapper method itemHovered.

@Override
public void itemHovered(@Nullable BreadcrumbsPsiItem item) {
    if (!Registry.is("editor.breadcrumbs.highlight.on.hover")) {
        return;
    }
    HighlightManager hm = HighlightManager.getInstance(myProject);
    if (myHighlighed != null) {
        for (RangeHighlighter highlighter : myHighlighed) {
            hm.removeSegmentHighlighter(myEditor, highlighter);
        }
        myHighlighed = null;
    }
    if (item != null) {
        final TextRange range = item.getPsiElement().getTextRange();
        final TextAttributes attributes = new TextAttributes();
        final CrumbPresentation p = item.getPresentation();
        final Color color = p != null ? p.getBackgroundColor(false, false, false) : BreadcrumbsComponent.ButtonSettings.getBackgroundColor(false, false, false, false);
        final Color background = EditorColorsManager.getInstance().getGlobalScheme().getColor(EditorColors.CARET_ROW_COLOR);
        attributes.setBackgroundColor(XmlTagTreeHighlightingUtil.makeTransparent(color, background != null ? background : Gray._200, 0.3));
        myHighlighed = new ArrayList<>(1);
        int flags = HighlightManager.HIDE_BY_ESCAPE | HighlightManager.HIDE_BY_TEXT_CHANGE | HighlightManager.HIDE_BY_ANY_KEY;
        hm.addOccurrenceHighlight(myEditor, range.getStartOffset(), range.getEndOffset(), attributes, flags, myHighlighed, null);
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)97 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)26 TextRange (com.intellij.openapi.util.TextRange)22 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)18 Editor (com.intellij.openapi.editor.Editor)15 ArrayList (java.util.ArrayList)15 NotNull (org.jetbrains.annotations.NotNull)15 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)10 Project (com.intellij.openapi.project.Project)10 RelativePoint (com.intellij.ui.awt.RelativePoint)8 Nullable (org.jetbrains.annotations.Nullable)8 Document (com.intellij.openapi.editor.Document)7 EditorEx (com.intellij.openapi.editor.ex.EditorEx)7 MarkupModelEx (com.intellij.openapi.editor.ex.MarkupModelEx)7 MarkupModel (com.intellij.openapi.editor.markup.MarkupModel)7 PsiElement (com.intellij.psi.PsiElement)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 EditorWindow (com.intellij.injected.editor.EditorWindow)4 Result (com.intellij.openapi.application.Result)4 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)4