Skip to content

Commit 781d89e

Browse files
Maximilian WittmerHeikoKlare
Maximilian Wittmer
authored andcommitted
Move activation logic for SearchOptions.WHOLE_WORDS to FindReplaceLogic
when "whole words" is selected but a find-string was entered that is not a whole word, the dialog/overlay will disable the option for "whole words". However, the dialog/overlay will keep the selection of the button in it's internal state. By passing the findString to this method, we can make sure that we don't try to perform a whole-words search for a string that isn't a whole word. See eclipse-platform#1192 (comment) Extracted from eclipse-platform#1192
1 parent 81709ca commit 781d89e

File tree

5 files changed

+80
-36
lines changed

5 files changed

+80
-36
lines changed

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

+22-14
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,19 @@ private void resetStatus() {
114114
status = null;
115115
}
116116

117+
@Override
118+
public boolean isWholeWordSearchAvailable(String findString) {
119+
return !isRegExSearchAvailableAndActive() && isWord(findString);
120+
}
121+
117122
/**
118-
* Returns <code>true</code> if searching should be restricted to entire words,
119-
* <code>false</code> if not. This is the case if the respective checkbox is
120-
* turned on, regex is off, and the checkbox is enabled, i.e. the current find
121-
* string is an entire word.
123+
* Tests whether each character in the given string is a letter.
122124
*
123-
* @return <code>true</code> if the search is restricted to whole words
125+
* @param str the string to check
126+
* @return <code>true</code> if the given string is a word
124127
*/
125-
private boolean isWholeWordSearchAvailableAndActive() {
126-
return isActive(SearchOptions.WHOLE_WORD) && !isRegExSearchAvailableAndActive();
128+
private static boolean isWord(String str) {
129+
return str != null && !str.isEmpty() && str.chars().allMatch(Character::isJavaIdentifierPart);
127130
}
128131

129132
@Override
@@ -495,13 +498,18 @@ private int findIndex(String findString, int startPosition) {
495498

496499
@Override
497500
public int findAndSelect(int offset, String findString) {
498-
if (target instanceof IFindReplaceTargetExtension3)
499-
return ((IFindReplaceTargetExtension3) target).findAndSelect(offset, findString,
500-
isActive(SearchOptions.FORWARD),
501-
isActive(SearchOptions.CASE_SENSITIVE), isWholeWordSearchAvailableAndActive(), isActive(SearchOptions.REGEX));
502-
return target.findAndSelect(offset, findString, isActive(SearchOptions.FORWARD),
503-
isActive(SearchOptions.CASE_SENSITIVE),
504-
isWholeWordSearchAvailableAndActive());
501+
boolean wholeWordSearch = isActive(SearchOptions.WHOLE_WORD) && isWholeWordSearchAvailable(findString);
502+
boolean forwardSearch = isActive(SearchOptions.FORWARD);
503+
boolean caseSensitiveSearch = isActive(SearchOptions.CASE_SENSITIVE);
504+
boolean regexSearch = isActive(SearchOptions.REGEX);
505+
506+
if (target instanceof IFindReplaceTargetExtension3 regexSupportingTarget) {
507+
return (regexSupportingTarget).findAndSelect(offset, findString,
508+
forwardSearch, caseSensitiveSearch,
509+
wholeWordSearch, regexSearch);
510+
}
511+
return target.findAndSelect(offset, findString, forwardSearch,
512+
caseSensitiveSearch, wholeWordSearch);
505513
}
506514

507515
/**

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

+10
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,14 @@ public interface IFindReplaceLogic {
166166
*/
167167
public IFindReplaceTarget getTarget();
168168

169+
/**
170+
* Returns <code>true</code> if searching can be restricted to entire words,
171+
* <code>false</code> if not. Searching for whole words requires the given find
172+
* string to be an entire word and the regex search option to be disabled.
173+
*
174+
* @param findString the string that is currently being searched for.
175+
* @return <code>true</code> if the search can be restricted to whole words
176+
*/
177+
public boolean isWholeWordSearchAvailable(String findString);
178+
169179
}

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

+4-22
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ public void widgetSelected(SelectionEvent e) {
733733
}
734734
});
735735
storeButtonWithMnemonicInMap(fIsRegExCheckBox);
736-
fWholeWordCheckBox.setEnabled(!findReplaceLogic.isRegExSearchAvailableAndActive());
736+
fWholeWordCheckBox.setEnabled(findReplaceLogic.isWholeWordSearchAvailable(getFindString()));
737737
fWholeWordCheckBox.addSelectionListener(new SelectionAdapter() {
738738
@Override
739739
public void widgetSelected(SelectionEvent e) {
@@ -1065,7 +1065,7 @@ private void updateButtonState(boolean disableReplace) {
10651065
boolean isSelectionGoodForReplace = selectionString != "" //$NON-NLS-1$
10661066
|| !isRegExSearchAvailableAndActive;
10671067

1068-
fWholeWordCheckBox.setEnabled(isWord(str) && !isRegExSearchAvailableAndActive);
1068+
fWholeWordCheckBox.setEnabled(findReplaceLogic.isWholeWordSearchAvailable(getFindString()));
10691069
fFindNextButton.setEnabled(enable && isFindStringSet);
10701070
fSelectAllButton.setEnabled(enable && isFindStringSet && (target instanceof IFindReplaceTargetExtension4));
10711071
fReplaceSelectionButton.setEnabled(
@@ -1076,23 +1076,6 @@ private void updateButtonState(boolean disableReplace) {
10761076
}
10771077
}
10781078

1079-
/**
1080-
* Tests whether each character in the given string is a letter.
1081-
*
1082-
* @param str the string to check
1083-
* @return <code>true</code> if the given string is a word
1084-
* @since 3.0
1085-
*/
1086-
private boolean isWord(String str) {
1087-
if (str == null || str.isEmpty())
1088-
return false;
1089-
1090-
for (int i = 0; i < str.length(); i++) {
1091-
if (!Character.isJavaIdentifierPart(str.charAt(i)))
1092-
return false;
1093-
}
1094-
return true;
1095-
}
10961079

10971080
/**
10981081
* Updates the given combo with the given content.
@@ -1178,7 +1161,7 @@ public void updateTarget(IFindReplaceTarget target, boolean isTargetEditable, bo
11781161
}
11791162

11801163
if (okToUse(fWholeWordCheckBox)) {
1181-
fWholeWordCheckBox.setEnabled(!findReplaceLogic.isRegExSearchAvailableAndActive());
1164+
fWholeWordCheckBox.setEnabled(findReplaceLogic.isWholeWordSearchAvailable(getFindString()));
11821165
}
11831166

11841167
if (okToUse(fIncrementalCheckBox)) {
@@ -1280,8 +1263,7 @@ private void setupFindReplaceLogic() {
12801263
activateInFindReplaceLogicIf(SearchOptions.FORWARD, fForwardRadioButton.getSelection());
12811264
activateInFindReplaceLogicIf(SearchOptions.CASE_SENSITIVE, fCaseCheckBox.getSelection());
12821265
activateInFindReplaceLogicIf(SearchOptions.REGEX, fIsRegExCheckBox.getSelection());
1283-
activateInFindReplaceLogicIf(SearchOptions.WHOLE_WORD,
1284-
fWholeWordCheckBox.getEnabled() && fWholeWordCheckBox.getSelection());
1266+
activateInFindReplaceLogicIf(SearchOptions.WHOLE_WORD, fWholeWordCheckBox.getSelection());
12851267
activateInFindReplaceLogicIf(SearchOptions.INCREMENTAL,
12861268
fIncrementalCheckBox.getEnabled() && fIncrementalCheckBox.getSelection());
12871269
}

tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogicTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,28 @@ public void testSelectInSearchScope() {
452452
assertThat(textViewer.getTextWidget().getText(), is("lie1\nine2\nine3"));
453453
}
454454

455+
@Test
456+
public void testWholeWordSearchAvailable() {
457+
TextViewer textViewer= setupTextViewer("line1\nline2\nline3");
458+
IFindReplaceLogic findReplaceLogic= setupFindReplaceLogicObject(textViewer);
459+
460+
assertThat(findReplaceLogic.isWholeWordSearchAvailable("oneword"), is(true));
461+
assertThat(findReplaceLogic.isWholeWordSearchAvailable("stilläoneäword"), is(true));
462+
assertThat(findReplaceLogic.isWholeWordSearchAvailable("two.words"), is(false));
463+
assertThat(findReplaceLogic.isWholeWordSearchAvailable("two words"), is(false));
464+
assertThat(findReplaceLogic.isWholeWordSearchAvailable("oneword"), is(true));
465+
assertThat(findReplaceLogic.isWholeWordSearchAvailable("twöwords"), is(true));
466+
467+
findReplaceLogic.activate(SearchOptions.REGEX);
468+
469+
assertThat(findReplaceLogic.isWholeWordSearchAvailable("oneword"), is(false));
470+
assertThat(findReplaceLogic.isWholeWordSearchAvailable("stilläoneöword"), is(false));
471+
assertThat(findReplaceLogic.isWholeWordSearchAvailable("two.words"), is(false));
472+
assertThat(findReplaceLogic.isWholeWordSearchAvailable("two words"), is(false));
473+
474+
assertThat(findReplaceLogic.isWholeWordSearchAvailable(""), is(false));
475+
}
476+
455477
private void expectStatusEmpty(IFindReplaceLogic findReplaceLogic) {
456478
assertThat(findReplaceLogic.getStatus(), instanceOf(NoStatus.class));
457479
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,28 @@ public void testReplaceAndFindAfterInitializingFindWithSelectedString() {
392392
assertEquals(4, (target.getSelection()).y);
393393
}
394394

395+
@Test
396+
public void testActivateWholeWordsAndSearchForTwoWords() {
397+
openTextViewer("text text text");
398+
openFindReplaceDialogForTextViewer();
399+
400+
dialog.wholeWordCheckBox.setSelection(true);
401+
402+
dialog.findCombo.setText("text text");
403+
assertTrue(dialog.wholeWordCheckBox.getSelection());
404+
assertFalse(dialog.wholeWordCheckBox.getEnabled());
405+
406+
dialog.findCombo.setText("text");
407+
assertTrue(dialog.wholeWordCheckBox.getSelection());
408+
assertTrue(dialog.wholeWordCheckBox.getEnabled());
409+
410+
dialog.regExCheckBox.setSelection(true);
411+
dialog.regExCheckBox.notifyListeners(SWT.Selection, null);
412+
runEventQueue();
413+
assertTrue(dialog.wholeWordCheckBox.getSelection());
414+
assertFalse(dialog.wholeWordCheckBox.getEnabled());
415+
}
416+
395417
private static void select(Button button) {
396418
button.setSelection(true);
397419
button.notifyListeners(SWT.Selection, null);

0 commit comments

Comments
 (0)