Skip to content

Commit

Permalink
Find/Replace Logic: Allow reset of Incremental Base Position
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 authored and HeikoKlare committed Jul 19, 2024
1 parent 8534560 commit ecb803a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 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 @@ -141,7 +141,8 @@ public boolean isRegExSearchAvailableAndActive() {
* 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 +160,7 @@ public boolean shouldInitIncrementalBaseLocation() {
*/
private void initializeSearchScope() {
if (shouldInitIncrementalBaseLocation()) {
initIncrementalBaseLocation();
resetIncrementalBaseLocation();
}

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

Expand Down Expand Up @@ -604,7 +605,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,10 @@ public interface IFindReplaceLogic {
*/
public boolean isWholeWordSearchAvailable(String findString);

/**
* Initializes the anchor used as starting point for incremental searching.
* Subsequent incremental searches will start from the current selection.
*/
void resetIncrementalBaseLocation();

}
Original file line number Diff line number Diff line change
Expand Up @@ -611,11 +611,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 ecb803a

Please sign in to comment.