Skip to content

Commit

Permalink
Fix: "Search-scopes don't update #1740"
Browse files Browse the repository at this point in the history
Updates the search-scopes when a new selection is created.
Also implements a regression-test to the FindReplaceDialogTest-class.

Fixes #1047
  • Loading branch information
Wittmaxi authored and HeikoKlare committed Mar 23, 2024
1 parent 9189478 commit e983db8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
Expand Up @@ -50,7 +50,6 @@
public class FindReplaceLogic implements IFindReplaceLogic {
private IFindReplaceStatus status;
private IFindReplaceTarget target;
private IRegion oldScope;
private Point incrementalBaseLocation;

private boolean isTargetSupportingRegEx;
Expand All @@ -65,15 +64,15 @@ public void activate(SearchOptions searchOption) {

switch (searchOption) {
case GLOBAL:
useSelectedLines(false);
unsetSearchScope();
break;
case FORWARD:
case INCREMENTAL:
if (shouldInitIncrementalBaseLocation()) {
initIncrementalBaseLocation();
}
break;
// $CASES-OMITTED$
// $CASES-OMITTED$
default:
break;
}
Expand All @@ -86,7 +85,7 @@ public void deactivate(SearchOptions searchOption) {
}

if (searchOption == SearchOptions.GLOBAL) {
useSelectedLines(true);
initializeSearchScope();
}

if (searchOption == SearchOptions.FORWARD && shouldInitIncrementalBaseLocation()) {
Expand Down Expand Up @@ -152,10 +151,8 @@ public boolean shouldInitIncrementalBaseLocation() {
/**
* Tells the dialog to perform searches only in the scope given by the actually
* selected lines.
*
* @param selectedLines <code>true</code> if selected lines should be used
*/
private void useSelectedLines(boolean selectedLines) {
private void initializeSearchScope() {
if (shouldInitIncrementalBaseLocation()) {
initIncrementalBaseLocation();
}
Expand All @@ -166,25 +163,27 @@ private void useSelectedLines(boolean selectedLines) {

IFindReplaceTargetExtension extensionTarget = (IFindReplaceTargetExtension) target;

if (selectedLines) {
IRegion scope;
Point lineSelection = extensionTarget.getLineSelection();
scope = new Region(lineSelection.x, lineSelection.y);

IRegion scope;
if (oldScope == null) {
Point lineSelection = extensionTarget.getLineSelection();
scope = new Region(lineSelection.x, lineSelection.y);
} else {
scope = oldScope;
oldScope = null;
}
int offset = isActive(SearchOptions.FORWARD) ? scope.getOffset() : scope.getOffset() + scope.getLength();

int offset = isActive(SearchOptions.FORWARD) ? scope.getOffset() : scope.getOffset() + scope.getLength();
extensionTarget.setSelection(offset, 0);
extensionTarget.setScope(scope);
}

extensionTarget.setSelection(offset, 0);
extensionTarget.setScope(scope);
} else {
oldScope = extensionTarget.getScope();
extensionTarget.setScope(null);
/**
* Unsets the search scope for a "Scoped"-Search.
*/
private void unsetSearchScope() {
if (target == null || !(target instanceof IFindReplaceTargetExtension)) {
return;
}

IFindReplaceTargetExtension extensionTarget = (IFindReplaceTargetExtension) target;

extensionTarget.setScope(null);
}

/**
Expand Down
Expand Up @@ -33,6 +33,7 @@

import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IFindReplaceTarget;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.text.TextViewer;

import org.eclipse.ui.internal.findandreplace.status.FindAllStatus;
Expand Down Expand Up @@ -432,6 +433,25 @@ public void testSelectWholeWords() {
expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH);
}

@Test
public void testSelectInSearchScope() {
TextViewer textViewer= setupTextViewer("line1\nline2\nline3");
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
textViewer.setSelection(new TextSelection(6, 11));
findReplaceLogic.deactivate(SearchOptions.GLOBAL);
findReplaceLogic.performReplaceAll("l", "", Display.getCurrent());
expectStatusIsReplaceAllWithCount(findReplaceLogic, 2);

findReplaceLogic.activate(SearchOptions.GLOBAL);
textViewer.setSelection(new TextSelection(0, 5));
findReplaceLogic.deactivate(SearchOptions.GLOBAL);

findReplaceLogic.performReplaceAll("n", "", Display.getCurrent());
expectStatusIsReplaceAllWithCount(findReplaceLogic, 1);

assertThat(textViewer.getTextWidget().getText(), is("lie1\nine2\nine3"));
}

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

0 comments on commit e983db8

Please sign in to comment.