Skip to content

Commit

Permalink
Find/Replace Overlay: store overlay options
Browse files Browse the repository at this point in the history
Stores the search options of the overlay and actively reloads them, even
when switching between overlays.

fixes eclipse-platform#2057
  • Loading branch information
Maximilian Wittmer authored and Maximilian Wittmer committed Jul 8, 2024
1 parent 0e79c18 commit b67bd42
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public void shellActivated(ShellEvent e) {

@Override
public void shellDeactivated(ShellEvent e) {
writeSearchOptions();
removeSearchScope();
}
};
Expand Down Expand Up @@ -286,6 +287,7 @@ private void adaptToPartActivationChange() {
targetPart.getSite().getShell().setActive();
targetPart.setFocus();
getShell().getDisplay().asyncExec(this::focusTargetWidget);
readSearchOptions();
}
}

Expand Down Expand Up @@ -341,7 +343,8 @@ public boolean close() {
if (!overlayOpen) {
return true;
}
storeOverlaySettings();
storeReplaceBarSettings();
writeSearchOptions();

findReplaceLogic.activate(SearchOptions.GLOBAL);
overlayOpen = false;
Expand All @@ -356,8 +359,8 @@ public int open() {
int returnCode = Window.OK;
if (!overlayOpen) {
returnCode = super.open();
restoreReplaceBarSettings();
bindListeners();
restoreOverlaySettings();
}
overlayOpen = true;
applyOverlayColors(backgroundToUse, true);
Expand All @@ -366,15 +369,16 @@ public int open() {
getShell().layout();
positionToPart();

readSearchOptions();
searchBar.forceFocus();
return returnCode;
}

private void storeOverlaySettings() {
private void storeReplaceBarSettings() {
getDialogSettings().put(REPLACE_BAR_OPEN_DIALOG_SETTING, replaceBarOpen);
}

private void restoreOverlaySettings() {
private void restoreReplaceBarSettings() {
Boolean shouldOpenReplaceBar = getDialogSettings().getBoolean(REPLACE_BAR_OPEN_DIALOG_SETTING);
if (shouldOpenReplaceBar && replaceToggle != null) {
toggleReplace();
Expand Down Expand Up @@ -524,7 +528,7 @@ private void createAreaSearchButton() {
.withImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_SEARCH_IN_AREA))
.withToolTipText(FindReplaceMessages.FindReplaceOverlay_searchInSelectionButton_toolTip)
.withSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
activateInFindReplacerIf(SearchOptions.GLOBAL, !searchInSelectionButton.getSelection());
activateInFindReplaceLogicIf(SearchOptions.GLOBAL, !searchInSelectionButton.getSelection());
updateIncrementalSearch();
})).build();
searchInSelectionButton.setSelection(findReplaceLogic.isActive(SearchOptions.WHOLE_WORD));
Expand All @@ -535,7 +539,7 @@ private void createRegexSearchButton() {
.withImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_FIND_REGEX))
.withToolTipText(FindReplaceMessages.FindReplaceOverlay_regexSearchButton_toolTip)
.withSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
activateInFindReplacerIf(SearchOptions.REGEX, ((ToolItem) e.widget).getSelection());
activateInFindReplaceLogicIf(SearchOptions.REGEX, ((ToolItem) e.widget).getSelection());
wholeWordSearchButton.setEnabled(!findReplaceLogic.isActive(SearchOptions.REGEX));
updateIncrementalSearch();
})).build();
Expand All @@ -547,7 +551,8 @@ private void createCaseSensitiveButton() {
.withImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_CASE_SENSITIVE))
.withToolTipText(FindReplaceMessages.FindReplaceOverlay_caseSensitiveButton_toolTip)
.withSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
activateInFindReplacerIf(SearchOptions.CASE_SENSITIVE, caseSensitiveSearchButton.getSelection());
activateInFindReplaceLogicIf(SearchOptions.CASE_SENSITIVE,
caseSensitiveSearchButton.getSelection());
updateIncrementalSearch();
})).build();
caseSensitiveSearchButton.setSelection(findReplaceLogic.isActive(SearchOptions.CASE_SENSITIVE));
Expand All @@ -558,7 +563,7 @@ private void createWholeWordsButton() {
.withImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_WHOLE_WORD))
.withToolTipText(FindReplaceMessages.FindReplaceOverlay_wholeWordsButton_toolTip)
.withSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
activateInFindReplacerIf(SearchOptions.WHOLE_WORD, wholeWordSearchButton.getSelection());
activateInFindReplaceLogicIf(SearchOptions.WHOLE_WORD, wholeWordSearchButton.getSelection());
updateIncrementalSearch();
})).build();
wholeWordSearchButton.setSelection(findReplaceLogic.isActive(SearchOptions.WHOLE_WORD));
Expand Down Expand Up @@ -894,10 +899,10 @@ private void performSingleReplace() {

private void performSearch(boolean forward) {
boolean oldForwardSearchSetting = findReplaceLogic.isActive(SearchOptions.FORWARD);
activateInFindReplacerIf(SearchOptions.FORWARD, forward);
activateInFindReplaceLogicIf(SearchOptions.FORWARD, forward);
findReplaceLogic.deactivate(SearchOptions.INCREMENTAL);
findReplaceLogic.performSearch(getFindString());
activateInFindReplacerIf(SearchOptions.FORWARD, oldForwardSearchSetting);
activateInFindReplaceLogicIf(SearchOptions.FORWARD, oldForwardSearchSetting);
findReplaceLogic.activate(SearchOptions.INCREMENTAL);
}

Expand Down Expand Up @@ -934,7 +939,7 @@ private void showUserFeedback(Color feedbackColor, boolean colorReplaceBar) {
}
}

