Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find/Replace Logic: Allow reset of Incremental Base Position #2104

Merged
merged 1 commit into from
Jul 19, 2024
Merged
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 @@ -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() {
Wittmaxi marked this conversation as resolved.
Show resolved Hide resolved
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();
HeikoKlare marked this conversation as resolved.
Show resolved Hide resolved

}
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
Loading