Skip to content

Commit 08b804b

Browse files
committed
Find/replace: unify search options availability and activity retrieval
The FindReplaceLogic provides different methods for retrieving the active/available state of different search options. This change unifies the state retrieval by providing an isAvailable() and isAvailableAndActive() method accepting a search option instance, such that the information can be retrieved for any search option of interest in a unform way.
1 parent cd670d0 commit 08b804b

File tree

6 files changed

+69
-59
lines changed

6 files changed

+69
-59
lines changed

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

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class FindReplaceLogic implements IFindReplaceLogic {
6161
@Override
6262
public void setFindString(String findString) {
6363
this.findString = Objects.requireNonNull(findString);
64-
if (isIncrementalSearchAvailable() && isActive(SearchOptions.INCREMENTAL)) {
64+
if (isAvailableAndActive(SearchOptions.INCREMENTAL)) {
6565
performSearch(true);
6666
}
6767
}
@@ -130,14 +130,28 @@ private void resetStatus() {
130130
}
131131

132132
@Override
133-
public boolean isIncrementalSearchAvailable() {
134-
return !isRegExSearchAvailableAndActive();
133+
public boolean isAvailable(SearchOptions searchOption) {
134+
switch (searchOption) {
135+
case INCREMENTAL:
136+
return !isAvailableAndActive(SearchOptions.REGEX);
137+
case REGEX:
138+
return isTargetSupportingRegEx;
139+
case WHOLE_WORD:
140+
return !isAvailableAndActive(SearchOptions.REGEX) && isWord(findString);
141+
case CASE_SENSITIVE:
142+
case FORWARD:
143+
case GLOBAL:
144+
case WRAP:
145+
default:
146+
return true;
147+
}
135148
}
136149

137150
@Override
138-
public boolean isWholeWordSearchAvailable() {
139-
return !isRegExSearchAvailableAndActive() && isWord(findString);
151+
public boolean isAvailableAndActive(SearchOptions searchOption) {
152+
return isAvailable(searchOption) && isActive(searchOption);
140153
}
154+
141155
/**
142156
* Tests whether each character in the given string is a letter.
143157
*
@@ -148,11 +162,6 @@ private static boolean isWord(String str) {
148162
return str != null && str.chars().allMatch(Character::isJavaIdentifierPart);
149163
}
150164

151-
@Override
152-
public boolean isRegExSearchAvailableAndActive() {
153-
return isActive(SearchOptions.REGEX) && isTargetSupportingRegEx;
154-
}
155-
156165
@Override
157166
public void resetIncrementalBaseLocation() {
158167
if (target != null && shouldInitIncrementalBaseLocation()) {
@@ -185,7 +194,8 @@ private void initializeSearchScope() {
185194
Point lineSelection = extensionTarget.getLineSelection();
186195
scope = new Region(lineSelection.x, lineSelection.y);
187196

188-
int offset = isActive(SearchOptions.FORWARD) ? scope.getOffset() : scope.getOffset() + scope.getLength();
197+
int offset = isAvailableAndActive(SearchOptions.FORWARD) ? scope.getOffset()
198+
: scope.getOffset() + scope.getLength();
189199

190200
extensionTarget.setSelection(offset, 0);
191201
extensionTarget.setScope(scope);
@@ -318,7 +328,7 @@ public boolean performSearch() {
318328
private boolean performSearch(boolean validateSearchOptions) {
319329
resetStatus();
320330

321-
if (validateSearchOptions && (isActive(SearchOptions.INCREMENTAL) && !isIncrementalSearchAvailable())) {
331+
if (validateSearchOptions && !isAvailable(SearchOptions.INCREMENTAL) && isActive(SearchOptions.INCREMENTAL)) {
322332
return false; // Do nothing if search options are not compatible
323333
}
324334
boolean somethingFound = false;
@@ -417,7 +427,7 @@ private int selectAll() {
417427
*/
418428
private int findIndex(int startPosition) {
419429
int index = 0;
420-
if (isActive(SearchOptions.FORWARD)) {
430+
if (isAvailableAndActive(SearchOptions.FORWARD)) {
421431
index = findAndSelect(startPosition);
422432
} else {
423433
index = startPosition == 0 ? -1
@@ -426,7 +436,7 @@ private int findIndex(int startPosition) {
426436

427437
if (index == -1) {
428438

429-
if (isActive(SearchOptions.WRAP)) {
439+
if (isAvailableAndActive(SearchOptions.WRAP)) {
430440
statusLineMessage(FindReplaceMessages.FindReplace_Status_wrapped_label);
431441
status = new FindStatus(FindStatus.StatusCode.WRAPPED);
432442
index = findAndSelect(-1);
@@ -439,10 +449,10 @@ private int findIndex(int startPosition) {
439449

440450
@Override
441451
public int findAndSelect(int offset) {
442-
boolean wholeWordSearch = isActive(SearchOptions.WHOLE_WORD) && isWholeWordSearchAvailable();
443-
boolean forwardSearch = isActive(SearchOptions.FORWARD);
444-
boolean caseSensitiveSearch = isActive(SearchOptions.CASE_SENSITIVE);
445-
boolean regexSearch = isActive(SearchOptions.REGEX);
452+
boolean wholeWordSearch = isAvailableAndActive(SearchOptions.WHOLE_WORD);
453+
boolean forwardSearch = isAvailableAndActive(SearchOptions.FORWARD);
454+
boolean caseSensitiveSearch = isAvailableAndActive(SearchOptions.CASE_SENSITIVE);
455+
boolean regexSearch = isAvailableAndActive(SearchOptions.REGEX);
446456

447457
if (target instanceof IFindReplaceTargetExtension3 regexSupportingTarget) {
448458
return (regexSupportingTarget).findAndSelect(offset, findString,
@@ -464,7 +474,8 @@ public int findAndSelect(int offset) {
464474
*/
465475
private Point replaceSelection() {
466476
if (target instanceof IFindReplaceTargetExtension3)
467-
((IFindReplaceTargetExtension3) target).replaceSelection(replaceString, isRegExSearchAvailableAndActive());
477+
((IFindReplaceTargetExtension3) target).replaceSelection(replaceString,
478+
isAvailableAndActive(SearchOptions.REGEX));
468479
else
469480
target.replaceSelection(replaceString);
470481

@@ -553,9 +564,9 @@ public boolean performSelectAndReplace() {
553564

554565
private boolean isFindStringSelected() {
555566
String selectedString = getCurrentSelection();
556-
if (isRegExSearchAvailableAndActive()) {
567+
if (isAvailableAndActive(SearchOptions.REGEX)) {
557568
int patternFlags = 0;
558-
if (!isActive(SearchOptions.CASE_SENSITIVE)) {
569+
if (!isAvailableAndActive(SearchOptions.CASE_SENSITIVE)) {
559570
patternFlags |= Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
560571
}
561572
Pattern pattern = Pattern.compile(findString, patternFlags);

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,37 @@ public interface IFindReplaceLogic {
6060
public boolean isActive(SearchOptions searchOption);
6161

6262
/**
63-
* Returns the current status of FindReplaceLogic. The Status can inform about
64-
* events such as an error happening, a warning happening (e.g.: the
65-
* search-string wasn't found) and brings a method to retrieve a message that
66-
* can directly be displayed to the user.
63+
* Returns whether the given search options is currently available. This
64+
* includes validation of whether the target supports a specific option (such as
65+
* {@link SearchOptions#REGEX}) and the compatibility of search options (such as
66+
* {@link SearchOptions#INCREMENTAL} not being available when
67+
* {@link SearchOptions#REGEX} is active).
6768
*
68-
* @return FindAndReplaceMessageStatus
69+
* @param searchOption the search option to check for availability
70+
*
71+
* @return whether the search option is currently available
6972
*/
70-
public IFindReplaceStatus getStatus();
73+
public boolean isAvailable(SearchOptions searchOption);
7174

7275
/**
73-
* RegEx-Search is not possible on every target. Hence, even after {code
74-
* activate(SearchOptions.REGEX)}, we need to check, whether we may use
75-
* RegEx-Search.
76+
* Returns whether the given search options is currently available and active.
77+
* Combines {@link #isActive(SearchOptions)} and
78+
* {@link #isAvailable(SearchOptions)}.
7679
*
77-
* @return whether RegEx search is currently used
80+
* @param searchOption the search option to check
81+
* @return whether the search option is currently available and active
7882
*/
79-
public boolean isRegExSearchAvailableAndActive();
83+
public boolean isAvailableAndActive(SearchOptions searchOption);
8084

8185
/**
82-
* {@return whether incremental search may be performed by the
83-
* find/replace-logic based on the currently active options}
86+
* Returns the current status of FindReplaceLogic. The Status can inform about
87+
* events such as an error happening, a warning happening (e.g.: the
88+
* search-string wasn't found) and brings a method to retrieve a message that
89+
* can directly be displayed to the user.
90+
*
91+
* @return FindAndReplaceMessageStatus
8492
*/
85-
public boolean isIncrementalSearchAvailable();
93+
public IFindReplaceStatus getStatus();
8694

8795
/**
8896
* Replaces all occurrences of the current find string with the replace string.
@@ -156,15 +164,6 @@ public interface IFindReplaceLogic {
156164
*/
157165
public IFindReplaceTarget getTarget();
158166

159-
/**
160-
* Returns <code>true</code> if searching can be restricted to entire words,
161-
* <code>false</code> if not. Searching for whole words requires the given find
162-
* string to be an entire word and the regex search option to be disabled.
163-
*
164-
* @return <code>true</code> if the search can be restricted to whole words
165-
*/
166-
public boolean isWholeWordSearchAvailable();
167-
168167
/**
169168
* Initializes the anchor used as the starting point for incremental searching.
170169
* Subsequent incremental searches will start from the first letter of the

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ private void createRegexSearchButton() {
556556
.withToolTipText(FindReplaceMessages.FindReplaceOverlay_regexSearchButton_toolTip)
557557
.withOperation(() -> {
558558
activateInFindReplacerIf(SearchOptions.REGEX, regexSearchButton.getSelection());
559-
wholeWordSearchButton.setEnabled(findReplaceLogic.isWholeWordSearchAvailable());
559+
wholeWordSearchButton.setEnabled(findReplaceLogic.isAvailable(SearchOptions.WHOLE_WORD));
560560
updateIncrementalSearch();
561561
updateContentAssistAvailability();
562562
}).withShortcuts(KeyboardShortcuts.OPTION_REGEX).build();
@@ -632,7 +632,7 @@ private void createSearchBar() {
632632
searchBar.forceFocus();
633633
searchBar.selectAll();
634634
searchBar.addModifyListener(e -> {
635-
wholeWordSearchButton.setEnabled(findReplaceLogic.isWholeWordSearchAvailable());
635+
wholeWordSearchButton.setEnabled(findReplaceLogic.isAvailable(SearchOptions.WHOLE_WORD));
636636

637637
showUserFeedback(normalTextForegroundColor, true);
638638
findReplaceLogic.setFindString(searchBar.getText());
@@ -978,7 +978,7 @@ private void updateFromTargetSelection() {
978978
findReplaceLogic.deactivate(SearchOptions.GLOBAL);
979979
searchInSelectionButton.setSelection(true);
980980
} else if (!selectionText.isEmpty()) {
981-
if (findReplaceLogic.isRegExSearchAvailableAndActive()) {
981+
if (findReplaceLogic.isAvailable(SearchOptions.REGEX)) {
982982
selectionText = FindReplaceDocumentAdapter.escapeForRegExPattern(selectionText);
983983
}
984984
searchBar.setText(selectionText);
@@ -1038,6 +1038,6 @@ private void setContentAssistsEnablement(boolean enable) {
10381038
}
10391039

10401040
private void updateContentAssistAvailability() {
1041-
setContentAssistsEnablement(findReplaceLogic.isRegExSearchAvailableAndActive());
1041+
setContentAssistsEnablement(findReplaceLogic.isAvailableAndActive(SearchOptions.REGEX));
10421042
}
10431043
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -755,18 +755,18 @@ public void widgetSelected(SelectionEvent e) {
755755
storeSettings();
756756
updateButtonState();
757757
setContentAssistsEnablement(newState);
758-
fIncrementalCheckBox.setEnabled(findReplaceLogic.isIncrementalSearchAvailable());
758+
fIncrementalCheckBox.setEnabled(findReplaceLogic.isAvailable(SearchOptions.INCREMENTAL));
759759
}
760760
});
761761
storeButtonWithMnemonicInMap(fIsRegExCheckBox);
762-
fWholeWordCheckBox.setEnabled(findReplaceLogic.isWholeWordSearchAvailable());
762+
fWholeWordCheckBox.setEnabled(findReplaceLogic.isAvailable(SearchOptions.WHOLE_WORD));
763763
fWholeWordCheckBox.addSelectionListener(new SelectionAdapter() {
764764
@Override
765765
public void widgetSelected(SelectionEvent e) {
766766
updateButtonState();
767767
}
768768
});
769-
fIncrementalCheckBox.setEnabled(findReplaceLogic.isIncrementalSearchAvailable());
769+
fIncrementalCheckBox.setEnabled(findReplaceLogic.isAvailable(SearchOptions.INCREMENTAL));
770770
return panel;
771771
}
772772

@@ -957,7 +957,7 @@ private void initFindStringFromSelection() {
957957
String selection = getCurrentSelection();
958958
String searchInput = getFirstLine(selection);
959959
boolean isSingleLineInput = searchInput.equals(selection);
960-
if (findReplaceLogic.isRegExSearchAvailableAndActive()) {
960+
if (findReplaceLogic.isAvailableAndActive(SearchOptions.REGEX)) {
961961
searchInput = FindReplaceDocumentAdapter.escapeForRegExPattern(selection);
962962
}
963963
fFindField.setText(searchInput);
@@ -1088,11 +1088,11 @@ private void updateButtonState(boolean disableReplace) {
10881088
boolean isFindStringSet = str != null && !str.isEmpty();
10891089
String selectionString = enable ? target.getSelection().toString() : ""; //$NON-NLS-1$
10901090
boolean isTargetEditable = enable ? target.isEditable() : false;
1091-
boolean isRegExSearchAvailableAndActive = findReplaceLogic.isRegExSearchAvailableAndActive();
1091+
boolean isRegExSearchAvailableAndActive = findReplaceLogic.isAvailableAndActive(SearchOptions.REGEX);
10921092
boolean isSelectionGoodForReplace = selectionString != "" //$NON-NLS-1$
10931093
|| !isRegExSearchAvailableAndActive;
10941094

1095-
fWholeWordCheckBox.setEnabled(findReplaceLogic.isWholeWordSearchAvailable());
1095+
fWholeWordCheckBox.setEnabled(findReplaceLogic.isAvailable(SearchOptions.WHOLE_WORD));
10961096
fFindNextButton.setEnabled(enable && isFindStringSet);
10971097
fSelectAllButton.setEnabled(enable && isFindStringSet && (target instanceof IFindReplaceTargetExtension4));
10981098
fReplaceSelectionButton.setEnabled(
@@ -1203,11 +1203,11 @@ public void updateTarget(IFindReplaceTarget target, boolean isTargetEditable, bo
12031203
}
12041204

12051205
if (okToUse(fWholeWordCheckBox)) {
1206-
fWholeWordCheckBox.setEnabled(findReplaceLogic.isWholeWordSearchAvailable());
1206+
fWholeWordCheckBox.setEnabled(findReplaceLogic.isAvailable(SearchOptions.WHOLE_WORD));
12071207
}
12081208

12091209
if (okToUse(fIncrementalCheckBox)) {
1210-
fIncrementalCheckBox.setEnabled(findReplaceLogic.isIncrementalSearchAvailable());
1210+
fIncrementalCheckBox.setEnabled(findReplaceLogic.isAvailable(SearchOptions.INCREMENTAL));
12111211
}
12121212

12131213
if (okToUse(fReplaceLabel)) {
@@ -1222,7 +1222,7 @@ public void updateTarget(IFindReplaceTarget target, boolean isTargetEditable, bo
12221222

12231223
updateButtonState();
12241224

1225-
setContentAssistsEnablement(findReplaceLogic.isRegExSearchAvailableAndActive());
1225+
setContentAssistsEnablement(findReplaceLogic.isAvailableAndActive(SearchOptions.REGEX));
12261226
}
12271227

12281228
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ public void testWholeWordSearchAvailable() {
630630

631631
Predicate<String> isConsideredWholeWord= string -> {
632632
findReplaceLogic.setFindString(string);
633-
return findReplaceLogic.isWholeWordSearchAvailable();
633+
return findReplaceLogic.isAvailable(SearchOptions.WHOLE_WORD);
634634
};
635635

636636
assertTrue(isConsideredWholeWord.test("oneword"));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public void assertInitialConfiguration() {
286286
assertEnabled(SearchOptions.GLOBAL);
287287
assertEnabled(SearchOptions.REGEX);
288288
assertEnabled(SearchOptions.CASE_SENSITIVE);
289-
if (getFindText().equals("") || findReplaceLogic.isWholeWordSearchAvailable()) {
289+
if (getFindText().equals("") || findReplaceLogic.isAvailable(SearchOptions.WHOLE_WORD)) {
290290
assertEnabled(SearchOptions.WHOLE_WORD);
291291
} else {
292292
assertDisabled(SearchOptions.WHOLE_WORD);

0 commit comments

Comments
 (0)