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 ae03912e9d1..c353701a02f 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 @@ -18,6 +18,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import org.eclipse.swt.custom.BusyIndicator; @@ -593,13 +594,23 @@ public boolean performReplaceAndFind(String findString, String replaceString) { @Override public boolean performSelectAndReplace(String findString, String replaceString) { resetStatus(); - boolean needToSelectFirst = !getCurrentSelection().equals(findString); - if (needToSelectFirst) { + if (!isFindStringSelected(findString)) { performSearch(findString); } return replaceSelection(replaceString); } + private boolean isFindStringSelected(String findString) { + String selectedString = getCurrentSelection(); + if (isActive(SearchOptions.REGEX)) { + int patternFlags = isActive(SearchOptions.CASE_SENSITIVE) ? 0 : Pattern.CASE_INSENSITIVE; + Pattern pattern = Pattern.compile(findString, patternFlags); + return pattern.matcher(selectedString).find(); + } else { + return getCurrentSelection().equals(findString); + } + } + @Override public void updateTarget(IFindReplaceTarget newTarget, boolean canEditTarget) { resetStatus(); 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 7ae3ae22bdd..7cd79f44ea1 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 @@ -213,7 +213,6 @@ public void testPerformSelectAndReplaceBackward() { expectStatusEmpty(findReplaceLogic); } - @Test public void testPerformReplaceAndFind() { TextViewer textViewer= setupTextViewer("HelloWorld!"); @@ -237,6 +236,36 @@ public void testPerformReplaceAndFind() { expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH); } + @Test + public void testPerformReplaceAndFindRegEx() { + TextViewer textViewer= setupTextViewer("HelloWorld!!"); + IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer); + findReplaceLogic.activate(SearchOptions.FORWARD); + findReplaceLogic.activate(SearchOptions.REGEX); + + boolean status= findReplaceLogic.performReplaceAndFind("<(\\w*)>", " "); + assertThat(status, is(true)); + assertThat(textViewer.getDocument().get(), equalTo("Hello World!!")); + assertThat(findReplaceLogic.getTarget().getSelectionText(), equalTo("")); + expectStatusEmpty(findReplaceLogic); + + status= findReplaceLogic.performReplaceAndFind("<(\\w*)>", " "); + assertThat(status, is(true)); + assertThat(textViewer.getDocument().get(), equalTo("Hello World !!")); + assertThat(findReplaceLogic.getTarget().getSelectionText(), equalTo("")); + expectStatusEmpty(findReplaceLogic); + + status= findReplaceLogic.performReplaceAndFind("<(\\w*)>", " "); + assertThat(status, is(true)); + assertThat(textViewer.getDocument().get(), equalTo("Hello World ! !")); + expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH); + + status= findReplaceLogic.performReplaceAndFind("<(\\w*)>", " "); + assertEquals("Status wasn't correctly returned", false, status); + assertEquals("Text shouldn't have been changed", "Hello World ! !", textViewer.getDocument().get()); + expectStatusIsCode(findReplaceLogic, FindStatus.StatusCode.NO_MATCH); + } + @Test public void testPerformSelectAllForward() { TextViewer textViewer= setupTextViewer("AbAbAbAb");