Skip to content

Commit

Permalink
Find/replace logic: proper replace for case-insensitive matches
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
HeikoKlare committed Oct 2, 2024
1 parent 901a83e commit 1b2426f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,10 @@ private boolean isFindStringSelected() {
Pattern pattern = Pattern.compile(findString, patternFlags);
return pattern.matcher(selectedString).find();
} else {
return getCurrentSelection().equals(findString);
if (isAvailableAndActive(SearchOptions.CASE_SENSITIVE)) {
return getCurrentSelection().equals(findString);
}
return getCurrentSelection().equalsIgnoreCase(findString);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,25 +348,71 @@ public void testPerformSelectAndReplaceBackward() {
}

@Test
public void testPerformReplaceAndFind() {
public void testPerformReplaceAndFind_caseInsensitive() {
TextViewer textViewer= setupTextViewer("Hello<replace>World<replace>!");
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
findReplaceLogic.activate(SearchOptions.FORWARD);
setFindAndReplaceString(findReplaceLogic, "<Replace>", " ");

boolean status= findReplaceLogic.performReplaceAndFind();
assertTrue("replace should have been performed", status);
assertThat(textViewer.getDocument().get(), equalTo("Hello World<replace>!"));
assertThat(findReplaceLogic.getTarget().getSelectionText(), equalTo("<replace>"));
expectStatusEmpty(findReplaceLogic);

setFindAndReplaceString(findReplaceLogic, "<replace>", " ");
status= findReplaceLogic.performReplaceAndFind();
assertTrue("replace should have been performed", status);
assertThat(textViewer.getDocument().get(), equalTo("Hello World !"));
expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH);

status= findReplaceLogic.performReplaceAndFind();
assertFalse("replace should not have been performed", status);
assertEquals("Text shouldn't have been changed", "Hello World !", textViewer.getDocument().get());
expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH);
}

@Test
public void testPerformReplaceAndFind_caseSensitive() {
TextViewer textViewer= setupTextViewer("Hello<Replace>World<replace>!");
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
findReplaceLogic.activate(SearchOptions.FORWARD);
findReplaceLogic.activate(SearchOptions.CASE_SENSITIVE);
setFindAndReplaceString(findReplaceLogic, "<replace>", " ");

boolean status= findReplaceLogic.performReplaceAndFind();
assertThat(status, is(true));
assertTrue("replace should have been performed", status);
assertThat(textViewer.getDocument().get(), equalTo("Hello<Replace>World !"));
assertThat(findReplaceLogic.getTarget().getSelectionText(), equalTo(" "));

status= findReplaceLogic.performReplaceAndFind();
assertFalse("replace should not have been performed", status);
assertThat(textViewer.getDocument().get(), equalTo("Hello<Replace>World !"));
assertThat(findReplaceLogic.getTarget().getSelectionText(), equalTo(" "));
}

@Test
public void testPerformReplaceAndFind_caseSensitiveAndIncremental() {
TextViewer textViewer= setupTextViewer("Hello<replace>World<replace>!");
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
findReplaceLogic.activate(SearchOptions.FORWARD);
findReplaceLogic.activate(SearchOptions.INCREMENTAL);
setFindAndReplaceString(findReplaceLogic, "<Replace>", " ");

boolean status= findReplaceLogic.performReplaceAndFind();
assertTrue("replace should have been performed", status);
assertThat(textViewer.getDocument().get(), equalTo("Hello World<replace>!"));
assertThat(findReplaceLogic.getTarget().getSelectionText(), equalTo("<replace>"));
expectStatusEmpty(findReplaceLogic);

setFindAndReplaceString(findReplaceLogic, "<replace>", " ");
status= findReplaceLogic.performReplaceAndFind();
assertThat(status, is(true));
assertTrue("replace should have been performed", status);
assertThat(textViewer.getDocument().get(), equalTo("Hello World !"));
expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH);

status= findReplaceLogic.performReplaceAndFind();
assertEquals("Status wasn't correctly returned", false, status);
assertFalse("replace should not have been performed", status);
assertEquals("Text shouldn't have been changed", "Hello World !", textViewer.getDocument().get());
expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH);
}
Expand Down

0 comments on commit 1b2426f

Please sign in to comment.