Skip to content

Commit

Permalink
Find/Replace Overlay: Add a search history
Browse files Browse the repository at this point in the history
Add a search history for the Find/Replace overlay, displayed as a
dropdown below the find/replace inputs.

fixes #1907
  • Loading branch information
Maximilian Wittmer authored and HeikoKlare committed Jul 16, 2024
1 parent 224ee6f commit 9ff464d
Show file tree
Hide file tree
Showing 10 changed files with 385 additions and 16 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ private FindReplaceMessages() {
public static String FindReplaceOverlay_searchBar_message;
public static String FindReplaceOverlay_replaceBar_message;
public static String FindReplaceOverlay_replaceToggle_toolTip;
public static String FindReplaceOverlay_searchHistory_toolTip;
public static String FindReplaceOverlay_replaceHistory_toolTip;
public static String FindReplaceOverlayFirstTimePopup_FindReplaceOverlayFirstTimePopup_message;
public static String FindReplaceOverlayFirstTimePopup_FindReplaceOverlayFirstTimePopup_title;
public static String SearchHistoryMenu_SEARCH_HISTORY_EMPTY_STRING;
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ FindReplace_CloseButton_label=Close

# Messages for the find/replace overlay
FindReplaceOverlay_closeButton_toolTip=Close
# Messages for the "new" Find-Replace-Overlay
FindReplaceOverlay_upSearchButton_toolTip=Search backward
FindReplaceOverlay_downSearchButton_toolTip=Search forward
FindReplaceOverlay_searchAllButton_toolTip=Search all
Expand All @@ -60,5 +59,8 @@ FindReplaceOverlay_replaceAllButton_toolTip=Replace all
FindReplaceOverlay_searchBar_message=Find
FindReplaceOverlay_replaceBar_message=Replace
FindReplaceOverlay_replaceToggle_toolTip=Toggle input for replace
FindReplaceOverlay_searchHistory_toolTip=Show search history
FindReplaceOverlay_replaceHistory_toolTip=Show replace history
FindReplaceOverlayFirstTimePopup_FindReplaceOverlayFirstTimePopup_message=Find and replace can now be done using an overlay embedded inside the editor. If you prefer the dialog, you can disable the overlay in the preferences or <a>disable it now</a>.
FindReplaceOverlayFirstTimePopup_FindReplaceOverlayFirstTimePopup_title=New Find/Replace Overlay
SearchHistoryMenu_SEARCH_HISTORY_EMPTY_STRING=perform search for search history
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,11 @@ private void writeHistory() {
settingsManager.put(sectionName, names);
}

public int indexOf(String entry) {
return history.indexOf(entry);
}

public int size() {
return history.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.findandreplace.FindReplaceLogic;
import org.eclipse.ui.internal.findandreplace.FindReplaceMessages;
import org.eclipse.ui.internal.findandreplace.HistoryStore;
import org.eclipse.ui.internal.findandreplace.SearchOptions;
import org.eclipse.ui.internal.findandreplace.status.IFindReplaceStatus;

Expand Down Expand Up @@ -104,6 +105,7 @@ private final class KeyboardShortcuts {
private static final double BIG_WIDTH_RATIO_EDITOR_TO_OVERLAY = 0.7;
private static final String MINIMAL_WIDTH_TEXT = "THIS TEXT IS SHORT "; //$NON-NLS-1$
private static final String IDEAL_WIDTH_TEXT = "THIS TEXT HAS A REASONABLE LENGTH FOR SEARCHING"; //$NON-NLS-1$
private static final int HISTORY_SIZE = 15;

private final Map<KeyStroke, Runnable> searchKeyStrokeHandlers = new HashMap<>();
private final Map<KeyStroke, Runnable> replaceKeyStrokeHandlers = new HashMap<>();
Expand All @@ -120,7 +122,7 @@ private final class KeyboardShortcuts {

private Composite searchContainer;
private Composite searchBarContainer;
private Text searchBar;
private HistoryTextWrapper searchBar;
private AccessibleToolBar searchTools;
private ToolItem searchInSelectionButton;
private ToolItem wholeWordSearchButton;
Expand All @@ -134,7 +136,7 @@ private final class KeyboardShortcuts {

private Composite replaceContainer;
private Composite replaceBarContainer;
private Text replaceBar;
private HistoryTextWrapper replaceBar;
private AccessibleToolBar replaceTools;
private ToolItem replaceButton;
private ToolItem replaceAllButton;
Expand All @@ -151,7 +153,6 @@ public FindReplaceOverlay(Shell parent, IWorkbenchPart part, IFindReplaceTarget
setShellStyle(SWT.MODELESS);
setBlockOnOpen(false);
targetPart = part;

}

@Override
Expand All @@ -176,12 +177,14 @@ private void performReplaceAll() {
BusyIndicator.showWhile(getShell() != null ? getShell().getDisplay() : Display.getCurrent(),
() -> findReplaceLogic.performReplaceAll(getFindString(), getReplaceString()));
evaluateFindReplaceStatus();
replaceBar.storeHistory();
searchBar.storeHistory();
}

private void performSelectAll() {
BusyIndicator.showWhile(getShell() != null ? getShell().getDisplay() : Display.getCurrent(),
() -> findReplaceLogic.performSelectAll(getFindString()));
evaluateFindReplaceStatus();
searchBar.storeHistory();
}

private void toggleToolItem(ToolItem toolItem) {
Expand Down Expand Up @@ -372,6 +375,7 @@ private void applyOverlayColors(Color color, boolean tryToColorReplaceBar) {
replaceContainer.setBackground(color);
replaceBar.setBackground(color);
replaceBarContainer.setBackground(color);
replaceTools.setBackground(color);
replaceAllButton.setBackground(color);
replaceButton.setBackground(color);
}
Expand Down Expand Up @@ -441,17 +445,19 @@ private void retrieveBackgroundColor() {
textBarForRetrievingTheRightColor.dispose();
}


private void createSearchTools() {
searchTools = new AccessibleToolBar(searchContainer);
GridDataFactory.fillDefaults().grab(false, true).align(GridData.END, GridData.END).applyTo(searchTools);

searchTools.createToolItem(SWT.SEPARATOR);

createCaseSensitiveButton();
createRegexSearchButton();
createWholeWordsButton();
createAreaSearchButton();

@SuppressWarnings("unused")
ToolItem separator = searchTools.createToolItem(SWT.SEPARATOR);
searchTools.createToolItem(SWT.SEPARATOR);

Runnable searchUpOperation = () -> performSearch(false);
searchUpButton = new AccessibleToolItemBuilder(searchTools).withStyleBits(SWT.PUSH)
Expand Down Expand Up @@ -564,6 +570,9 @@ private void createReplaceTools() {
Color warningColor = JFaceColors.getErrorText(getShell().getDisplay());

replaceTools = new AccessibleToolBar(replaceContainer);

replaceTools.createToolItem(SWT.SEPARATOR);

GridDataFactory.fillDefaults().grab(false, true).align(GridData.CENTER, GridData.END).applyTo(replaceTools);
replaceButton = new AccessibleToolItemBuilder(replaceTools).withStyleBits(SWT.PUSH)
.withImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_REPLACE))
Expand Down Expand Up @@ -593,7 +602,9 @@ private void createReplaceTools() {
}

private void createSearchBar() {
searchBar = new Text(searchBarContainer, SWT.SINGLE);
HistoryStore searchHistory = new HistoryStore(getDialogSettings(), "searchhistory", //$NON-NLS-1$
HISTORY_SIZE);
searchBar = new HistoryTextWrapper(searchHistory, searchBarContainer, SWT.SINGLE);
GridDataFactory.fillDefaults().grab(true, false).align(GridData.FILL, GridData.END).applyTo(searchBar);
searchBar.forceFocus();
searchBar.selectAll();
Expand Down Expand Up @@ -656,7 +667,8 @@ private void updateIncrementalSearch() {
}

private void createReplaceBar() {
replaceBar = new Text(replaceBarContainer, SWT.SINGLE);
HistoryStore replaceHistory = new HistoryStore(getDialogSettings(), "replacehistory", HISTORY_SIZE); //$NON-NLS-1$
replaceBar = new HistoryTextWrapper(replaceHistory, replaceBarContainer, SWT.SINGLE);
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.END).applyTo(replaceBar);
replaceBar.setMessage(FindReplaceMessages.FindReplaceOverlay_replaceBar_message);
replaceBar.addFocusListener(FocusListener.focusLostAdapter(e -> {
Expand Down Expand Up @@ -922,6 +934,8 @@ private String getReplaceString() {
private void performSingleReplace() {
findReplaceLogic.performReplaceAndFind(getFindString(), getReplaceString());
evaluateFindReplaceStatus();
replaceBar.storeHistory();
searchBar.storeHistory();
}

private void performSearch(boolean forward) {
Expand All @@ -932,6 +946,7 @@ private void performSearch(boolean forward) {
activateInFindReplacerIf(SearchOptions.FORWARD, oldForwardSearchSetting);
findReplaceLogic.activate(SearchOptions.INCREMENTAL);
evaluateFindReplaceStatus();
searchBar.storeHistory();
}

private void updateFromTargetSelection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
*/
class FindReplaceOverlayImages {
private static final String PREFIX_ELCL = TextEditorPlugin.PLUGIN_ID + ".elcl."; //$NON-NLS-1$

static final String KEY_CLOSE = PREFIX_ELCL + "close"; //$NON-NLS-1$
static final String KEY_FIND_NEXT = PREFIX_ELCL + "select_next"; //$NON-NLS-1$
static final String KEY_FIND_PREV = PREFIX_ELCL + "select_prev"; //$NON-NLS-1$
Expand All @@ -47,6 +46,7 @@ class FindReplaceOverlayImages {
static final String KEY_SEARCH_IN_AREA = PREFIX_ELCL + "search_in_selection"; //$NON-NLS-1$
static final String KEY_OPEN_REPLACE_AREA = PREFIX_ELCL + "open_replace"; //$NON-NLS-1$
static final String KEY_CLOSE_REPLACE_AREA = PREFIX_ELCL + "close_replace"; //$NON-NLS-1$
static final String KEY_OPEN_HISTORY = "open_history"; //$NON-NLS-1$

/**
* The image registry containing {@link Image images}.
Expand All @@ -73,6 +73,7 @@ private static void declareImages() {
declareRegistryImage(KEY_SEARCH_IN_AREA, ELCL + "search_in_area.png"); //$NON-NLS-1$
declareRegistryImage(KEY_OPEN_REPLACE_AREA, ELCL + "open_replace.png"); //$NON-NLS-1$
declareRegistryImage(KEY_CLOSE_REPLACE_AREA, ELCL + "close_replace.png"); //$NON-NLS-1$
declareRegistryImage(KEY_OPEN_HISTORY, ELCL + "open_history.png"); //$NON-NLS-1$
}

/**
Expand Down
Loading

0 comments on commit 9ff464d

Please sign in to comment.