From 110d2906da3b2dfa5456ac4d9a6fc61f8bfea696 Mon Sep 17 00:00:00 2001 From: andrey zaytsev Date: Mon, 21 Feb 2011 20:22:15 +0300 Subject: [PATCH] search/replace: replace-all in selection is fixed. occurrences navigation and selection --- .../intellij/find/EditorSearchComponent.java | 12 +++++- .../LivePreviewControllerBase.java | 27 ++++++++++++-- .../find/impl/livePreview/SearchResults.java | 37 +++++++++++++++---- 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/find/EditorSearchComponent.java b/platform/lang-impl/src/com/intellij/find/EditorSearchComponent.java index 516bdaac1f41..d0e18e187ce3 100644 --- a/platform/lang-impl/src/com/intellij/find/EditorSearchComponent.java +++ b/platform/lang-impl/src/com/intellij/find/EditorSearchComponent.java @@ -194,7 +194,15 @@ public class EditorSearchComponent extends JPanel implements DataProvider, Selec public void getFocusBack() { mySearchField.requestFocus(); } + + @Override + public TextRange performReplace(LiveOccurrence occurrence, String replacement, Editor editor) { + myToChangeSelection = true; + return super + .performReplace(occurrence, replacement, editor); //To change body of overridden methods use File | Settings | File Templates. + } }; + mySearchResults.addListener(this); setMatchesLimit(MATCHES_LIMIT); @@ -640,7 +648,9 @@ public class EditorSearchComponent extends JPanel implements DataProvider, Selec model.setGlobal(!mySelectionOnly.isSelected()); model.setPreserveCase(myPreserveCase.isEnabled() && myPreserveCase.isSelected()); } - myToChangeSelection = allowedToChangedEditorSelection; + if (!myToChangeSelection) { + myToChangeSelection = allowedToChangedEditorSelection; + } myLivePreviewController.updateInBackground(model); } diff --git a/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreviewControllerBase.java b/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreviewControllerBase.java index a94ec03b51a1..c78fd723f35e 100644 --- a/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreviewControllerBase.java +++ b/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreviewControllerBase.java @@ -26,6 +26,21 @@ public class LivePreviewControllerBase implements LivePreview.Delegate, FindUtil private SearchResults mySearchResults; private LivePreview myLivePreview; + public interface ReplaceListener { + void replacePerformed(LiveOccurrence occurrence, final String replacement, final Editor editor); + void replaceAllPerformed(Editor e); + } + + private ReplaceListener myReplaceListener; + + public ReplaceListener getReplaceListener() { + return myReplaceListener; + } + + public void setReplaceListener(ReplaceListener replaceListener) { + myReplaceListener = replaceListener; + } + public LivePreviewControllerBase(SearchResults searchResults, LivePreview livePreview) { mySearchResults = searchResults; myLivePreview = livePreview; @@ -86,21 +101,27 @@ public class LivePreviewControllerBase implements LivePreview.Delegate, FindUtil public TextRange performReplace(final LiveOccurrence occurrence, final String replacement, final Editor editor) { TextRange range = occurrence.getPrimaryRange(); FindModel findModel = mySearchResults.getFindModel(); + TextRange result = null; try { - return FindUtil.doReplace(editor.getProject(), editor.getDocument(), findModel, new FindResultImpl(range.getStartOffset(), range.getEndOffset()), + result = FindUtil.doReplace(editor.getProject(), editor.getDocument(), findModel, new FindResultImpl(range.getStartOffset(), range.getEndOffset()), FindManager.getInstance(editor.getProject()).getStringToReplace(editor.getDocument().getText(range), findModel), true, new ArrayList>()); } catch (FindManager.MalformedReplacementStringException e) { /**/ } + if (myReplaceListener != null) { + myReplaceListener.replacePerformed(occurrence, replacement, editor); + } mySearchResults.updateThreadSafe(findModel); - return null; + return result; } @Override public void performReplaceAll(Editor e) { - FindUtil.replace(e.getProject(), e, 0, mySearchResults.getFindModel(), this); + FindUtil.replace(e.getProject(), e, + mySearchResults.getFindModel().isGlobal() ? 0 : mySearchResults.getEditor().getSelectionModel().getSelectionStart(), + mySearchResults.getFindModel(), this); } @Override 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 2381f31aaa3e..1fb568ed4c51 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 @@ -4,10 +4,12 @@ package com.intellij.find.impl.livePreview; import com.intellij.find.FindManager; import com.intellij.find.FindModel; import com.intellij.find.FindResult; +import com.intellij.find.FindUtil; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.Nullable; @@ -32,6 +34,7 @@ public class SearchResults { private FindModel myFindModel; private int myMatchesLimit = 100; + private boolean myNotFound = false; public SearchResults(Editor editor) { myEditor = editor; @@ -131,10 +134,16 @@ public class SearchResults { final ArrayList results = new ArrayList(); if (findModel != null) { - + final Ref selectionRef = new Ref(); + ApplicationManager.getApplication().runReadAction(new Runnable() { + @Override + public void run() { + selectionRef.set(new TextRange(editor.getSelectionModel().getSelectionStart(), + editor.getSelectionModel().getSelectionEnd())); + } + }); TextRange r = findModel.isGlobal() ? new TextRange(0, Integer.MAX_VALUE) : - new TextRange(editor.getSelectionModel().getSelectionStart(), - editor.getSelectionModel().getSelectionEnd()); + selectionRef.get(); if (r.getLength() == 0) { r = new TextRange(0, Integer.MAX_VALUE); } @@ -305,18 +314,30 @@ public class SearchResults { } public void prevOccurrence() { - LiveOccurrence prev = firstOccurrenceBeforeCaret(); - if (prev == null && !getOccurrences().isEmpty()) { - prev = getOccurrences().get(getOccurrences().size() - 1); + LiveOccurrence next = null; + if (!myFindModel.isGlobal()) { + next = prevOccurrence(myCursor); + } else { + next = firstOccurrenceBeforeCaret(); } - moveCursorTo(prev); + if (next == null && !getOccurrences().isEmpty()) { + next = getOccurrences().get(getOccurrences().size()-1); + } + + moveCursorTo(next); } public void nextOccurrence() { - LiveOccurrence next = firstOccurrenceAfterCaret(); + LiveOccurrence next = null; + if (!myFindModel.isGlobal()) { + next = nextOccurrence(myCursor); + } else { + next = firstOccurrenceAfterCaret(); + } if (next == null && !getOccurrences().isEmpty()) { next = getOccurrences().get(0); } + moveCursorTo(next); }