Skip to content

Commit

Permalink
Find/Replace Logic: Allow reset of Incremental Base Position (#2104)
Browse files Browse the repository at this point in the history
Allow the client to reset the base position for incremental search
without enabling/disabling the Incremental search.

fixes #1913
  • Loading branch information
Wittmaxi committed Jul 19, 2024
1 parent 0dd221c commit 56bca9c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void activate(SearchOptions searchOption) {
case FORWARD:
case INCREMENTAL:
if (shouldInitIncrementalBaseLocation()) {
initIncrementalBaseLocation();
resetIncrementalBaseLocation();
}
break;
// $CASES-OMITTED$
Expand All @@ -87,7 +87,7 @@ public void deactivate(SearchOptions searchOption) {
}

if (searchOption == SearchOptions.FORWARD && shouldInitIncrementalBaseLocation()) {
initIncrementalBaseLocation();
resetIncrementalBaseLocation();
}
}

Expand Down Expand Up @@ -136,12 +136,8 @@ public boolean isRegExSearchAvailableAndActive() {
return isActive(SearchOptions.REGEX) && isTargetSupportingRegEx;
}


/**
* Initializes the anchor used as starting point for incremental searching.
*
*/
private void initIncrementalBaseLocation() {
@Override
public void resetIncrementalBaseLocation() {
if (target != null && isActive(SearchOptions.INCREMENTAL) && !isRegExSearchAvailableAndActive()) {
incrementalBaseLocation = target.getSelection();
} else {
Expand All @@ -159,7 +155,7 @@ public boolean shouldInitIncrementalBaseLocation() {
*/
private void initializeSearchScope() {
if (shouldInitIncrementalBaseLocation()) {
initIncrementalBaseLocation();
resetIncrementalBaseLocation();
}

if (target == null || !(target instanceof IFindReplaceTargetExtension)) {
Expand Down Expand Up @@ -343,7 +339,7 @@ public boolean performSearch(String searchString) {
*/
private boolean performSearch(boolean mustInitIncrementalBaseLocation, String findString) {
if (mustInitIncrementalBaseLocation) {
initIncrementalBaseLocation();
resetIncrementalBaseLocation();
}
resetStatus();

Expand Down Expand Up @@ -604,7 +600,7 @@ public void updateTarget(IFindReplaceTarget newTarget, boolean canEditTarget) {
}
}

initIncrementalBaseLocation();
resetIncrementalBaseLocation();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,26 @@ public interface IFindReplaceLogic {
*/
public boolean isWholeWordSearchAvailable(String findString);

/**
* Initializes the anchor used as the starting point for incremental searching.
* Subsequent incremental searches will start from the first letter of the
* currently selected range in the FindReplaceTarget.
*
* <p>
* The "current selection" refers to the range of text that is currently
* highlighted or selected within the FindReplaceTarget. This selection can be
* either a single position (if no range is selected) or a range of text.
*
* <p>
* When handling range selections:
* <ul>
* <li>Forward search operations will use the beginning of the selection as the
* starting point.</li>
* <li>Backward search operations will use the end of the selection as the
* starting point.</li>
* </ul>
* </p>
*/
void resetIncrementalBaseLocation();

}
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,7 @@ private void createSearchBar() {

@Override
public void focusGained(FocusEvent e) {
// we want to update the base-location of where we start incremental search
// to the currently selected position in the target
// when coming back into the dialog
findReplaceLogic.deactivate(SearchOptions.INCREMENTAL);
findReplaceLogic.activate(SearchOptions.INCREMENTAL);
findReplaceLogic.resetIncrementalBaseLocation();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,23 @@ public void testDontSelectAndReplaceIfFindNotSuccessful() {
assertThat(findReplaceLogic.getTarget().getSelection().y, is(4));
}

@Test
public void testResetIncrementalBaseLocation() {
String setupString= "test\ntest\ntest";
TextViewer textViewer= setupTextViewer(setupString);
textViewer.setSelectedRange(0, 0);
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
findReplaceLogic.activate(SearchOptions.FORWARD);
findReplaceLogic.activate(SearchOptions.WRAP);
findReplaceLogic.activate(SearchOptions.INCREMENTAL);
findReplaceLogic.performIncrementalSearch("test");
assertThat(textViewer.getSelectedRange(), is(new Point(0, 4)));
textViewer.setSelectedRange(5, 0);
findReplaceLogic.resetIncrementalBaseLocation();
findReplaceLogic.performIncrementalSearch("test");
assertThat(textViewer.getSelectedRange(), is(new Point(5, 4)));
}

private void expectStatusEmpty(IFindReplaceLogic findReplaceLogic) {
assertThat(findReplaceLogic.getStatus(), instanceOf(NoStatus.class));
}
Expand Down

0 comments on commit 56bca9c

Please sign in to comment.