From 3f6691a38bc0df02f17ddd644f0fa6fef1d139ed Mon Sep 17 00:00:00 2001 From: andrey zaytsev Date: Thu, 17 Feb 2011 18:22:25 +0300 Subject: [PATCH] IDEA-65641 --- .../com/intellij/find/impl/LivePreview.java | 46 +++++++++++++++---- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/find/impl/LivePreview.java b/platform/lang-impl/src/com/intellij/find/impl/LivePreview.java index 5882ea6c05a9..e092ac5293fe 100644 --- a/platform/lang-impl/src/com/intellij/find/impl/LivePreview.java +++ b/platform/lang-impl/src/com/intellij/find/impl/LivePreview.java @@ -307,9 +307,31 @@ public class LivePreview extends DocumentAdapter implements ReplacementView.Dele private void highlightUsages(TextRange oldCursorRange) { if (myEditor == null || myShouldStop) return; - LiveOccurrence firstVisibleOccurrence = null; - LiveOccurrence firstOccurrence = null; + for (LiveOccurrence o : mySearchResults) { + for (TextRange textRange : o.getSecondaryRanges()) { + highlightRange(textRange, OTHER_TARGETS_ATTRIBUTES, myHighlighters); + } + highlightRange(o.getPrimaryRange(), MAIN_TARGET_ATTRIBUTES, myHighlighters); + } + + if (!tryToRepairOldCursor(oldCursorRange)) { + LiveOccurrence afterCaret = firstOccurrenceAfterCaret(); + if (afterCaret != null) { + setCursor(afterCaret); + } else { + LiveOccurrence occurrence = firstVisibleOccurrence(); + if (occurrence != null) { + setCursor(occurrence); + } + } + } + } + + @Nullable + private LiveOccurrence firstVisibleOccurrence() { int offset = Integer.MAX_VALUE; + LiveOccurrence firstOccurrence = null; + LiveOccurrence firstVisibleOccurrence = null; for (LiveOccurrence o : mySearchResults) { if (insideVisibleArea(myEditor, o.getPrimaryRange())) { if (firstVisibleOccurrence == null || o.getPrimaryRange().getStartOffset() < firstVisibleOccurrence.getPrimaryRange().getStartOffset()) { @@ -320,16 +342,22 @@ public class LivePreview extends DocumentAdapter implements ReplacementView.Dele offset = o.getPrimaryRange().getStartOffset(); firstOccurrence = o; } + } + return firstVisibleOccurrence != null ? firstVisibleOccurrence : firstOccurrence; + } - for (TextRange textRange : o.getSecondaryRanges()) { - highlightRange(textRange, OTHER_TARGETS_ATTRIBUTES, myHighlighters); + @Nullable + private LiveOccurrence firstOccurrenceAfterCaret() { + LiveOccurrence afterCaret = null; + int caret = myEditor.getCaretModel().getOffset(); + for (LiveOccurrence occurrence : mySearchResults) { + if (occurrence.getPrimaryRange().getStartOffset() >= caret) { + if (afterCaret == null || occurrence.getPrimaryRange().getStartOffset() < afterCaret.getPrimaryRange().getStartOffset() ) { + afterCaret = occurrence; + } } - highlightRange(o.getPrimaryRange(), MAIN_TARGET_ATTRIBUTES, myHighlighters); - } - - if (!tryToRepairOldCursor(oldCursorRange)) { - setCursor(firstVisibleOccurrence != null ? firstVisibleOccurrence : firstOccurrence); } + return afterCaret; } private boolean tryToRepairOldCursor(TextRange oldCursorRange) {