private void removeFromEditor() { Editor editor = mySearchResults.getEditor(); if (myReplacementBalloon != null) { myReplacementBalloon.hide(); } if (editor != null) { for (VisibleAreaListener visibleAreaListener : myVisibleAreaListenersToRemove) { editor.getScrollingModel().removeVisibleAreaListener(visibleAreaListener); } myVisibleAreaListenersToRemove.clear(); Project project = mySearchResults.getProject(); if (project != null && !project.isDisposed()) { for (RangeHighlighter h : myHighlighters) { HighlightManager.getInstance(project).removeSegmentHighlighter(editor, h); } if (myCursorHighlighter != null) { HighlightManager.getInstance(project) .removeSegmentHighlighter(editor, myCursorHighlighter); myCursorHighlighter = null; } } myHighlighters.clear(); if (myListeningSelection) { editor.getSelectionModel().removeSelectionListener(this); myListeningSelection = false; } } }
private void updateCursorHighlighting(boolean scroll) { hideBalloon(); if (myCursorHighlighter != null) { HighlightManager.getInstance(mySearchResults.getProject()) .removeSegmentHighlighter(mySearchResults.getEditor(), myCursorHighlighter); myCursorHighlighter = null; } final FindResult cursor = mySearchResults.getCursor(); Editor editor = mySearchResults.getEditor(); SelectionModel selection = editor.getSelectionModel(); if (cursor != null) { Set<RangeHighlighter> dummy = new HashSet<RangeHighlighter>(); highlightRange( cursor, new TextAttributes(null, null, Color.BLACK, EffectType.ROUNDED_BOX, 0), dummy); if (!dummy.isEmpty()) { myCursorHighlighter = dummy.iterator().next(); } if (scroll) { if (mySearchResults.getFindModel().isGlobal()) { FoldingModel foldingModel = editor.getFoldingModel(); final FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions(); foldingModel.runBatchFoldingOperation( new Runnable() { @Override public void run() { for (FoldRegion region : allRegions) { if (!region.isValid()) continue; if (cursor.intersects(TextRange.create(region))) { region.setExpanded(true); } } } }); selection.setSelection(cursor.getStartOffset(), cursor.getEndOffset()); editor.getCaretModel().moveToOffset(cursor.getEndOffset()); editor.getScrollingModel().scrollToCaret(ScrollType.CENTER); } else { if (!SearchResults.insideVisibleArea(editor, cursor)) { LogicalPosition pos = editor.offsetToLogicalPosition(cursor.getStartOffset()); editor.getScrollingModel().scrollTo(pos, ScrollType.CENTER); } } } editor .getScrollingModel() .runActionOnScrollingFinished( new Runnable() { @Override public void run() { showReplacementPreview(); } }); } }
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<RangeHighlighter>(); Set<RangeHighlighter> toAdd = new HashSet<RangeHighlighter>(); for (RangeHighlighter highlighter : myHighlighters) { 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, 0), toAdd); highlighter.putUserData(IN_SELECTION_KEY, IN_SELECTION1); toAnnotate.putUserData(IN_SELECTION_KEY, IN_SELECTION2); } } myHighlighters.removeAll(toRemove); myHighlighters.addAll(toAdd); }
private RangeHighlighter doHightlightRange( final TextRange textRange, final TextAttributes attributes, Set<RangeHighlighter> highlighters) { HighlightManager highlightManager = HighlightManager.getInstance(mySearchResults.getProject()); MarkupModelEx markupModel = (MarkupModelEx) mySearchResults.getEditor().getMarkupModel(); final RangeHighlighter[] candidate = new RangeHighlighter[1]; boolean notFound = markupModel.processRangeHighlightersOverlappingWith( textRange.getStartOffset(), textRange.getEndOffset(), new Processor<RangeHighlighterEx>() { @Override public boolean process(RangeHighlighterEx highlighter) { TextAttributes textAttributes = highlighter.getTextAttributes(); if (highlighter.getUserData(SEARCH_MARKER) != null && textAttributes != null && textAttributes.equals(attributes) && highlighter.getStartOffset() == textRange.getStartOffset() && highlighter.getEndOffset() == textRange.getEndOffset()) { candidate[0] = highlighter; return false; } return true; } }); if (!notFound && highlighters.contains(candidate[0])) { return candidate[0]; } final ArrayList<RangeHighlighter> dummy = new ArrayList<RangeHighlighter>(); highlightManager.addRangeHighlight( mySearchResults.getEditor(), textRange.getStartOffset(), textRange.getEndOffset(), attributes, false, dummy); final RangeHighlighter h = dummy.get(0); highlighters.add(h); h.putUserData(SEARCH_MARKER, YES); return h; }
private void clearUnusedHightlighters() { Set<RangeHighlighter> unused = new com.intellij.util.containers.HashSet<RangeHighlighter>(); 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.isDisposed()) { for (RangeHighlighter highlighter : unused) { HighlightManager.getInstance(project) .removeSegmentHighlighter(mySearchResults.getEditor(), highlighter); } } }
@Override public void searchResultsUpdated(SearchResults sr) { final Project project = mySearchResults.getProject(); if (project == null || project.isDisposed()) return; if (mySuppressedUpdate) { mySuppressedUpdate = false; return; } if (!myInSmartUpdate) { removeFromEditor(); } highlightUsages(); updateCursorHighlighting(false); if (myInSmartUpdate) { clearUnusedHightlighters(); myInSmartUpdate = false; } }