From f2eccfa6c5f0b583992d5e0773f7a41785a078e2 Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Tue, 13 Sep 2016 14:39:55 +0200 Subject: [PATCH] remove selection made during search when nothing was found --- .../java-tests/testSrc/com/intellij/find/FindInEditorTest.java | 2 ++ .../src/com/intellij/find/impl/livePreview/SearchResults.java | 3 ++- .../com/intellij/find/impl/livePreview/SelectionManager.java | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) 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()) {