Skip to content

Commit f391341

Browse files
Maximilian WittmerHeikoKlare
Maximilian Wittmer
authored andcommitted
Move activation logic for SearchOptions.INCREMENTAL to FindReplaceLogic
when "incremental search" is selected and "RegEx-Search" is as well, the dialog/overlay will disable the option for "whole words". This PR extracts that functionality into the Find/Replace-Logic. Extracted from a Review-Comment in eclipse-platform#1791
1 parent d03f4ff commit f391341

File tree

4 files changed

+77
-14
lines changed

4 files changed

+77
-14
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,15 @@ private void resetStatus() {
114114
status = null;
115115
}
116116

117+
@Override
118+
public boolean isIncrementalSearchAvailable() {
119+
return !isRegExSearchAvailableAndActive();
120+
}
121+
117122
@Override
118123
public boolean isWholeWordSearchAvailable(String findString) {
119124
return !isRegExSearchAvailableAndActive() && isWord(findString);
120125
}
121-
122126
/**
123127
* Tests whether each character in the given string is a letter.
124128
*
@@ -686,7 +690,7 @@ private void statusLineMessage(String message) {
686690
public void performIncrementalSearch(String searchString) {
687691
resetStatus();
688692

689-
if (isActive(SearchOptions.INCREMENTAL) && !isRegExSearchAvailableAndActive()) {
693+
if (isActive(SearchOptions.INCREMENTAL) && isIncrementalSearchAvailable()) {
690694
if (searchString.equals("") && target != null) { //$NON-NLS-1$
691695
// empty selection at base location
692696
int offset = incrementalBaseLocation.x;

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

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public interface IFindReplaceLogic {
6666
*/
6767
public boolean isRegExSearchAvailableAndActive();
6868

69+
/**
70+
* {@return whether incremental search may be performed by the
71+
* find/replace-logic based on the currently active options}
72+
*/
73+
public boolean isIncrementalSearchAvailable();
74+
6975
/**
7076
* Updates the search result after the Text was Modified. Used in combination
7177
* with <code>setIncrementalSearch(true)</code>. This method specifically allows

bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -725,11 +725,11 @@ public void widgetDefaultSelected(SelectionEvent e) {
725725
@Override
726726
public void widgetSelected(SelectionEvent e) {
727727
boolean newState = fIsRegExCheckBox.getSelection();
728-
fIncrementalCheckBox.setEnabled(!newState);
729728
setupFindReplaceLogic();
730729
storeSettings();
731730
updateButtonState();
732731
setContentAssistsEnablement(newState);
732+
fIncrementalCheckBox.setEnabled(findReplaceLogic.isIncrementalSearchAvailable());
733733
}
734734
});
735735
storeButtonWithMnemonicInMap(fIsRegExCheckBox);
@@ -740,7 +740,7 @@ public void widgetSelected(SelectionEvent e) {
740740
updateButtonState();
741741
}
742742
});
743-
fIncrementalCheckBox.setEnabled(!findReplaceLogic.isRegExSearchAvailableAndActive());
743+
fIncrementalCheckBox.setEnabled(findReplaceLogic.isIncrementalSearchAvailable());
744744
return panel;
745745
}
746746

@@ -1165,7 +1165,7 @@ public void updateTarget(IFindReplaceTarget target, boolean isTargetEditable, bo
11651165
}
11661166

11671167
if (okToUse(fIncrementalCheckBox)) {
1168-
fIncrementalCheckBox.setEnabled(!findReplaceLogic.isRegExSearchAvailableAndActive());
1168+
fIncrementalCheckBox.setEnabled(findReplaceLogic.isIncrementalSearchAvailable());
11691169
}
11701170

11711171
if (okToUse(fReplaceLabel)) {
@@ -1264,8 +1264,7 @@ private void setupFindReplaceLogic() {
12641264
activateInFindReplaceLogicIf(SearchOptions.CASE_SENSITIVE, fCaseCheckBox.getSelection());
12651265
activateInFindReplaceLogicIf(SearchOptions.REGEX, fIsRegExCheckBox.getSelection());
12661266
activateInFindReplaceLogicIf(SearchOptions.WHOLE_WORD, fWholeWordCheckBox.getSelection());
1267-
activateInFindReplaceLogicIf(SearchOptions.INCREMENTAL,
1268-
fIncrementalCheckBox.getEnabled() && fIncrementalCheckBox.getSelection());
1267+
activateInFindReplaceLogicIf(SearchOptions.INCREMENTAL, fIncrementalCheckBox.getSelection());
12691268
}
12701269

12711270
/**

tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/FindReplaceDialogTest.java

+61-7
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ private class DialogAccess {
9898

9999
private Runnable closeOperation;
100100

101-
DialogAccess(Accessor findReplaceDialogAccessor) {
101+
102+
DialogAccess(Accessor findReplaceDialogAccessor, boolean checkInitialConfiguration) {
102103
findReplaceLogic= (FindReplaceLogic) findReplaceDialogAccessor.get("findReplaceLogic");
103104
findCombo= (Combo) findReplaceDialogAccessor.get("fFindField");
104105
forwardRadioButton= (Button) findReplaceDialogAccessor.get("fForwardRadioButton");
@@ -111,7 +112,9 @@ private class DialogAccess {
111112
replaceFindButton= (Button) findReplaceDialogAccessor.get("fReplaceFindButton");
112113
shellRetriever= () -> ((Shell) findReplaceDialogAccessor.get("fActiveShell"));
113114
closeOperation= () -> findReplaceDialogAccessor.invoke("close", null);
114-
assertInitialConfiguration();
115+
if (checkInitialConfiguration) {
116+
assertInitialConfiguration();
117+
}
115118
}
116119

117120
void restoreInitialConfiguration() {
@@ -148,9 +151,13 @@ private void assertInitialConfiguration() {
148151
assertFalse(regExCheckBox.getSelection());
149152
}
150153

151-
void close() {
154+
void closeAndRestore() {
152155
restoreInitialConfiguration();
153156
assertInitialConfiguration();
157+
close();
158+
}
159+
160+
void close() {
154161
closeOperation.run();
155162
}
156163

@@ -171,7 +178,7 @@ private void openFindReplaceDialog() {
171178
Shell shell= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
172179
Accessor dialogAccessor= new Accessor("org.eclipse.ui.texteditor.FindReplaceDialog", getClass().getClassLoader(), new Object[] { shell });
173180
dialogAccessor.invoke("create", null);
174-
dialog= new DialogAccess(dialogAccessor);
181+
dialog= new DialogAccess(dialogAccessor, true);
175182
}
176183

177184
private void openTextViewerAndFindReplaceDialog() {
@@ -190,6 +197,10 @@ private void openTextViewer(String content) {
190197
}
191198

192199
private void openFindReplaceDialogForTextViewer() {
200+
openFindReplaceDialogForTextViewer(true);
201+
}
202+
203+
private void openFindReplaceDialogForTextViewer(boolean checkInitialConfiguration) {
193204
Accessor fFindReplaceAction;
194205
fFindReplaceAction= new Accessor("org.eclipse.ui.texteditor.FindReplaceAction", getClass().getClassLoader(),
195206
new Class[] { ResourceBundle.class, String.class, Shell.class, IFindReplaceTarget.class },
@@ -203,13 +214,13 @@ private void openFindReplaceDialogForTextViewer() {
203214
Accessor fFindReplaceDialogStubAccessor= new Accessor(fFindReplaceDialogStub, "org.eclipse.ui.texteditor.FindReplaceAction$FindReplaceDialogStub", getClass().getClassLoader());
204215

205216
Accessor dialogAccessor= new Accessor(fFindReplaceDialogStubAccessor.invoke("getDialog", null), "org.eclipse.ui.texteditor.FindReplaceDialog", getClass().getClassLoader());
206-
dialog= new DialogAccess(dialogAccessor);
217+
dialog= new DialogAccess(dialogAccessor, checkInitialConfiguration);
207218
}
208219

209220
@After
210221
public void tearDown() throws Exception {
211222
if (dialog != null) {
212-
dialog.close();
223+
dialog.closeAndRestore();
213224
dialog= null;
214225
}
215226

@@ -268,10 +279,11 @@ public void testFocusNotChangedWhenEnterPressed() {
268279
simulateEnterInFindInputField(false);
269280
dialog.ensureHasFocusOnGTK();
270281

271-
if (Util.isMac())
282+
if (Util.isMac()) {
272283
/* On the Mac, checkboxes only take focus if "Full Keyboard Access" is enabled in the System Preferences.
273284
* Let's not assume that someone pressed Ctrl+F7 on every test machine... */
274285
return;
286+
}
275287

