Skip to content

Commit

Permalink
Find/replace: fix search operation during replace in RegEx mode eclip…
Browse files Browse the repository at this point in the history
…se-platform#2203

When performing a search operation in the FindReplaceLogic, it currently
always validates compatibility of the search options. In particular,
having incremental search and regex search enabled at the same time will
result in search operations not being performed. This results in
problems when performing replace operations that also include search
operations, as the search operation will not be executed then.

With this change, the search operations performed while doing a replace
will not validate the search operations but always perform the requested
search instead.

Fixes eclipse-platform#2203
  • Loading branch information
HeikoKlare committed Aug 19, 2024
1 parent e69252a commit 0bf8ce2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,13 @@ private boolean replaceSelection(String replaceString) {

@Override
public boolean performSearch(String findString) {
return performSearch(findString, true);
}

private boolean performSearch(String findString, boolean validateSearchOptions) {
resetStatus();

if (isActive(SearchOptions.INCREMENTAL) && !isIncrementalSearchAvailable()) {
if (validateSearchOptions && (isActive(SearchOptions.INCREMENTAL) && !isIncrementalSearchAvailable())) {
return false; // Do nothing if search options are not compatible
}
boolean somethingFound = false;
Expand Down Expand Up @@ -543,7 +547,7 @@ private int calculateFindBeginningOffset() {
public boolean performReplaceAndFind(String findString, String replaceString) {
resetStatus();
if (performSelectAndReplace(findString, replaceString)) {
performSearch(findString);
performSearch(findString, false);
return true;
}
return false;
Expand All @@ -553,7 +557,7 @@ public boolean performReplaceAndFind(String findString, String replaceString) {
public boolean performSelectAndReplace(String findString, String replaceString) {
resetStatus();
if (!isFindStringSelected(findString)) {
performSearch(findString);
performSearch(findString, false);
}
if (getStatus().wasSuccessful()) {
return replaceSelection(replaceString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,21 @@ public void testPerformReplaceAndFindRegEx() {
findReplaceLogic.activate(SearchOptions.FORWARD);
findReplaceLogic.activate(SearchOptions.REGEX);

executeReplaceAndFindRegExTest(textViewer, findReplaceLogic);
}

@Test
public void testPerformReplaceAndFindRegEx_incrementalActive() {
TextViewer textViewer= setupTextViewer("Hello<replace>World<replace>!<r>!");
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
findReplaceLogic.activate(SearchOptions.FORWARD);
findReplaceLogic.activate(SearchOptions.INCREMENTAL);
findReplaceLogic.activate(SearchOptions.REGEX);

executeReplaceAndFindRegExTest(textViewer, findReplaceLogic);
}

private void executeReplaceAndFindRegExTest(TextViewer textViewer, IFindReplaceLogic findReplaceLogic) {
boolean status= findReplaceLogic.performReplaceAndFind("<(\\w*)>", " ");
assertTrue(status);
assertThat(textViewer.getDocument().get(), equalTo("Hello World<replace>!<r>!"));
Expand All @@ -374,6 +389,22 @@ public void testPerformReplaceAndFindRegEx() {
expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH);
}

@Test
public void testPerformSearchAndReplaceRegEx_incrementalActive() {
TextViewer textViewer= setupTextViewer("some text");
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
findReplaceLogic.activate(SearchOptions.FORWARD);
findReplaceLogic.activate(SearchOptions.INCREMENTAL);
findReplaceLogic.activate(SearchOptions.REGEX);

findReplaceLogic.performSearch("text");
textViewer.setSelectedRange(0, 0);

findReplaceLogic.performSelectAndReplace("text", "");

assertEquals("some ", textViewer.getDocument().get());
}

@Test
public void testPerformSelectAllForward() {
TextViewer textViewer= setupTextViewer("AbAbAbAb");
Expand Down

0 comments on commit 0bf8ce2

Please sign in to comment.