From cca9d327ed283f8d35ed319738e053b86e5bdd59 Mon Sep 17 00:00:00 2001 From: "Vassiliy.Kudryashov" Date: Tue, 23 Oct 2018 02:16:17 +0300 Subject: [PATCH] IDEA-200011 Replace in pre-selected text doesn't work any more --- .../intellij/find/EditorSearchSession.java | 38 +++++++++++++++---- .../src/com/intellij/find/FindUtil.java | 19 ++++------ 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/find/EditorSearchSession.java b/platform/lang-impl/src/com/intellij/find/EditorSearchSession.java index 970864b92c81..5fb6b879aa95 100644 --- a/platform/lang-impl/src/com/intellij/find/EditorSearchSession.java +++ b/platform/lang-impl/src/com/intellij/find/EditorSearchSession.java @@ -146,15 +146,37 @@ public class EditorSearchSession implements SearchSession, new SwitchToFind(getComponent()); new SwitchToReplace(getComponent()); - myFindModel.addObserver(findModel1 -> { - String stringToFind = myFindModel.getStringToFind(); - if (!wholeWordsApplicable(stringToFind)) { - myFindModel.setWholeWordsOnly(false); + myFindModel.addObserver(new FindModel.FindModelObserver() { + boolean myReentrantLock = false; + boolean myIsGlobal = myFindModel.isGlobal(); + boolean myIsReplace = myFindModel.isReplaceState(); + @Override + public void findModelChanged(FindModel findModel1) { + if (myReentrantLock) return; + try { + myReentrantLock = true; + String stringToFind = myFindModel.getStringToFind(); + if (!wholeWordsApplicable(stringToFind)) { + myFindModel.setWholeWordsOnly(false); + } + if (myIsGlobal != myFindModel.isGlobal() || myIsReplace != myFindModel.isReplaceState()) { + if (myFindModel.getStringToFind().isEmpty() && myFindModel.isGlobal()) { + myFindModel.setStringToFind(StringUtil.notNullize(myEditor.getSelectionModel().getSelectedText(), "")); + } + if (!myFindModel.isGlobal() && myFindModel.getStringToFind().equals(myEditor.getSelectionModel().getSelectedText())) { + myFindModel.setStringToFind(""); + } + myIsGlobal = myFindModel.isGlobal(); + myIsReplace = myFindModel.isReplaceState(); + } + EditorSearchSession.this.updateUIWithFindModel(); + mySearchResults.clear(); + EditorSearchSession.this.updateResults(true); + FindUtil.updateFindInFileModel(EditorSearchSession.this.getProject(), myFindModel, !ConsoleViewUtil.isConsoleViewEditor(editor)); + } finally { + myReentrantLock = false; + } } - updateUIWithFindModel(); - mySearchResults.clear(); - updateResults(true); - FindUtil.updateFindInFileModel(getProject(), myFindModel, !ConsoleViewUtil.isConsoleViewEditor(editor)); }); updateUIWithFindModel(); diff --git a/platform/lang-impl/src/com/intellij/find/FindUtil.java b/platform/lang-impl/src/com/intellij/find/FindUtil.java index 5073ce0a7c06..29d987cd0583 100644 --- a/platform/lang-impl/src/com/intellij/find/FindUtil.java +++ b/platform/lang-impl/src/com/intellij/find/FindUtil.java @@ -81,28 +81,23 @@ public class FindUtil { } } - private static boolean isMultilineSelection(Editor editor) { - SelectionModel selectionModel = editor != null ? editor.getSelectionModel() : null; - if (selectionModel != null) { - String selectedText = selectionModel.getSelectedText(); - if (selectedText != null && selectedText.contains("\n")) { - return true; - } - } - return false; - } - public static void configureFindModel(boolean replace, @Nullable Editor editor, FindModel model, boolean firstSearch) { boolean isGlobal = true; String stringToFind = firstSearch ? "" : model.getStringToFind(); final SelectionModel selectionModel = editor != null ? editor.getSelectionModel() : null; String selectedText = selectionModel != null ? selectionModel.getSelectedText() : null; if (!StringUtil.isEmpty(selectedText)) { - model.setMultiline(isMultilineSelection(editor)); stringToFind = selectedText; } model.setReplaceState(replace); + boolean multiline = stringToFind.contains("\n"); + if (replace && multiline) { + isGlobal = false; + stringToFind = ""; + multiline = false; + } model.setStringToFind(stringToFind); + model.setMultiline(multiline); model.setGlobal(isGlobal); model.setPromptOnReplace(false); }