From 6aa63c362c4974a108a1faaf79887e4839e869a7 Mon Sep 17 00:00:00 2001 From: Maximilian Wittmer Date: Fri, 19 Jul 2024 13:38:59 +0200 Subject: [PATCH] Find/Replace overlay: add content assist in RegEx mode Adds a content assist feature when the Overlay is in RegEx mode. Uses the same content proposals as the find/replace dialog. --- .../overlay/FindReplaceOverlay.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java index d9f8cda37f6..060e365d62a 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java @@ -53,12 +53,14 @@ import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.dialogs.IPageChangedListener; import org.eclipse.jface.dialogs.PageChangedEvent; +import org.eclipse.jface.fieldassist.TextContentAdapter; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.resource.JFaceColors; import org.eclipse.jface.window.Window; import org.eclipse.jface.text.FindReplaceDocumentAdapter; +import org.eclipse.jface.text.FindReplaceDocumentAdapterContentProposalProvider; import org.eclipse.jface.text.IFindReplaceTarget; import org.eclipse.jface.text.IFindReplaceTargetExtension; import org.eclipse.jface.text.ITextViewer; @@ -67,6 +69,7 @@ import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchPartReference; import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter; import org.eclipse.ui.internal.findandreplace.FindReplaceLogic; import org.eclipse.ui.internal.findandreplace.FindReplaceMessages; import org.eclipse.ui.internal.findandreplace.HistoryStore; @@ -75,6 +78,7 @@ import org.eclipse.ui.part.MultiPageEditorSite; import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds; +import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; import org.eclipse.ui.texteditor.StatusTextEditor; public class FindReplaceOverlay extends Dialog { @@ -143,6 +147,7 @@ private final class KeyboardShortcuts { private Color normalTextForegroundColor; private boolean positionAtTop = true; private final TargetPartVisibilityHandler targetPartVisibilityHandler; + private ContentAssistCommandAdapter contentAssistSearchField, contentAssistReplaceField; public FindReplaceOverlay(Shell parent, IWorkbenchPart part, IFindReplaceTarget target) { super(parent); @@ -360,7 +365,8 @@ public int open() { getShell().layout(); updatePlacementAndVisibility(); - + updateContentAssistAvailability(); + searchBar.forceFocus(); return returnCode; } @@ -537,6 +543,7 @@ private void createRegexSearchButton() { activateInFindReplacerIf(SearchOptions.REGEX, regexSearchButton.getSelection()); wholeWordSearchButton.setEnabled(!findReplaceLogic.isActive(SearchOptions.REGEX)); updateIncrementalSearch(); + updateContentAssistAvailability(); }).withShortcuts(KeyboardShortcuts.OPTION_REGEX).build(); regexSearchButton.setSelection(findReplaceLogic.isActive(SearchOptions.REGEX)); } @@ -594,6 +601,14 @@ private void createReplaceTools() { }).withShortcuts(KeyboardShortcuts.SEARCH_ALL).build(); } + private ContentAssistCommandAdapter createContentAssistField(HistoryTextWrapper control, boolean isFind) { + TextContentAdapter contentAdapter = new TextContentAdapter(); + FindReplaceDocumentAdapterContentProposalProvider findProposer = new FindReplaceDocumentAdapterContentProposalProvider( + isFind); + return new ContentAssistCommandAdapter(control.getTextBar(), contentAdapter, findProposer, + ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, new char[0], true); + } + private void createSearchBar() { HistoryStore searchHistory = new HistoryStore(getDialogSettings(), "searchhistory", //$NON-NLS-1$ HISTORY_SIZE); @@ -624,6 +639,7 @@ public void focusLost(FocusEvent e) { }); searchBar.setMessage(FindReplaceMessages.FindReplaceOverlay_searchBar_message); + contentAssistSearchField = createContentAssistField(searchBar, true); } private void updateIncrementalSearch() { @@ -645,6 +661,7 @@ private void createReplaceBar() { replaceBar.setForeground(normalTextForegroundColor); searchBar.setForeground(normalTextForegroundColor); })); + contentAssistReplaceField = createContentAssistField(replaceBar, false); } private void createFindContainer() { @@ -993,4 +1010,15 @@ private void removeSearchScope() { findReplaceLogic.activate(SearchOptions.GLOBAL); searchInSelectionButton.setSelection(false); } + + private void setContentAssistsEnablement(boolean enable) { + contentAssistSearchField.setEnabled(enable); + if (okayToUse(replaceBar)) { + contentAssistReplaceField.setEnabled(enable); + } + } + + private void updateContentAssistAvailability() { + setContentAssistsEnablement(findReplaceLogic.isRegExSearchAvailableAndActive()); + } } \ No newline at end of file