From e983db854976445b47b58b5f1c004724794fd6d9 Mon Sep 17 00:00:00 2001 From: Maximilian Wittmer Date: Fri, 8 Mar 2024 14:14:56 +0100 Subject: [PATCH] Fix: "Search-scopes don't update #1740" Updates the search-scopes when a new selection is created. Also implements a regression-test to the FindReplaceDialogTest-class. Fixes https://github.com/eclipse-platform/eclipse.platform.ui/issues/1047 --- .../findandreplace/FindReplaceLogic.java | 43 +++++++++---------- .../findandreplace/FindReplaceLogicTest.java | 20 +++++++++ 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java index fadff8e1f21..777251b021b 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java @@ -50,7 +50,6 @@ public class FindReplaceLogic implements IFindReplaceLogic { private IFindReplaceStatus status; private IFindReplaceTarget target; - private IRegion oldScope; private Point incrementalBaseLocation; private boolean isTargetSupportingRegEx; @@ -65,7 +64,7 @@ public void activate(SearchOptions searchOption) { switch (searchOption) { case GLOBAL: - useSelectedLines(false); + unsetSearchScope(); break; case FORWARD: case INCREMENTAL: @@ -73,7 +72,7 @@ public void activate(SearchOptions searchOption) { initIncrementalBaseLocation(); } break; - // $CASES-OMITTED$ + // $CASES-OMITTED$ default: break; } @@ -86,7 +85,7 @@ public void deactivate(SearchOptions searchOption) { } if (searchOption == SearchOptions.GLOBAL) { - useSelectedLines(true); + initializeSearchScope(); } if (searchOption == SearchOptions.FORWARD && shouldInitIncrementalBaseLocation()) { @@ -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 true if selected lines should be used */ - private void useSelectedLines(boolean selectedLines) { + private void initializeSearchScope() { if (shouldInitIncrementalBaseLocation()) { initIncrementalBaseLocation(); } @@ -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); } /** diff --git a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogicTest.java b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogicTest.java index 3c8063d4d31..91a72c2d465 100644 --- a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogicTest.java +++ b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogicTest.java @@ -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; @@ -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)); }