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; } } }
public LivePreview(SearchResults searchResults) { mySearchResults = searchResults; searchResultsUpdated(searchResults); searchResults.addListener(this); myListeningSelection = true; mySearchResults.getEditor().getSelectionModel().addSelectionListener(this); }
private void showReplacementPreview() { hideBalloon(); final FindResult cursor = mySearchResults.getCursor(); final Editor editor = mySearchResults.getEditor(); if (myDelegate != null && cursor != null) { String replacementPreviewText = myDelegate.getStringToReplace(editor, cursor); if (StringUtil.isEmpty(replacementPreviewText)) { replacementPreviewText = EMPTY_STRING_DISPLAY_TEXT; } final FindModel findModel = mySearchResults.getFindModel(); if (findModel.isRegularExpressions() && findModel.isReplaceState()) { showBalloon(cursor, editor, replacementPreviewText); } } }
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); } } }
private void highlightUsages() { if (mySearchResults.getEditor() == null) return; if (mySearchResults.getMatchesCount() >= mySearchResults.getMatchesLimit()) return; for (FindResult range : mySearchResults.getOccurrences()) { TextAttributes attributes = EditorColorsManager.getInstance() .getGlobalScheme() .getAttributes(EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES); if (range.getLength() == 0) { attributes = attributes.clone(); attributes.setEffectType(EffectType.BOXED); attributes.setEffectColor(attributes.getBackgroundColor()); } if (mySearchResults.isExcluded(range)) { highlightRange(range, strikout(attributes), myHighlighters); } else { highlightRange(range, attributes, myHighlighters); } } updateInSelectionHighlighters(); if (!myListeningSelection) { mySearchResults.getEditor().getSelectionModel().addSelectionListener(this); myListeningSelection = true; } }
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(); } }); } }
@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; } }
@Override public RelativePoint recalculateLocation(final Balloon object) { FindResult cursor = mySearchResults.getCursor(); if (cursor == null) return null; final TextRange cur = cursor; int startOffset = cur.getStartOffset(); int endOffset = cur.getEndOffset(); if (startOffset >= myEditor.getDocument().getTextLength()) { if (!object.isDisposed()) { requestBalloonHiding(object); } return null; } if (!SearchResults.insideVisibleArea(myEditor, cur)) { requestBalloonHiding(object); VisibleAreaListener visibleAreaListener = new VisibleAreaListener() { @Override public void visibleAreaChanged(VisibleAreaEvent e) { if (SearchResults.insideVisibleArea(myEditor, cur)) { showReplacementPreview(); final VisibleAreaListener visibleAreaListener = this; final boolean remove = myVisibleAreaListenersToRemove.remove(visibleAreaListener); if (remove) { myEditor.getScrollingModel().removeVisibleAreaListener(visibleAreaListener); } } } }; myEditor.getScrollingModel().addVisibleAreaListener(visibleAreaListener); myVisibleAreaListenersToRemove.add(visibleAreaListener); } Point startPoint = myEditor.visualPositionToXY(myEditor.offsetToVisualPosition(startOffset)); Point endPoint = myEditor.visualPositionToXY(myEditor.offsetToVisualPosition(endOffset)); Point point = new Point((startPoint.x + endPoint.x) / 2, startPoint.y); return new RelativePoint(myEditor.getContentComponent(), point); }
public void dispose() { cleanUp(); mySearchResults.removeListener(this); }
@Override public void editorChanged(SearchResults sr, Editor oldEditor) { removeFromEditor(); oldEditor.getDocument().removeDocumentListener(this); mySearchResults.getEditor().getDocument().addDocumentListener(this); }
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<Pair<Integer, Character>>(); for (RangeHighlighter highlighter : highlighters) { ranges.add(new Pair<Integer, Character>(highlighter.getStartOffset(), '[')); ranges.add(new Pair<Integer, Character>(highlighter.getEndOffset(), ']')); } SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.getSelectionStart() != selectionModel.getSelectionEnd()) { ranges.add(new Pair<Integer, Character>(selectionModel.getSelectionStart(), '<')); ranges.add(new Pair<Integer, Character>(selectionModel.getSelectionEnd(), '>')); } ranges.add(new Pair<Integer, Character>(-1, '\n')); ranges.add(new Pair<Integer, Character>(editor.getDocument().getTextLength() + 1, '\n')); ContainerUtil.sort( ranges, new Comparator<Pair<Integer, Character>>() { @Override public int compare(Pair<Integer, Character> pair, Pair<Integer, Character> 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("------------"); }