Skip to content

Commit 72bb8b1

Browse files
committed
This pr stores the current editor caret position when user types a new
search pattern.
1 parent fbad80e commit 72bb8b1

File tree

1 file changed

+24
-1
lines changed
  • bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace

1 file changed

+24
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public class FindReplaceLogic implements IFindReplaceLogic {
5050
private IFindReplaceStatus status;
5151
private IFindReplaceTarget target;
5252
private Point incrementalBaseLocation;
53-
53+
private Point restoreBaseLocation = new Point(0, 0);
54+
private boolean isSetRestoreBaseLocation = true;
5455
private boolean isTargetSupportingRegEx;
5556
private boolean isTargetEditable;
5657
private final Set<SearchOptions> searchOptions = new HashSet<>();
@@ -60,6 +61,11 @@ public class FindReplaceLogic implements IFindReplaceLogic {
6061

6162
@Override
6263
public void setFindString(String findString) {
64+
if (this.findString.isEmpty() && !findString.isEmpty()) {
65+
// User just started a new search after clearing previous search.
66+
if (target != null)
67+
restoreBaseLocation = target.getSelection();
68+
}
6369
this.findString = Objects.requireNonNull(findString);
6470
if (isAvailableAndActive(SearchOptions.INCREMENTAL)) {
6571
performSearch(true);
@@ -165,6 +171,10 @@ private static boolean isWord(String str) {
165171
public void resetIncrementalBaseLocation() {
166172
if (target != null && shouldInitIncrementalBaseLocation()) {
167173
incrementalBaseLocation = target.getSelection();
174+
if (isSetRestoreBaseLocation) {
175+
restoreBaseLocation = incrementalBaseLocation;
176+
isSetRestoreBaseLocation = false;
177+
}
168178
} else {
169179
incrementalBaseLocation = new Point(0, 0);
170180
}
@@ -324,6 +334,7 @@ public boolean performSearch() {
324334
private boolean performSearch(boolean updateFromIncrementalBaseLocation) {
325335
resetStatus();
326336
if (findString.isEmpty()) {
337+
restoreSelectionIfEmpty();
327338
return false;
328339
}
329340

@@ -338,6 +349,18 @@ private boolean performSearch(boolean updateFromIncrementalBaseLocation) {
338349
return somethingFound;
339350
}
340351

352+
/**
353+
* Restores the original caret/selection position when the search field becomes
354+
* empty.
355+
*/
356+
private void restoreSelectionIfEmpty() {
357+
if (restoreBaseLocation == null)
358+
return;
359+
incrementalBaseLocation = restoreBaseLocation;
360+
if (target instanceof IFindReplaceTargetExtension extension)
361+
extension.setSelection(restoreBaseLocation.x, restoreBaseLocation.y);
362+
}
363+
341364
/**
342365
* Replaces all occurrences of the user's findString with the replace string.
343366
* Returns the number of replacements that occur.

0 commit comments

Comments
 (0)