private void activateInFindReplacerIf(SearchOptions option, boolean shouldActivate) {
private void activateInFindReplaceLogicIf(SearchOptions option, boolean shouldActivate) {
if (shouldActivate) {
findReplaceLogic.activate(option);
} else {
Expand All @@ -954,4 +959,34 @@ private void removeSearchScope() {
findReplaceLogic.activate(SearchOptions.GLOBAL);
searchInSelectionButton.setSelection(false);
}

/**
* Initializes itself from the dialog settings with the same state as at the
* previous invocation.
*/
private void readSearchOptions() {
IDialogSettings settings = getDialogSettings();
activateInFindReplaceLogicIf(SearchOptions.CASE_SENSITIVE, settings.getBoolean("casesensitive")); //$NON-NLS-1$
activateInFindReplaceLogicIf(SearchOptions.WHOLE_WORD, settings.getBoolean("wholeword")); //$NON-NLS-1$
activateInFindReplaceLogicIf(SearchOptions.REGEX, settings.getBoolean("isRegEx")); //$NON-NLS-1$

if (findReplaceLogic.isWholeWordSearchAvailable(getFindString())) {
wholeWordSearchButton.setSelection(findReplaceLogic.isActive(SearchOptions.WHOLE_WORD));
}
regexSearchButton.setSelection(findReplaceLogic.isRegExSearchAvailableAndActive());
caseSensitiveSearchButton.setSelection(findReplaceLogic.isActive(SearchOptions.CASE_SENSITIVE));
}

/**
* Stores its current configuration in the dialog store.
*/
private void writeSearchOptions() {
IDialogSettings s = getDialogSettings();

s.put("casesensitive", findReplaceLogic.isActive(SearchOptions.CASE_SENSITIVE)); //$NON-NLS-1$
if (findReplaceLogic.isWholeWordSearchAvailable(getFindString())) {
s.put("wholeword", findReplaceLogic.isActive(SearchOptions.WHOLE_WORD)); //$NON-NLS-1$
}
s.put("isRegEx", findReplaceLogic.isActive(SearchOptions.REGEX)); //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,34 @@ public void testActivateDialogWithSelectionActive() {
assertThat(fTextViewer.getDocument().get(), is("text" + System.lineSeparator() + System.lineSeparator()));
}

@Test
public void testOptionsRestoredCorrectly() {
openTextViewer("text");
initializeFindReplaceUIForTextViewer();

dialog.select(SearchOptions.REGEX);
dialog.select(SearchOptions.CASE_SENSITIVE);

reopenFindReplaceUIForTextViewer();

dialog.assertSelected(SearchOptions.REGEX);
dialog.assertSelected(SearchOptions.CASE_SENSITIVE);
}

@Test
public void testWholeWordOptionRestoredCorrectly() {
openTextViewer("text");
initializeFindReplaceUIForTextViewer();

dialog.setFindText("text");
dialog.select(SearchOptions.WHOLE_WORD);

dialog.close();
fTextViewer.setSelectedRange(0, 4);
dialog= openUIFromTextViewer(fTextViewer);
dialog.assertSelected(SearchOptions.WHOLE_WORD);
}

protected AccessType getDialog() {
return dialog;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public IFindReplaceTarget getTarget() {
}

private void restoreInitialConfiguration() {
find.setText("");
find.setText("word");
select(SearchOptions.GLOBAL);
unselect(SearchOptions.REGEX);
unselect(SearchOptions.CASE_SENSITIVE);
Expand Down

0 comments on commit b67bd42

Please sign in to comment.