diff --git a/java/java-tests/testSrc/com/intellij/find/FindInEditorTest.java b/java/java-tests/testSrc/com/intellij/find/FindInEditorTest.java index 684e3357bfd7..d1fef45e14ce 100644 --- a/java/java-tests/testSrc/com/intellij/find/FindInEditorTest.java +++ b/java/java-tests/testSrc/com/intellij/find/FindInEditorTest.java @@ -60,6 +60,8 @@ public class FindInEditorTest extends LightCodeInsightTestCase { initFind(); myFindModel.setStringToFind("a"); checkResults(); + myFindModel.setStringToFind("a2"); + assertTrue(!myEditor.getSelectionModel().hasSelection()); } public void testEmacsLikeFallback() throws Exception { diff --git a/platform/lang-impl/src/com/intellij/find/impl/livePreview/SearchResults.java b/platform/lang-impl/src/com/intellij/find/impl/livePreview/SearchResults.java index 104d9b443511..3c804117e5f1 100644 --- a/platform/lang-impl/src/com/intellij/find/impl/livePreview/SearchResults.java +++ b/platform/lang-impl/src/com/intellij/find/impl/livePreview/SearchResults.java @@ -91,12 +91,13 @@ public class SearchResults implements DocumentListener { private final Stack> myCursorPositions = new Stack<>(); - private final SelectionManager mySelectionManager = new SelectionManager(this); + private final SelectionManager mySelectionManager; public SearchResults(Editor editor, Project project) { myEditor = editor; myProject = project; myEditor.getDocument().addDocumentListener(this); + mySelectionManager = new SelectionManager(this); // important to initialize last for accessing other fields } public void setNotFoundState(boolean isForward) { diff --git a/platform/lang-impl/src/com/intellij/find/impl/livePreview/SelectionManager.java b/platform/lang-impl/src/com/intellij/find/impl/livePreview/SelectionManager.java index 93c582611a21..d4ced63c086d 100644 --- a/platform/lang-impl/src/com/intellij/find/impl/livePreview/SelectionManager.java +++ b/platform/lang-impl/src/com/intellij/find/impl/livePreview/SelectionManager.java @@ -26,10 +26,12 @@ import java.util.List; public class SelectionManager { @NotNull private final SearchResults mySearchResults; + private final boolean myHadSelectionInitially; private final List myRegionsToRestore = new ArrayList<>(); public SelectionManager(@NotNull SearchResults results) { mySearchResults = results; + myHadSelectionInitially = results.getEditor().getSelectionModel().hasSelection(); } public void updateSelection(boolean removePreviousSelection, boolean removeAllPreviousSelections) { @@ -39,6 +41,7 @@ public class SelectionManager { } final FindResult cursor = mySearchResults.getCursor(); if (cursor == null) { + if (removePreviousSelection && !myHadSelectionInitially) editor.getSelectionModel().removeSelection(); return; } if (mySearchResults.getFindModel().isGlobal()) {