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 HeikoKlare committed Jul 23, 2024
1 parent fd8ce3e commit 8b4c0b6
Showing 1 changed file with 30 additions and 0 deletions.
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,6 +365,7 @@ public int open() {

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

return returnCode;
}
Expand Down Expand Up @@ -543,6 +549,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 @@ -600,6 +607,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 @@ -630,6 +645,7 @@ public void focusLost(FocusEvent e) {

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

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

private void createFindContainer() {
Expand Down Expand Up @@ -709,13 +726,15 @@ private void toggleReplace() {
}
replaceToggle.setSelection(false); // We don't want the button to look "locked in", so don't
// use it's selectionState
updateContentAssistAvailability();
}

private void hideReplace() {
if (!replaceBarOpen) {
return;
}
searchBar.forceFocus();
contentAssistReplaceField = null;
replaceBarOpen = false;
replaceContainer.dispose();
updatePlacementAndVisibility();
Expand Down Expand Up @@ -999,4 +1018,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 8b4c0b6

Please sign in to comment.