Skip to content

Commit

Permalink
Changed the functionality of the regex search with control decorations
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisCode authored and fedejeanne committed Oct 10, 2024
1 parent a431d71 commit eb8bef5
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 39 deletions.
2 changes: 2 additions & 0 deletions bundles/org.eclipse.search.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ Require-Bundle:
org.eclipse.text;bundle-version="3.13.100"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Automatic-Module-Name: org.eclipse.search.core
Import-Package: org.eclipse.jface.fieldassist,
org.eclipse.swt.graphics
4 changes: 3 additions & 1 deletion bundles/org.eclipse.search/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Export-Package: org.eclipse.search.internal.ui;x-internal:=true,
org.eclipse.search2.internal.ui.basic.views;x-internal:=true,
org.eclipse.search2.internal.ui.text;x-internal:=true,
org.eclipse.search2.internal.ui.text2;x-internal:=true
Import-Package: org.eclipse.ui.internal.findandreplace
Require-Bundle:
org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
org.eclipse.core.resources;bundle-version="[3.14.0,4.0.0)",
Expand All @@ -29,7 +30,8 @@ Require-Bundle:
org.eclipse.ui.forms;bundle-version="[3.4.0,4.0.0)",
org.eclipse.ltk.core.refactoring;bundle-version="[3.5.0,4.0.0)",
org.eclipse.ltk.ui.refactoring;bundle-version="[3.5.0,4.0.0)",
org.eclipse.search.core;bundle-version="[3.16.0,4.0.0)";visibility:=reexport
org.eclipse.search.core;bundle-version="[3.16.0,4.0.0)";visibility:=reexport,
org.eclipse.ui.browser;bundle-version="3.8.400"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Automatic-Module-Name: org.eclipse.search
Service-Component: OSGI-INF/*.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.fieldassist.ComboContentAdapter;
import org.eclipse.jface.resource.JFaceColors;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.viewers.ISelection;

import org.eclipse.jface.text.FindReplaceDocumentAdapter;
Expand All @@ -68,6 +68,7 @@
import org.eclipse.ui.IWorkingSetManager;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
import org.eclipse.ui.internal.SearchDecoration;

import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;

Expand Down Expand Up @@ -140,6 +141,7 @@ public class TextSearchPage extends DialogPage implements ISearchPage, IReplaceP
*/
private String[] fPreviousExtensions;
private Label fFileNamePatternDescription;
private ControlDecoration decoration;


