Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public class FindReplaceLogic implements IFindReplaceLogic {
private IFindReplaceStatus status;
private IFindReplaceTarget target;
private Point incrementalBaseLocation;

private boolean isTargetSupportingRegEx;
private boolean isTargetEditable;
private final Set<SearchOptions> searchOptions = new HashSet<>();
Expand Down Expand Up @@ -324,6 +323,9 @@ public boolean performSearch() {
private boolean performSearch(boolean updateFromIncrementalBaseLocation) {
resetStatus();
if (findString.isEmpty()) {
if (isAvailableAndActive(SearchOptions.INCREMENTAL)) {
restoreSelectionIfEmpty();
}
return false;
}

Expand All @@ -338,6 +340,19 @@ private boolean performSearch(boolean updateFromIncrementalBaseLocation) {
return somethingFound;
}

/**
* Restores the original caret/selection position when the search field becomes
* empty.
*/
private void restoreSelectionIfEmpty() {
if (incrementalBaseLocation == null) {
return;
}
if (target instanceof IFindReplaceTargetExtension extension) {
extension.setSelection(incrementalBaseLocation.x, incrementalBaseLocation.y);
}
}

/**
* Replaces all occurrences of the user's findString with the replace string.
* Returns the number of replacements that occur.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,41 @@ public void testResetIncrementalBaseLocation() {
assertThat(textViewer.getSelectedRange(), is(new Point(5, 4)));
}

@Test
public void testRestoreSelectionOnEmptyFindStringInIncrementalMode() {
String setupString= "test\ntest\ntest";
TextViewer textViewer= setupTextViewer(setupString);
textViewer.setSelectedRange(5, 0); // Set initial selection
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
findReplaceLogic.activate(SearchOptions.FORWARD);
findReplaceLogic.activate(SearchOptions.WRAP);
findReplaceLogic.activate(SearchOptions.INCREMENTAL);

findReplaceLogic.resetIncrementalBaseLocation(); // Set base location
findReplaceLogic.setFindString("test");
assertThat(textViewer.getSelectedRange(), is(new Point(5, 4)));
findReplaceLogic.setFindString(""); // Clear the find string
assertThat(textViewer.getSelectedRange(), is(new Point(5, 0))); // Restored to base location
}

@Test
public void testNoRestoreOnEmptyFindStringWhenNotIncremental() {
String setupString= "test\ntest\ntest";
TextViewer textViewer= setupTextViewer(setupString);
textViewer.setSelectedRange(5, 0); // Set initial selection
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
findReplaceLogic.activate(SearchOptions.FORWARD);
findReplaceLogic.activate(SearchOptions.WRAP);
// Do NOT activate incremental mode

findReplaceLogic.resetIncrementalBaseLocation();
findReplaceLogic.setFindString("test");
assertThat(textViewer.getSelectedRange(), is(new Point(5, 4)));
findReplaceLogic.setFindString(""); // Clear the find string
// Should not restore selection
assertThat(textViewer.getSelectedRange(), is(new Point(5, 4))); // Remains unchanged
}

@Test
public void testSetFindString_incrementalInactive() {
TextViewer textViewer= setupTextViewer("Test Test Test Test");
Expand Down Expand Up @@ -867,7 +902,7 @@ public void testSetFindString_incrementalActive() {
assertEquals(new Point(0, 2), findReplaceLogic.getTarget().getSelection());

findReplaceLogic.setFindString(""); // this clears the incremental search, but the "old search" still remains active
assertEquals(new Point(0, 2), findReplaceLogic.getTarget().getSelection());
assertEquals(new Point(0, 0), findReplaceLogic.getTarget().getSelection()); // after #3379 pr
}

@Test
Expand Down
Loading