Skip to content

Commit cd670d0

Browse files
committed
Find/replace: keep find/replace input state in FindReplaceLogic
The FindReplaceLogic is a stateful representation of a search context for a search target. It currently already has a state of the used search options. Still, the find and replace string used for performing operations on the target are passed to each operation instead of keeping it in the logic object itself. Keeping this information in the logic object simplifies its interface and allows to encapsulate more of the incremental logic in the logic class, as it then has the information about when the search input changes. In addition, it works towards a clear model (logic) and view (dialog/overlay) separation. This change adds the find and replace input string state to the FindReplaceLogic class and simplifies its interface accordingly.
1 parent b3a93e3 commit cd670d0

File tree

7 files changed

+326
-241
lines changed

7 files changed

+326
-241
lines changed

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

Lines changed: 63 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.ArrayList;
1818
import java.util.HashSet;
1919
import java.util.List;
20+
import java.util.Objects;
2021
import java.util.Set;
2122
import java.util.regex.Pattern;
2223
import java.util.regex.PatternSyntaxException;
@@ -54,6 +55,22 @@ public class FindReplaceLogic implements IFindReplaceLogic {
5455
private boolean isTargetEditable;
5556
private Set<SearchOptions> searchOptions = new HashSet<>();
5657

58+
private String findString = ""; //$NON-NLS-1$
59+
private String replaceString = ""; //$NON-NLS-1$
60+
61+
@Override
62+
public void setFindString(String findString) {
63+
this.findString = Objects.requireNonNull(findString);
64+
if (isIncrementalSearchAvailable() && isActive(SearchOptions.INCREMENTAL)) {
65+
performSearch(true);
66+
}
67+
}
68+
69+
@Override
70+
public void setReplaceString(String replaceString) {
71+
this.replaceString = Objects.requireNonNull(replaceString);
72+
}
73+
5774
@Override
5875
public void activate(SearchOptions searchOption) {
5976
if (!searchOptions.add(searchOption)) {
@@ -118,7 +135,7 @@ public boolean isIncrementalSearchAvailable() {
118135
}
119136

120137
@Override
121-
public boolean isWholeWordSearchAvailable(String findString) {
138+
public boolean isWholeWordSearchAvailable() {
122139
return !isRegExSearchAvailableAndActive() && isWord(findString);
123140
}
124141
/**
@@ -213,12 +230,12 @@ private IEditorStatusLine getStatusLineManager() {
213230
}
214231

215232
@Override
216-
public void performReplaceAll(String findString, String replaceString) {
233+
public void performReplaceAll() {
217234
resetStatus();
218235

219236
if (findString != null && !findString.isEmpty()) {
220237
try {
221-
int replaceCount = replaceAll(findString, replaceString == null ? "" : replaceString); //$NON-NLS-1$
238+
int replaceCount = replaceAll();
222239
if (replaceCount != 0) {
223240
if (replaceCount == 1) { // not plural
224241
statusLineMessage(FindReplaceMessages.FindReplace_Status_replacement_label);
@@ -243,12 +260,12 @@ public void performReplaceAll(String findString, String replaceString) {
243260
}
244261

245262
@Override
246-
public void performSelectAll(String findString) {
263+
public void performSelectAll() {
247264
resetStatus();
248265

249266
if (findString != null && !findString.isEmpty()) {
250267
try {
251-
int selectCount = selectAll(findString);
268+
int selectCount = selectAll();
252269
if (selectCount != 0) {
253270
if (selectCount == 1) { // not plural
254271
statusLineMessage(FindReplaceMessages.FindReplace_Status_selection_label);
@@ -293,43 +310,12 @@ private boolean prepareTargetForEditing() {
293310
return isEditable();
294311
}
295312

296-
/**
297-
* Replaces the current selection of the target with the user's replace string.
298-
*
299-
* @param replaceString the String to replace the selection with
300-
*
301-
* @return <code>true</code> if the operation was successful
302-
*/
303-
private boolean replaceSelection(String replaceString) {
304-
305-
if (!prepareTargetForEditing()) {
306-
return false;
307-
}
308-
309-
if (replaceString == null) {
310-
replaceString = ""; //$NON-NLS-1$
311-
}
312-
313-
boolean replaced;
314-
try {
315-
replaceSelection(replaceString, isRegExSearchAvailableAndActive());
316-
replaced = true;
317-
} catch (PatternSyntaxException ex) {
318-
status = new InvalidRegExStatus(ex);
319-
replaced = false;
320-
} catch (IllegalStateException ex) {
321-
replaced = false;
322-
}
323-
324-
return replaced;
325-
}
326-
327313
@Override
328-
public boolean performSearch(String findString) {
329-
return performSearch(findString, true);
314+
public boolean performSearch() {
315+
return performSearch(true);
330316
}
331317

332-
private boolean performSearch(String findString, boolean validateSearchOptions) {
318+
private boolean performSearch(boolean validateSearchOptions) {
333319
resetStatus();
334320

335321
if (validateSearchOptions && (isActive(SearchOptions.INCREMENTAL) && !isIncrementalSearchAvailable())) {
@@ -340,7 +326,7 @@ private boolean performSearch(String findString, boolean validateSearchOptions)
340326
if (findString != null && !findString.isEmpty()) {
341327

342328
try {
343-
somethingFound = findNext(findString);
329+
somethingFound = findNext();
344330
} catch (PatternSyntaxException ex) {
345331
status = new InvalidRegExStatus(ex);
346332
} catch (IllegalStateException ex) {
@@ -354,13 +340,10 @@ private boolean performSearch(String findString, boolean validateSearchOptions)
354340
* Replaces all occurrences of the user's findString with the replace string.
355341
* Returns the number of replacements that occur.
356342
*
357-
* @param findString the string to search for
358-
* @param replaceString the replacement string
359-
* expression
360343
* @return the number of occurrences
361344
*
362345
*/
363-
private int replaceAll(String findString, String replaceString) {
346+
private int replaceAll() {
364347
if (!prepareTargetForEditing()) {
365348
return 0;
366349
}
@@ -369,8 +352,8 @@ private int replaceAll(String findString, String replaceString) {
369352
executeInForwardMode(() -> {
370353
executeWithReplaceAllEnabled(() -> {
371354
Point currentSelection = new Point(0, 0);
372-
while (findAndSelect(currentSelection.x + currentSelection.y, findString) != -1) {
373-
currentSelection = replaceSelection(replaceString, isRegExSearchAvailableAndActive());
355+
while (findAndSelect(currentSelection.x + currentSelection.y) != -1) {
356+
currentSelection = replaceSelection();
374357
replacements.add(currentSelection);
375358
}
376359
});
@@ -405,14 +388,13 @@ private void executeWithReplaceAllEnabled(Runnable runnable) {
405388
}
406389

407390
/**
408-
* @param findString the string to select as part of the search
409391
* @return the number of selected elements
410392
*/
411-
private int selectAll(String findString) {
393+
private int selectAll() {
412394
List<Point> selections = new ArrayList<>();
413395
executeInForwardMode(() -> {
414396
Point currentSeletion = new Point(0, 0);
415-
while (findAndSelect(currentSeletion.x + currentSeletion.y, findString) != -1) {
397+
while (findAndSelect(currentSeletion.x + currentSeletion.y) != -1) {
416398
currentSeletion = target.getSelection();
417399
selections.add(currentSeletion);
418400
}
@@ -429,26 +411,25 @@ private int selectAll(String findString) {
429411
* Returns the position of the specified search string, or <code>-1</code> if
430412
* the string can not be found when searching using the given options.
431413
*
432-
* @param findString the string to search for
433414
* @param startPosition the position at which to start the search
434415
* @return the occurrence of the find string following the options or
435416
* <code>-1</code> if nothing found
436417
*/
437-
private int findIndex(String findString, int startPosition) {
418+
private int findIndex(int startPosition) {
438419
int index = 0;
439420
if (isActive(SearchOptions.FORWARD)) {
440-
index = findAndSelect(startPosition, findString);
421+
index = findAndSelect(startPosition);
441422
} else {
442423
index = startPosition == 0 ? -1
443-
: findAndSelect(startPosition - 1, findString);
424+
: findAndSelect(startPosition - 1);
444425
}
445426

446427
if (index == -1) {
447428

448429
if (isActive(SearchOptions.WRAP)) {
449430
statusLineMessage(FindReplaceMessages.FindReplace_Status_wrapped_label);
450431
status = new FindStatus(FindStatus.StatusCode.WRAPPED);
451-
index = findAndSelect(-1, findString);
432+
index = findAndSelect(-1);
452433
} else {
453434
status = new FindStatus(FindStatus.StatusCode.NO_MATCH);
454435
}
@@ -457,8 +438,8 @@ private int findIndex(String findString, int startPosition) {
457438
}
458439

459440
@Override
460-
public int findAndSelect(int offset, String findString) {
461-
boolean wholeWordSearch = isActive(SearchOptions.WHOLE_WORD) && isWholeWordSearchAvailable(findString);
441+
public int findAndSelect(int offset) {
442+
boolean wholeWordSearch = isActive(SearchOptions.WHOLE_WORD) && isWholeWordSearchAvailable();
462443
boolean forwardSearch = isActive(SearchOptions.FORWARD);
463444
boolean caseSensitiveSearch = isActive(SearchOptions.CASE_SENSITIVE);
464445
boolean regexSearch = isActive(SearchOptions.REGEX);
@@ -473,20 +454,17 @@ public int findAndSelect(int offset, String findString) {
473454
}
474455

475456
/**
476-
* Replaces the selection with <code>replaceString</code>. If
477-
* <code>regExReplace</code> is <code>true</code>, <code>replaceString</code> is
478-
* a regex replace pattern which will get expanded if the underlying target
479-
* supports it. Returns the region of the inserted text; note that the returned
480-
* selection covers the expanded pattern in case of regex replace.
457+
* Replaces the selection with the current replace string. If the regex search
458+
* option has been activated, the replace string is considered as regex replace
459+
* pattern which will get expanded if the underlying target supports it. Returns
460+
* the region of the inserted text; note that the returned selection covers the
461+
* expanded pattern in case of regex replace.
481462
*
482-
* @param replaceString the replace string (or a regex pattern)
483-
* @param regExReplace <code>true</code> if <code>replaceString</code> is a
484-
* pattern
485463
* @return the selection after replacing, i.e. the inserted text
486464
*/
487-
private Point replaceSelection(String replaceString, boolean regExReplace) {
465+
private Point replaceSelection() {
488466
if (target instanceof IFindReplaceTargetExtension3)
489-
((IFindReplaceTargetExtension3) target).replaceSelection(replaceString, regExReplace);
467+
((IFindReplaceTargetExtension3) target).replaceSelection(replaceString, isRegExSearchAvailableAndActive());
490468
else
491469
target.replaceSelection(replaceString);
492470

@@ -497,20 +475,19 @@ private Point replaceSelection(String replaceString, boolean regExReplace) {
497475
* Returns whether the specified search string can be found using the given
498476
* options.
499477
*
500-
* @param findString the string to search for
501478
* @return <code>true</code> if the search string can be found using the given
502479
* options
503480
*
504481
*/
505-
private boolean findNext(String findString) {
482+
private boolean findNext() {
506483

507484
if (target == null) {
508485
return false;
509486
}
510487

511488
int findReplacePosition = calculateFindBeginningOffset();
512489

513-
int index = findIndex(findString, findReplacePosition);
490+
int index = findIndex(findReplacePosition);
514491

515492
if (index == -1) {
516493
String msg = NLSUtility.format(FindReplaceMessages.FindReplace_Status_noMatchWithValue_label, findString);
@@ -544,28 +521,37 @@ private int calculateFindBeginningOffset() {
544521
}
545522

546523
@Override
547-
public boolean performReplaceAndFind(String findString, String replaceString) {
524+
public boolean performReplaceAndFind() {
548525
resetStatus();
549-
if (performSelectAndReplace(findString, replaceString)) {
550-
performSearch(findString, false);
526+
if (performSelectAndReplace()) {
527+
performSearch(false);
551528
return true;
552529
}
553530
return false;
554531
}
555532

556533
@Override
557-
public boolean performSelectAndReplace(String findString, String replaceString) {
534+
public boolean performSelectAndReplace() {
558535
resetStatus();
559-
if (!isFindStringSelected(findString)) {
560-
performSearch(findString, false);
536+
if (!isFindStringSelected()) {
537+
performSearch(false);
561538
}
562539
if (getStatus().wasSuccessful()) {
563-
return replaceSelection(replaceString);
540+
if (!prepareTargetForEditing()) {
541+
return false;
542+
}
543+
try {
544+
replaceSelection();
545+
return true;
546+
} catch (PatternSyntaxException ex) {
547+
status = new InvalidRegExStatus(ex);
548+
} catch (IllegalStateException ex) {
549+
}
564550
}
565551
return false;
566552
}
567553

568-
private boolean isFindStringSelected(String findString) {
554+
private boolean isFindStringSelected() {
569555
String selectedString = getCurrentSelection();
570556
if (isRegExSearchAvailableAndActive()) {
571557
int patternFlags = 0;

0 commit comments

Comments
 (0)