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 70cbc0a868c..074197dd49d 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 @@ -114,11 +114,15 @@ private void resetStatus() { status = null; } + @Override + public boolean isIncrementalSearchAvailable() { + return !isRegExSearchAvailableAndActive(); + } + @Override public boolean isWholeWordSearchAvailable(String findString) { return !isRegExSearchAvailableAndActive() && isWord(findString); } - /** * Tests whether each character in the given string is a letter. * @@ -686,7 +690,7 @@ private void statusLineMessage(String message) { public void performIncrementalSearch(String searchString) { resetStatus(); - if (isActive(SearchOptions.INCREMENTAL) && !isRegExSearchAvailableAndActive()) { + if (isActive(SearchOptions.INCREMENTAL) && isIncrementalSearchAvailable()) { if (searchString.equals("") && target != null) { //$NON-NLS-1$ // empty selection at base location int offset = incrementalBaseLocation.x; diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/IFindReplaceLogic.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/IFindReplaceLogic.java index 0c8fb9e430b..1af8eec56bd 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/IFindReplaceLogic.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/IFindReplaceLogic.java @@ -66,6 +66,12 @@ public interface IFindReplaceLogic { */ public boolean isRegExSearchAvailableAndActive(); + /** + * {@return whether incremental search may be performed by the + * find/replace-logic based on the currently active options} + */ + public boolean isIncrementalSearchAvailable(); + /** * Updates the search result after the Text was Modified. Used in combination * with setIncrementalSearch(true). This method specifically allows diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java index 7f4553f7183..77cfd132e6e 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java @@ -725,11 +725,11 @@ public void widgetDefaultSelected(SelectionEvent e) { @Override public void widgetSelected(SelectionEvent e) { boolean newState = fIsRegExCheckBox.getSelection(); - fIncrementalCheckBox.setEnabled(!newState); setupFindReplaceLogic(); storeSettings(); updateButtonState(); setContentAssistsEnablement(newState); + fIncrementalCheckBox.setEnabled(findReplaceLogic.isIncrementalSearchAvailable()); } }); storeButtonWithMnemonicInMap(fIsRegExCheckBox); @@ -740,7 +740,7 @@ public void widgetSelected(SelectionEvent e) { updateButtonState(); } }); - fIncrementalCheckBox.setEnabled(!findReplaceLogic.isRegExSearchAvailableAndActive()); + fIncrementalCheckBox.setEnabled(findReplaceLogic.isIncrementalSearchAvailable()); return panel; } @@ -1165,7 +1165,7 @@ public void updateTarget(IFindReplaceTarget target, boolean isTargetEditable, bo } if (okToUse(fIncrementalCheckBox)) { - fIncrementalCheckBox.setEnabled(!findReplaceLogic.isRegExSearchAvailableAndActive()); + fIncrementalCheckBox.setEnabled(findReplaceLogic.isIncrementalSearchAvailable()); } if (okToUse(fReplaceLabel)) { @@ -1264,8 +1264,7 @@ private void setupFindReplaceLogic() { activateInFindReplaceLogicIf(SearchOptions.CASE_SENSITIVE, fCaseCheckBox.getSelection()); activateInFindReplaceLogicIf(SearchOptions.REGEX, fIsRegExCheckBox.getSelection()); activateInFindReplaceLogicIf(SearchOptions.WHOLE_WORD, fWholeWordCheckBox.getSelection()); - activateInFindReplaceLogicIf(SearchOptions.INCREMENTAL, - fIncrementalCheckBox.getEnabled() && fIncrementalCheckBox.getSelection()); + activateInFindReplaceLogicIf(SearchOptions.INCREMENTAL, fIncrementalCheckBox.getSelection()); } /** diff --git a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/FindReplaceDialogTest.java b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/FindReplaceDialogTest.java index 749d17617fa..88362c87fb4 100644 --- a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/FindReplaceDialogTest.java +++ b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/FindReplaceDialogTest.java @@ -98,7 +98,8 @@ private class DialogAccess { private Runnable closeOperation; - DialogAccess(Accessor findReplaceDialogAccessor) { + + DialogAccess(Accessor findReplaceDialogAccessor, boolean checkInitialConfiguration) { findReplaceLogic= (FindReplaceLogic) findReplaceDialogAccessor.get("findReplaceLogic"); findCombo= (Combo) findReplaceDialogAccessor.get("fFindField"); forwardRadioButton= (Button) findReplaceDialogAccessor.get("fForwardRadioButton"); @@ -111,7 +112,9 @@ private class DialogAccess { replaceFindButton= (Button) findReplaceDialogAccessor.get("fReplaceFindButton"); shellRetriever= () -> ((Shell) findReplaceDialogAccessor.get("fActiveShell")); closeOperation= () -> findReplaceDialogAccessor.invoke("close", null); - assertInitialConfiguration(); + if (checkInitialConfiguration) { + assertInitialConfiguration(); + } } void restoreInitialConfiguration() { @@ -148,9 +151,13 @@ private void assertInitialConfiguration() { assertFalse(regExCheckBox.getSelection()); } - void close() { + void closeAndRestore() { restoreInitialConfiguration(); assertInitialConfiguration(); + close(); + } + + void close() { closeOperation.run(); } @@ -171,7 +178,7 @@ private void openFindReplaceDialog() { Shell shell= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); Accessor dialogAccessor= new Accessor("org.eclipse.ui.texteditor.FindReplaceDialog", getClass().getClassLoader(), new Object[] { shell }); dialogAccessor.invoke("create", null); - dialog= new DialogAccess(dialogAccessor); + dialog= new DialogAccess(dialogAccessor, true); } private void openTextViewerAndFindReplaceDialog() { @@ -190,6 +197,10 @@ private void openTextViewer(String content) { } private void openFindReplaceDialogForTextViewer() { + openFindReplaceDialogForTextViewer(true); + } + + private void openFindReplaceDialogForTextViewer(boolean checkInitialConfiguration) { Accessor fFindReplaceAction; fFindReplaceAction= new Accessor("org.eclipse.ui.texteditor.FindReplaceAction", getClass().getClassLoader(), new Class[] { ResourceBundle.class, String.class, Shell.class, IFindReplaceTarget.class }, @@ -203,13 +214,13 @@ private void openFindReplaceDialogForTextViewer() { Accessor fFindReplaceDialogStubAccessor= new Accessor(fFindReplaceDialogStub, "org.eclipse.ui.texteditor.FindReplaceAction$FindReplaceDialogStub", getClass().getClassLoader()); Accessor dialogAccessor= new Accessor(fFindReplaceDialogStubAccessor.invoke("getDialog", null), "org.eclipse.ui.texteditor.FindReplaceDialog", getClass().getClassLoader()); - dialog= new DialogAccess(dialogAccessor); + dialog= new DialogAccess(dialogAccessor, checkInitialConfiguration); } @After public void tearDown() throws Exception { if (dialog != null) { - dialog.close(); + dialog.closeAndRestore(); dialog= null; } @@ -268,10 +279,11 @@ public void testFocusNotChangedWhenEnterPressed() { simulateEnterInFindInputField(false); dialog.ensureHasFocusOnGTK(); - if (Util.isMac()) + if (Util.isMac()) { /* On the Mac, checkboxes only take focus if "Full Keyboard Access" is enabled in the System Preferences. * Let's not assume that someone pressed Ctrl+F7 on every test machine... */ return; + } assertTrue(dialog.findCombo.isFocusControl()); @@ -414,6 +426,48 @@ public void testActivateWholeWordsAndSearchForTwoWords() { assertFalse(dialog.wholeWordCheckBox.getEnabled()); } + @Test + public void testIncrementalSearchOnlyEnabledWhenAllowed() { + openTextViewer("text text text"); + openFindReplaceDialogForTextViewer(); + + dialog.incrementalCheckBox.setSelection(true); + select(dialog.regExCheckBox); + runEventQueue(); + assertTrue(dialog.incrementalCheckBox.getSelection()); + assertFalse(dialog.incrementalCheckBox.getEnabled()); + } + + /* + * Test for https://github.com/eclipse-platform/eclipse.platform.ui/pull/1805#pullrequestreview-1993772378 + */ + @Test + public void testIncrementalSearchOptionRecoveredCorrectly() { + openTextViewer("text text text"); + openFindReplaceDialogForTextViewer(); + + select(dialog.incrementalCheckBox); + assertTrue(dialog.incrementalCheckBox.getSelection()); + assertTrue(dialog.incrementalCheckBox.getEnabled()); + + dialog.close(); + openFindReplaceDialogForTextViewer(false); + + assertTrue(dialog.incrementalCheckBox.getSelection()); + assertTrue(dialog.incrementalCheckBox.getEnabled()); + + select(dialog.incrementalCheckBox); + select(dialog.regExCheckBox); + assertTrue(dialog.incrementalCheckBox.getSelection()); + assertFalse(dialog.incrementalCheckBox.getEnabled()); + + dialog.close(); + openFindReplaceDialogForTextViewer(false); + + assertTrue(dialog.incrementalCheckBox.getSelection()); + assertFalse(dialog.incrementalCheckBox.getEnabled()); + } + private static void select(Button button) { button.setSelection(true); button.notifyListeners(SWT.Selection, null);