search/replace: replace-all in selection is fixed. occurrences navigation and selection

This commit is contained in:
andrey zaytsev
2011-02-21 20:22:45 +03:00
parent 18bca98e5f
commit 110d2906da
3 changed files with 64 additions and 12 deletions
@@ -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);
}
@@ -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<Pair<TextRange, String>>());
}
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
@@ -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<FindResult> results = new ArrayList<FindResult>();
if (findModel != null) {
final Ref<TextRange> selectionRef = new Ref<TextRange>();
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);
}