276288
assertTrue(dialog.findCombo.isFocusControl());
277289

@@ -414,6 +426,48 @@ public void testActivateWholeWordsAndSearchForTwoWords() {
414426
assertFalse(dialog.wholeWordCheckBox.getEnabled());
415427
}
416428

429+
@Test
430+
public void testIncrementalSearchOnlyEnabledWhenAllowed() {
431+
openTextViewer("text text text");
432+
openFindReplaceDialogForTextViewer();
433+
434+
dialog.incrementalCheckBox.setSelection(true);
435+
select(dialog.regExCheckBox);
436+
runEventQueue();
437+
assertTrue(dialog.incrementalCheckBox.getSelection());
438+
assertFalse(dialog.incrementalCheckBox.getEnabled());
439+
}
440+
441+
/*
442+
* Test for https://github.com/eclipse-platform/eclipse.platform.ui/pull/1805#pullrequestreview-1993772378
443+
*/
444+
@Test
445+
public void testIncrementalSearchOptionRecoveredCorrectly() {
446+
openTextViewer("text text text");
447+
openFindReplaceDialogForTextViewer();
448+
449+
select(dialog.incrementalCheckBox);
450+
assertTrue(dialog.incrementalCheckBox.getSelection());
451+
assertTrue(dialog.incrementalCheckBox.getEnabled());
452+
453+
dialog.close();
454+
openFindReplaceDialogForTextViewer(false);
455+
456+
assertTrue(dialog.incrementalCheckBox.getSelection());
457+
assertTrue(dialog.incrementalCheckBox.getEnabled());
458+
459+
select(dialog.incrementalCheckBox);
460+
select(dialog.regExCheckBox);
461+
assertTrue(dialog.incrementalCheckBox.getSelection());
462+
assertFalse(dialog.incrementalCheckBox.getEnabled());
463+
464+
dialog.close();
465+
openFindReplaceDialogForTextViewer(false);
466+
467+
assertTrue(dialog.incrementalCheckBox.getSelection());
468+
assertFalse(dialog.incrementalCheckBox.getEnabled());
469+
}
470+
417471
private static void select(Button button) {
418472
button.setSelection(true);
419473
button.notifyListeners(SWT.Selection, null);

0 commit comments

Comments
 (0)