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,7 @@ public class FindReplaceLogic implements IFindReplaceLogic {
private IFindReplaceStatus status;
private IFindReplaceTarget target;
private Point incrementalBaseLocation;

private Point restoreBaseLocation = new Point(0, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why initialize with (0,0) and then later check for null (which it can never be)? This leads to setting the cursor location to (0,0) when performing a search while no find string was entered.

private boolean isTargetSupportingRegEx;
private boolean isTargetEditable;
private final Set<SearchOptions> searchOptions = new HashSet<>();
Expand All @@ -60,6 +60,12 @@ public class FindReplaceLogic implements IFindReplaceLogic {

@Override
public void setFindString(String findString) {
if (this.findString.isEmpty() && !findString.isEmpty()) {
// User just started a new search after clearing previous search.
if (target != null) {
restoreBaseLocation = target.getSelection();
}
}
this.findString = Objects.requireNonNull(findString);
if (isAvailableAndActive(SearchOptions.INCREMENTAL)) {
performSearch(true);
Expand Down Expand Up @@ -324,6 +330,7 @@ public boolean performSearch() {
private boolean performSearch(boolean updateFromIncrementalBaseLocation) {
resetStatus();
if (findString.isEmpty()) {
restoreSelectionIfEmpty();
return false;
}

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

/**
* Restores the original caret/selection position when the search field becomes
* empty.
*/
private void restoreSelectionIfEmpty() {
if (restoreBaseLocation == null)
return;
Comment on lines +353 to +354
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted in #3379 (comment): please always put expression blocks into braces (applies in the same way also to other places in the PR).

incrementalBaseLocation = restoreBaseLocation;
if (target instanceof IFindReplaceTargetExtension extension)
extension.setSelection(restoreBaseLocation.x, restoreBaseLocation.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 @@ -829,10 +829,91 @@ public void testResetIncrementalBaseLocation() {

findReplaceLogic.setFindString("test");
assertThat(textViewer.getSelectedRange(), is(new Point(0, 4)));

// Move caret after first "test" i.e., caret after "test\n", so after 5 characters
textViewer.setSelectedRange(5, 0);
findReplaceLogic.resetIncrementalBaseLocation();
findReplaceLogic.performSearch();
assertThat(textViewer.getSelectedRange(), is(new Point(5, 4)));

// Move caret inside second "test" i.e., caret after "test\nte", so after 7 characters
textViewer.setSelectedRange(7, 0);
findReplaceLogic.resetIncrementalBaseLocation();
findReplaceLogic.performSearch();
assertThat(textViewer.getSelectedRange(), is(new Point(10, 4)));

// Move caret to position to third "test" i.e., caret after "test\ntest\n", so after 10 characters
textViewer.setSelectedRange(10, 0);
findReplaceLogic.resetIncrementalBaseLocation();
findReplaceLogic.performSearch();
assertThat(textViewer.getSelectedRange(), is(new Point(10, 4)));

findReplaceLogic.setFindString("");
findReplaceLogic.performSearch();
assertThat(textViewer.getSelectedRange(), is(new Point(0, 0)));
}

@Test
public void testSelectionRestoredAfterClearingSearch() {
String setupString= "alpha beta gamma";
TextViewer textViewer= setupTextViewer(setupString);
textViewer.setSelectedRange(6, 0); // caret after 'alpha '
IFindReplaceLogic logic= setupFindReplaceLogicObject(textViewer);
logic.activate(SearchOptions.FORWARD);
logic.activate(SearchOptions.INCREMENTAL);

logic.setFindString("gamma");
assertThat(textViewer.getSelectedRange(), is(new Point(11, 5))); // "gamma" found

logic.setFindString("");
assertThat(textViewer.getSelectedRange(), is(new Point(6, 0)));
}


@Test
public void testCaretRestoredWhenSearchCleared() {
String setupString= "alpha beta gamma";
TextViewer textViewer= setupTextViewer(setupString);
textViewer.setSelectedRange(0, 0);
IFindReplaceLogic logic= setupFindReplaceLogicObject(textViewer);
logic.activate(SearchOptions.FORWARD);
logic.activate(SearchOptions.INCREMENTAL);

logic.setFindString("beta");
assertThat(textViewer.getSelectedRange(), is(new Point(6, 4))); // found "beta"

// Clear the search field - should restore caret
logic.setFindString("");
logic.performSearch();
// Expect caret restored to starting location (0,0)
assertThat(textViewer.getSelectedRange(), is(new Point(0, 0)));
}


@Test
public void testCaretRestoredBetweenSearchSessions() {
String setupString= "alpha beta gamma";
TextViewer textViewer= setupTextViewer(setupString);
IFindReplaceLogic logic= setupFindReplaceLogicObject(textViewer);
logic.activate(SearchOptions.FORWARD);
logic.activate(SearchOptions.INCREMENTAL);

textViewer.setSelectedRange(0, 0); // caret at start
logic.setFindString("alpha");
assertThat(textViewer.getSelectedRange(), is(new Point(0, 5))); // "alpha" found

// Clear the search (simulate reopen of overlay)
logic.setFindString("");
logic.performSearch();
assertThat(textViewer.getSelectedRange(), is(new Point(0, 0))); // caret restored

textViewer.setSelectedRange(6, 0); // caret before "beta"
logic.setFindString("beta");
assertThat(textViewer.getSelectedRange(), is(new Point(6, 4))); // "beta" found

logic.setFindString("");
logic.performSearch();
assertThat(textViewer.getSelectedRange(), is(new Point(6, 0))); // caret restored to new base
}

@Test
Expand Down Expand Up @@ -867,7 +948,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