Skip to content

Commit e983db8

Browse files
Maximilian WittmerHeikoKlare
authored andcommitted
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 #1047
1 parent 9189478 commit e983db8

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
public class FindReplaceLogic implements IFindReplaceLogic {
5151
private IFindReplaceStatus status;
5252
private IFindReplaceTarget target;
53-
private IRegion oldScope;
5453
private Point incrementalBaseLocation;
5554

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

6665
switch (searchOption) {
6766
case GLOBAL:
68-
useSelectedLines(false);
67+
unsetSearchScope();
6968
break;
7069
case FORWARD:
7170
case INCREMENTAL:
7271
if (shouldInitIncrementalBaseLocation()) {
7372
initIncrementalBaseLocation();
7473
}
7574
break;
76-
// $CASES-OMITTED$
75+
// $CASES-OMITTED$
7776
default:
7877
break;
7978
}
@@ -86,7 +85,7 @@ public void deactivate(SearchOptions searchOption) {
8685
}
8786

8887
if (searchOption == SearchOptions.GLOBAL) {
89-
useSelectedLines(true);
88+
initializeSearchScope();
9089
}
9190

9291
if (searchOption == SearchOptions.FORWARD && shouldInitIncrementalBaseLocation()) {
@@ -152,10 +151,8 @@ public boolean shouldInitIncrementalBaseLocation() {
152151
/**
153152
* Tells the dialog to perform searches only in the scope given by the actually
154153
* selected lines.
155-
*
156-
* @param selectedLines <code>true</code> if selected lines should be used
157154
*/
158-
private void useSelectedLines(boolean selectedLines) {
155+
private void initializeSearchScope() {
159156
if (shouldInitIncrementalBaseLocation()) {
160157
initIncrementalBaseLocation();
161158
}
@@ -166,25 +163,27 @@ private void useSelectedLines(boolean selectedLines) {
166163

167164
IFindReplaceTargetExtension extensionTarget = (IFindReplaceTargetExtension) target;
168165

169-
if (selectedLines) {
166+
IRegion scope;
167+
Point lineSelection = extensionTarget.getLineSelection();
168+
scope = new Region(lineSelection.x, lineSelection.y);
170169

171-
IRegion scope;
172-
if (oldScope == null) {
173-
Point lineSelection = extensionTarget.getLineSelection();
174-
scope = new Region(lineSelection.x, lineSelection.y);
175-
} else {
176-
scope = oldScope;
177-
oldScope = null;
178-
}
170+
int offset = isActive(SearchOptions.FORWARD) ? scope.getOffset() : scope.getOffset() + scope.getLength();
179171

180-
int offset = isActive(SearchOptions.FORWARD) ? scope.getOffset() : scope.getOffset() + scope.getLength();
172+
extensionTarget.setSelection(offset, 0);
173+
extensionTarget.setScope(scope);
174+
}
181175

182-
extensionTarget.setSelection(offset, 0);
183-
extensionTarget.setScope(scope);
184-
} else {
185-
oldScope = extensionTarget.getScope();
186-
extensionTarget.setScope(null);
176+
/**
177+
* Unsets the search scope for a "Scoped"-Search.
178+
*/
179+
private void unsetSearchScope() {
180+
if (target == null || !(target instanceof IFindReplaceTargetExtension)) {
181+
return;
187182
}
183+
184+
IFindReplaceTargetExtension extensionTarget = (IFindReplaceTargetExtension) target;
185+
186+
extensionTarget.setScope(null);
188187
}
189188

190189
/**

tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogicTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import org.eclipse.jface.text.Document;
3535
import org.eclipse.jface.text.IFindReplaceTarget;
36+
import org.eclipse.jface.text.TextSelection;
3637
import org.eclipse.jface.text.TextViewer;
3738

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

436+
@Test
437+
public void testSelectInSearchScope() {
438+
TextViewer textViewer= setupTextViewer("line1\nline2\nline3");
439+
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
440+
textViewer.setSelection(new TextSelection(6, 11));
441+
findReplaceLogic.deactivate(SearchOptions.GLOBAL);
442+
findReplaceLogic.performReplaceAll("l", "", Display.getCurrent());
443+
expectStatusIsReplaceAllWithCount(findReplaceLogic, 2);
444+
445+
findReplaceLogic.activate(SearchOptions.GLOBAL);
446+
textViewer.setSelection(new TextSelection(0, 5));
447+
findReplaceLogic.deactivate(SearchOptions.GLOBAL);
448+
449+
findReplaceLogic.performReplaceAll("n", "", Display.getCurrent());
450+
expectStatusIsReplaceAllWithCount(findReplaceLogic, 1);
451+
452+
assertThat(textViewer.getTextWidget().getText(), is("lie1\nine2\nine3"));
453+
}
454+
435455
private void expectStatusEmpty(IFindReplaceLogic findReplaceLogic) {
436456
assertThat(findReplaceLogic.getStatus(), instanceOf(NoStatus.class));
437457
}

0 commit comments

Comments
 (0)