Skip to content

Commit 1b2426f

Browse files
committed
Find/replace logic: proper replace for case-insensitive matches
When using find/replace (via overlay or dialog) in incremental mode, if the currently found element is only a case-insensitive match with the current search string, a replace operation will perform an unnecessary additional search and thus replace the next matching element. The reason is a comparison for whether the currently found string exactly matches the search string, not ignoring the casing. This change adapts the behavior to properly consider case-sensitivity when performing replace operations. It also adds according regression tests.
1 parent 901a83e commit 1b2426f

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,10 @@ private boolean isFindStringSelected() {
568568
Pattern pattern = Pattern.compile(findString, patternFlags);
569569
return pattern.matcher(selectedString).find();
570570
} else {
571-
return getCurrentSelection().equals(findString);
571+
if (isAvailableAndActive(SearchOptions.CASE_SENSITIVE)) {
572+
return getCurrentSelection().equals(findString);
573+
}
574+
return getCurrentSelection().equalsIgnoreCase(findString);
572575
}
573576
}
574577

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,25 +348,71 @@ public void testPerformSelectAndReplaceBackward() {
348348
}
349349

350350
@Test
351-
public void testPerformReplaceAndFind() {
351+
public void testPerformReplaceAndFind_caseInsensitive() {
352352
TextViewer textViewer= setupTextViewer("Hello<replace>World<replace>!");
353353
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
354354
findReplaceLogic.activate(SearchOptions.FORWARD);
355+
setFindAndReplaceString(findReplaceLogic, "<Replace>", " ");
356+
357+
boolean status= findReplaceLogic.performReplaceAndFind();
358+
assertTrue("replace should have been performed", status);
359+
assertThat(textViewer.getDocument().get(), equalTo("Hello World<replace>!"));
360+
assertThat(findReplaceLogic.getTarget().getSelectionText(), equalTo("<replace>"));
361+
expectStatusEmpty(findReplaceLogic);
362+
363+
setFindAndReplaceString(findReplaceLogic, "<replace>", " ");
364+
status= findReplaceLogic.performReplaceAndFind();
365+
assertTrue("replace should have been performed", status);
366+
assertThat(textViewer.getDocument().get(), equalTo("Hello World !"));
367+
expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH);
368+
369+
status= findReplaceLogic.performReplaceAndFind();
370+
assertFalse("replace should not have been performed", status);
371+
assertEquals("Text shouldn't have been changed", "Hello World !", textViewer.getDocument().get());
372+
expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH);
373+
}
374+
375+
@Test
376+
public void testPerformReplaceAndFind_caseSensitive() {
377+
TextViewer textViewer= setupTextViewer("Hello<Replace>World<replace>!");
378+
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
379+
findReplaceLogic.activate(SearchOptions.FORWARD);
380+
findReplaceLogic.activate(SearchOptions.CASE_SENSITIVE);
355381
setFindAndReplaceString(findReplaceLogic, "<replace>", " ");
356382

357383
boolean status= findReplaceLogic.performReplaceAndFind();
358-
assertThat(status, is(true));
384+
assertTrue("replace should have been performed", status);
385+
assertThat(textViewer.getDocument().get(), equalTo("Hello<Replace>World !"));
386+
assertThat(findReplaceLogic.getTarget().getSelectionText(), equalTo(" "));
387+
388+
status= findReplaceLogic.performReplaceAndFind();
389+
assertFalse("replace should not have been performed", status);
390+
assertThat(textViewer.getDocument().get(), equalTo("Hello<Replace>World !"));
391+
assertThat(findReplaceLogic.getTarget().getSelectionText(), equalTo(" "));
392+
}
393+
394+
@Test
395+
public void testPerformReplaceAndFind_caseSensitiveAndIncremental() {
396+
TextViewer textViewer= setupTextViewer("Hello<replace>World<replace>!");
397+
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
398+
findReplaceLogic.activate(SearchOptions.FORWARD);
399+
findReplaceLogic.activate(SearchOptions.INCREMENTAL);
400+
setFindAndReplaceString(findReplaceLogic, "<Replace>", " ");
401+
402+
boolean status= findReplaceLogic.performReplaceAndFind();
403+
assertTrue("replace should have been performed", status);
359404
assertThat(textViewer.getDocument().get(), equalTo("Hello World<replace>!"));
360405
assertThat(findReplaceLogic.getTarget().getSelectionText(), equalTo("<replace>"));
361406
expectStatusEmpty(findReplaceLogic);
362407

408+
setFindAndReplaceString(findReplaceLogic, "<replace>", " ");
363409
status= findReplaceLogic.performReplaceAndFind();
364-
assertThat(status, is(true));
410+
assertTrue("replace should have been performed", status);
365411
assertThat(textViewer.getDocument().get(), equalTo("Hello World !"));
366412
expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH);
367413

368414
status= findReplaceLogic.performReplaceAndFind();
369-
assertEquals("Status wasn't correctly returned", false, status);
415+
assertFalse("replace should not have been performed", status);
370416
assertEquals("Text shouldn't have been changed", "Hello World !", textViewer.getDocument().get());
371417
expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH);
372418
}

0 commit comments

Comments
 (0)