Skip to content

Commit f5f30fe

Browse files
deepika-uHeikoKlare
authored andcommitted
Refactors find/replace overlay to restore cursor position when search
input is cleared in incremental mode
1 parent 8b402ec commit f5f30fe

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ public boolean performSearch() {
324324
private boolean performSearch(boolean updateFromIncrementalBaseLocation) {
325325
resetStatus();
326326
if (findString.isEmpty()) {
327+
if (isAvailableAndActive(SearchOptions.INCREMENTAL)) {
328+
restoreSelectionIfEmpty();
329+
}
327330
return false;
328331
}
329332

@@ -338,6 +341,19 @@ private boolean performSearch(boolean updateFromIncrementalBaseLocation) {
338341
return somethingFound;
339342
}
340343

344+
/**
345+
* Restores the original caret/selection position when the search field becomes
346+
* empty.
347+
*/
348+
private void restoreSelectionIfEmpty() {
349+
if (incrementalBaseLocation == null) {
350+
return;
351+
}
352+
if (target instanceof IFindReplaceTargetExtension extension) {
353+
extension.setSelection(incrementalBaseLocation.x, incrementalBaseLocation.y);
354+
}
355+
}
356+
341357
/**
342358
* Replaces all occurrences of the user's findString with the replace string.
343359
* Returns the number of replacements that occur.

tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogicTest.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,53 @@ public void testResetIncrementalBaseLocation() {
833833
findReplaceLogic.resetIncrementalBaseLocation();
834834
findReplaceLogic.performSearch();
835835
assertThat(textViewer.getSelectedRange(), is(new Point(5, 4)));
836+
textViewer.setSelectedRange(7, 0);
837+
findReplaceLogic.resetIncrementalBaseLocation();
838+
findReplaceLogic.performSearch();
839+
assertThat(textViewer.getSelectedRange(), is(new Point(10, 4)));
840+
textViewer.setSelectedRange(10, 0);
841+
findReplaceLogic.resetIncrementalBaseLocation();
842+
findReplaceLogic.performSearch();
843+
assertThat(textViewer.getSelectedRange(), is(new Point(10, 4)));
844+
// Verify that after clearing the search, caret goes back to last active location
845+
findReplaceLogic.setFindString("");
846+
findReplaceLogic.performSearch();
847+
assertThat(textViewer.getSelectedRange(), is(new Point(10, 4)));
848+
}
849+
850+
@Test
851+
public void testRestoreSelectionOnEmptyFindStringInIncrementalMode() {
852+
String setupString= "test\ntest\ntest";
853+
TextViewer textViewer= setupTextViewer(setupString);
854+
textViewer.setSelectedRange(5, 0); // Set initial selection
855+
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
856+
findReplaceLogic.activate(SearchOptions.FORWARD);
857+
findReplaceLogic.activate(SearchOptions.WRAP);
858+
findReplaceLogic.activate(SearchOptions.INCREMENTAL);
859+
860+
findReplaceLogic.resetIncrementalBaseLocation(); // Set base location
861+
findReplaceLogic.setFindString("test");
862+
assertThat(textViewer.getSelectedRange(), is(new Point(5, 4)));
863+
findReplaceLogic.setFindString(""); // Clear the find string
864+
assertThat(textViewer.getSelectedRange(), is(new Point(5, 0))); // Restored to base location
865+
}
866+
867+
@Test
868+
public void testNoRestoreOnEmptyFindStringWhenNotIncremental() {
869+
String setupString= "test\ntest\ntest";
870+
TextViewer textViewer= setupTextViewer(setupString);
871+
textViewer.setSelectedRange(5, 0); // Set initial selection
872+
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
873+
findReplaceLogic.activate(SearchOptions.FORWARD);
874+
findReplaceLogic.activate(SearchOptions.WRAP);
875+
// Do NOT activate incremental mode
876+
877+
findReplaceLogic.resetIncrementalBaseLocation();
878+
findReplaceLogic.setFindString("test");
879+
assertThat(textViewer.getSelectedRange(), is(new Point(5, 0)));
880+
findReplaceLogic.setFindString(""); // Clear the find string
881+
// Will not restore selection since incremental mode is not active
882+
assertThat(textViewer.getSelectedRange(), is(new Point(5, 0))); // Remains unchanged
836883
}
837884

838885
@Test
@@ -866,8 +913,8 @@ public void testSetFindString_incrementalActive() {
866913
findReplaceLogic.setFindString("Te");
867914
assertEquals(new Point(0, 2), findReplaceLogic.getTarget().getSelection());
868915

869-
findReplaceLogic.setFindString(""); // this clears the incremental search, but the "old search" still remains active
870-
assertEquals(new Point(0, 2), findReplaceLogic.getTarget().getSelection());
916+
findReplaceLogic.setFindString(""); // clears the incremental search input, restoring the caret to the base location
917+
assertEquals(new Point(0, 0), findReplaceLogic.getTarget().getSelection());
871918
}
872919

873920
@Test

0 commit comments

Comments
 (0)