private static class SearchPatternData {
Expand Down Expand Up @@ -450,6 +452,7 @@ public void setVisible(boolean visible) {
}

final void updateOKStatus() {
decoration.hide();
boolean regexStatus= validateRegex();
getContainer().setPerformActionEnabled(regexStatus);
}
Expand Down Expand Up @@ -481,22 +484,19 @@ public void createControl(Composite parent) {
PlatformUI.getWorkbench().getHelpSystem().setHelp(result, ISearchHelpContextIds.TEXT_SEARCH_PAGE);
}

private boolean validateRegex() {
private boolean validateRegex() {
SearchDecoration sDecoration = new SearchDecoration();

if (fIsRegExCheckbox.getSelection()) {
try {
PatternConstructor.createPattern(fPattern.getText(), fIsCaseSensitive, true);
} catch (PatternSyntaxException e) {
String locMessage= e.getLocalizedMessage();
int i= 0;
while (i < locMessage.length() && "\n\r".indexOf(locMessage.charAt(i)) == -1) { //$NON-NLS-1$
i++;
}
statusMessage(true, locMessage.substring(0, i)); // only take first line

sDecoration.decorateA(decoration, fPattern.getText());
return false;
}
statusMessage(false, ""); //$NON-NLS-1$
} else {
statusMessage(false, SearchMessages.SearchPage_containingText_hint);
decoration.hide();
}
return true;
}
Expand All @@ -510,8 +510,11 @@ private void addTextPatternControls(Composite group) {
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
label.setFont(group.getFont());


// Pattern combo
fPattern= new Combo(group, SWT.SINGLE | SWT.BORDER);
decoration = new ControlDecoration(fPattern, SWT.BOTTOM | SWT.LEFT);

// Not done here to prevent page from resizing
// fPattern.setItems(getPreviousSearchPatterns());
fPattern.addSelectionListener(new SelectionAdapter() {
Expand Down Expand Up @@ -561,7 +564,6 @@ public void widgetSelected(SelectionEvent e) {
public void widgetSelected(SelectionEvent e) {
fIsRegExSearch= fIsRegExCheckbox.getSelection();
updateOKStatus();

writeConfiguration();
fPatterFieldContentAssist.setEnabled(fIsRegExSearch);
fIsWholeWordCheckbox.setEnabled(!fIsRegExSearch);
Expand Down Expand Up @@ -860,15 +862,4 @@ private void writeConfiguration() {

}

private void statusMessage(boolean error, String message) {
fStatusLabel.setText(message);
if (error) {
fStatusLabel.setForeground(JFaceColors.getErrorText(fStatusLabel.getDisplay()));
}
else {
// use same color as another label to respect styling
fStatusLabel.setForeground(fFileNamePatternDescription.getForeground());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ Require-Bundle:
org.eclipse.jface.notifications
Bundle-RequiredExecutionEnvironment: JavaSE-17
Automatic-Module-Name: org.eclipse.ui.workbench.texteditor
Import-Package: org.eclipse.search.internal.core.text
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.IPageChangedListener;
import org.eclipse.jface.dialogs.PageChangedEvent;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
Expand All @@ -67,6 +68,7 @@
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
import org.eclipse.ui.internal.SearchDecoration;
import org.eclipse.ui.internal.findandreplace.FindReplaceLogic;
import org.eclipse.ui.internal.findandreplace.FindReplaceMessages;
import org.eclipse.ui.internal.findandreplace.HistoryStore;
Expand Down Expand Up @@ -146,8 +148,13 @@ private final class KeyboardShortcuts {
private Color normalTextForegroundColor;
private boolean positionAtTop = true;
private final TargetPartVisibilityHandler targetPartVisibilityHandler;

private ControlDecoration decoration;
private SearchDecoration dec;

private ContentAssistCommandAdapter contentAssistSearchField, contentAssistReplaceField;


public FindReplaceOverlay(Shell parent, IWorkbenchPart part, IFindReplaceTarget target) {
super(parent);
createFindReplaceLogic(target);
Expand All @@ -157,6 +164,7 @@ public FindReplaceOverlay(Shell parent, IWorkbenchPart part, IFindReplaceTarget
targetPart = part;
targetPartVisibilityHandler = new TargetPartVisibilityHandler(targetPart, this::asyncExecIfOpen, this::close,
this::updatePlacementAndVisibility);
this.dec = new SearchDecoration();
}

@Override
Expand Down Expand Up @@ -581,6 +589,11 @@ private void createRegexSearchButton() {
wholeWordSearchButton.setEnabled(findReplaceLogic.isAvailable(SearchOptions.WHOLE_WORD));
updateIncrementalSearch();
updateContentAssistAvailability();
if (!regexSearchButton.getSelection()) {
decoration.hide();
} else {
dec.decorateA(decoration, getFindString());
}
}).withShortcuts(KeyboardShortcuts.OPTION_REGEX).build();
regexSearchButton.setSelection(findReplaceLogic.isActive(SearchOptions.REGEX));
}
Expand Down Expand Up @@ -650,6 +663,7 @@ private void createSearchBar() {
HistoryStore searchHistory = new HistoryStore(getDialogSettings(), "searchhistory", //$NON-NLS-1$
HISTORY_SIZE);
searchBar = new HistoryTextWrapper(searchHistory, searchBarContainer, SWT.SINGLE);
decoration = new ControlDecoration(searchBar, SWT.BOTTOM | SWT.LEFT);
GridDataFactory.fillDefaults().grab(true, true).align(GridData.FILL, GridData.FILL).applyTo(searchBar);
searchBar.forceFocus();
searchBar.selectAll();
Expand All @@ -674,6 +688,8 @@ public void focusLost(FocusEvent e) {
});
searchBar.setMessage(FindReplaceMessages.FindReplaceOverlay_searchBar_message);
contentAssistSearchField = createContentAssistField(searchBar, true);

decorate();
}

private void updateIncrementalSearch() {
Expand Down Expand Up @@ -991,6 +1007,7 @@ private void performSearch(boolean forward) {
activateInFindReplacerIf(SearchOptions.FORWARD, oldForwardSearchSetting);
evaluateFindReplaceStatus();
searchBar.storeHistory();

}

private void updateFromTargetSelection() {
Expand Down Expand Up @@ -1061,4 +1078,17 @@ private void setContentAssistsEnablement(boolean enable) {
private void updateContentAssistAvailability() {
setContentAssistsEnablement(findReplaceLogic.isAvailableAndActive(SearchOptions.REGEX));
}

public void decorate() {
// decoration = new ControlDecoration(searchBar, SWT.BOTTOM | SWT.LEFT);

searchBar.addModifyListener(event -> {
if (regexSearchButton.getSelection()) {
dec.decorateA(decoration, getFindString());
} else
decoration.hide();
});

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.fieldassist.ComboContentAdapter;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.jface.resource.JFaceColors;
import org.eclipse.jface.util.Util;

import org.eclipse.jface.text.FindReplaceDocumentAdapter;
Expand All @@ -64,13 +64,12 @@

import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
import org.eclipse.ui.internal.SearchDecoration;
import org.eclipse.ui.internal.findandreplace.FindReplaceLogic;
import org.eclipse.ui.internal.findandreplace.FindReplaceLogicMessageGenerator;
import org.eclipse.ui.internal.findandreplace.FindReplaceMessages;
import org.eclipse.ui.internal.findandreplace.HistoryStore;
import org.eclipse.ui.internal.findandreplace.IFindReplaceLogic;
import org.eclipse.ui.internal.findandreplace.SearchOptions;
import org.eclipse.ui.internal.findandreplace.status.IFindReplaceStatus;
import org.eclipse.ui.internal.texteditor.SWTUtil;

/**
Expand Down Expand Up @@ -196,6 +195,8 @@ public void modifyText(ModifyEvent e) {
* @since 3.0
*/
private boolean fGiveFocusToFindField = true;
private ControlDecoration decoration;
private SearchDecoration dec = new SearchDecoration();

/**
* Holds the mnemonic/button pairs for all buttons.
Expand Down Expand Up @@ -634,6 +635,8 @@ private Composite createInputPanel(Composite parent) {
FindReplaceDocumentAdapterContentProposalProvider findProposer = new FindReplaceDocumentAdapterContentProposalProvider(
true);
fFindField = new Combo(panel, SWT.DROP_DOWN | SWT.BORDER);
decoration = new ControlDecoration(fFindField, SWT.BOTTOM | SWT.LEFT);

fContentAssistFindField = new ContentAssistCommandAdapter(fFindField, contentAdapter, findProposer,
ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, new char[0], true);
setGridData(fFindField, SWT.FILL, true, SWT.CENTER, false);
Expand Down Expand Up @@ -750,6 +753,11 @@ public void widgetDefaultSelected(SelectionEvent e) {
@Override
public void widgetSelected(SelectionEvent e) {
boolean newState = fIsRegExCheckBox.getSelection();
if (!newState) {
decoration.hide();
} else {
dec.decorateA(decoration, getFindString());
}
setupFindReplaceLogic();
storeSettings();
updateButtonState();
Expand Down Expand Up @@ -1050,9 +1058,10 @@ private void addDecorationMargin(Control control) {
if (!(layoutData instanceof GridData))
return;
GridData gd = (GridData) layoutData;
FieldDecoration dec = FieldDecorationRegistry.getDefault()

FieldDecoration dec_one = FieldDecorationRegistry.getDefault()
.getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
gd.horizontalIndent = dec.getImage().getBounds().width;
gd.horizontalIndent = dec_one.getImage().getBounds().width;
}

/**
Expand Down Expand Up @@ -1339,15 +1348,18 @@ private void activateInFindReplaceLogicIf(SearchOptions option, boolean shouldAc
* Evaluate the status of the FindReplaceLogic object.
*/
private void evaluateFindReplaceStatus() {
IFindReplaceStatus status = findReplaceLogic.getStatus();
decorate();
}

String dialogMessage = status.accept(new FindReplaceLogicMessageGenerator());
fStatusLabel.setText(dialogMessage);
if (status.isInputValid()) {
fStatusLabel.setForeground(fReplaceLabel.getForeground());
} else {
fStatusLabel.setForeground(JFaceColors.getErrorText(fStatusLabel.getDisplay()));
}
public void decorate() {

fFindField.addModifyListener(event -> {
if (fIsRegExCheckBox.getSelection()) {
dec.decorateA(decoration, fFindField.getText());
} else {
decoration.hide();
}
});
}

private String getCurrentSelection() {
Expand Down
4 changes: 2 additions & 2 deletions bundles/org.eclipse.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Plugin.name
Bundle-SymbolicName: org.eclipse.ui; singleton:=true
Bundle-Version: 3.206.200.qualifier
Bundle-Version: 3.207.0.qualifier
Export-Package: org.eclipse.ui.internal
Bundle-Activator: org.eclipse.ui.internal.UIPlugin
Bundle-ActivationPolicy: lazy
Bundle-Vendor: %Plugin.providerName
Bundle-Localization: plugin
Export-Package: org.eclipse.ui.internal;x-internal:=true
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
org.eclipse.swt;bundle-version="[3.126.0,4.0.0)";visibility:=reexport,
org.eclipse.jface;bundle-version="[3.34.0,4.0.0)";visibility:=reexport,
Expand Down
Loading

0 comments on commit eb8bef5

Please sign in to comment.