Skip to content

Commit

Permalink
Find/Replace overlay: add content assist in RegEx mode
Browse files Browse the repository at this point in the history
Adds a content assist feature when the Overlay is in RegEx mode.
Uses the same content proposals as the find/replace dialog.
  • Loading branch information
Maximilian Wittmer authored and Maximilian Wittmer committed Jul 22, 2024
1 parent b502ad7 commit 6aa63c3
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -360,7 +365,8 @@ public int open() {

getShell().layout();
updatePlacementAndVisibility();

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

Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -624,6 +639,7 @@ public void focusLost(FocusEvent e) {

});
searchBar.setMessage(FindReplaceMessages.FindReplaceOverlay_searchBar_message);
contentAssistSearchField = createContentAssistField(searchBar, true);
}

private void updateIncrementalSearch() {
Expand All @@ -645,6 +661,7 @@ private void createReplaceBar() {
replaceBar.setForeground(normalTextForegroundColor);
searchBar.setForeground(normalTextForegroundColor);
}));
contentAssistReplaceField = createContentAssistField(replaceBar, false);
}

private void createFindContainer() {
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 6aa63c3

Please sign in to comment.