Skip to content

Commit 31fefe3

Browse files
committed
Changed the functionality of the regex search with control decorations
1 parent 090a18a commit 31fefe3

File tree

5 files changed

+153
-42
lines changed

5 files changed

+153
-42
lines changed

bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/TextSearchPage.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import org.eclipse.jface.dialogs.ErrorDialog;
5555
import org.eclipse.jface.dialogs.IDialogSettings;
5656
import org.eclipse.jface.fieldassist.ComboContentAdapter;
57-
import org.eclipse.jface.resource.JFaceColors;
57+
import org.eclipse.jface.fieldassist.ControlDecoration;
5858
import org.eclipse.jface.viewers.ISelection;
5959

6060
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
@@ -68,6 +68,7 @@
6868
import org.eclipse.ui.IWorkingSetManager;
6969
import org.eclipse.ui.PlatformUI;
7070
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
71+
import org.eclipse.ui.internal.SearchDecoration;
7172

7273
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
7374

@@ -140,8 +141,7 @@ public class TextSearchPage extends DialogPage implements ISearchPage, IReplaceP
140141
*/
141142
private String[] fPreviousExtensions;
142143
private Label fFileNamePatternDescription;
143-
144-
144+
private ControlDecoration fPatternDecoration;
145145
private static class SearchPatternData {
146146
public final boolean isCaseSensitive;
147147
public final boolean isRegExSearch;
@@ -450,6 +450,7 @@ public void setVisible(boolean visible) {
450450
}
451451

452452
final void updateOKStatus() {
453+
fPatternDecoration.hide();
453454
boolean regexStatus= validateRegex();
454455
getContainer().setPerformActionEnabled(regexStatus);
455456
}
@@ -479,24 +480,19 @@ public void createControl(Composite parent) {
479480
setControl(result);
480481
Dialog.applyDialogFont(result);
481482
PlatformUI.getWorkbench().getHelpSystem().setHelp(result, ISearchHelpContextIds.TEXT_SEARCH_PAGE);
482-
}
483+
}
483484

484485
private boolean validateRegex() {
486+
485487
if (fIsRegExCheckbox.getSelection()) {
486488
try {
487489
PatternConstructor.createPattern(fPattern.getText(), fIsCaseSensitive, true);
488490
} catch (PatternSyntaxException e) {
489-
String locMessage= e.getLocalizedMessage();
490-
int i= 0;
491-
while (i < locMessage.length() && "\n\r".indexOf(locMessage.charAt(i)) == -1) { //$NON-NLS-1$
492-
i++;
493-
}
494-
statusMessage(true, locMessage.substring(0, i)); // only take first line
491+
SearchDecoration.validateRegex(fPattern.getText(), fPatternDecoration);
495492
return false;
496493
}
497-
statusMessage(false, ""); //$NON-NLS-1$
498494
} else {
499-
statusMessage(false, SearchMessages.SearchPage_containingText_hint);
495+
fPatternDecoration.hide();
500496
}
501497
return true;
502498
}
@@ -512,6 +508,8 @@ private void addTextPatternControls(Composite group) {
512508

513509
// Pattern combo
514510
fPattern= new Combo(group, SWT.SINGLE | SWT.BORDER);
511+
fPatternDecoration = new ControlDecoration(fPattern, SWT.BOTTOM | SWT.LEFT);
512+
515513
// Not done here to prevent page from resizing
516514
// fPattern.setItems(getPreviousSearchPatterns());
517515
fPattern.addSelectionListener(new SelectionAdapter() {
@@ -561,7 +559,6 @@ public void widgetSelected(SelectionEvent e) {
561559
public void widgetSelected(SelectionEvent e) {
562560
fIsRegExSearch= fIsRegExCheckbox.getSelection();
563561
updateOKStatus();
564-
565562
writeConfiguration();
566563
fPatterFieldContentAssist.setEnabled(fIsRegExSearch);
567564
fIsWholeWordCheckbox.setEnabled(!fIsRegExSearch);
@@ -860,15 +857,4 @@ private void writeConfiguration() {
860857

861858
}
862859

863-
private void statusMessage(boolean error, String message) {
864-
fStatusLabel.setText(message);
865-
if (error) {
866-
fStatusLabel.setForeground(JFaceColors.getErrorText(fStatusLabel.getDisplay()));
867-
}
868-
else {
869-
// use same color as another label to respect styling
870-
fStatusLabel.setForeground(fFileNamePatternDescription.getForeground());
871-
}
872-
}
873-
874860
}

bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.eclipse.jface.dialogs.IDialogSettings;
5252
import org.eclipse.jface.dialogs.IPageChangedListener;
5353
import org.eclipse.jface.dialogs.PageChangedEvent;
54+
import org.eclipse.jface.fieldassist.ControlDecoration;
5455
import org.eclipse.jface.fieldassist.TextContentAdapter;
5556
import org.eclipse.jface.layout.GridDataFactory;
5657
import org.eclipse.jface.layout.GridLayoutFactory;
@@ -67,6 +68,7 @@
6768
import org.eclipse.ui.IWorkbenchPartReference;
6869
import org.eclipse.ui.PlatformUI;
6970
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
71+
import org.eclipse.ui.internal.SearchDecoration;
7072
import org.eclipse.ui.internal.findandreplace.FindReplaceLogic;
7173
import org.eclipse.ui.internal.findandreplace.FindReplaceMessages;
7274
import org.eclipse.ui.internal.findandreplace.HistoryStore;
@@ -146,6 +148,7 @@ private final class KeyboardShortcuts {
146148
private Color normalTextForegroundColor;
147149
private boolean positionAtTop = true;
148150
private final TargetPartVisibilityHandler targetPartVisibilityHandler;
151+
private ControlDecoration searchBarDecoration;
149152
private ContentAssistCommandAdapter contentAssistSearchField, contentAssistReplaceField;
150153

151154
public FindReplaceOverlay(Shell parent, IWorkbenchPart part, IFindReplaceTarget target) {
@@ -241,8 +244,7 @@ private static class TargetPartVisibilityHandler implements IPartListener2, IPag
241244
private boolean isNestedLevelVisible = true;
242245

243246
TargetPartVisibilityHandler(IWorkbenchPart targetPart, Consumer<Runnable> asyncExecIfOpen,
244-
Runnable closeCallback,
245-
Runnable placementUpdateCallback) {
247+
Runnable closeCallback, Runnable placementUpdateCallback) {
246248
this.targetPart = targetPart;
247249
this.asyncExecIfOpen = asyncExecIfOpen;
248250
this.closeCallback = closeCallback;
@@ -581,6 +583,7 @@ private void createRegexSearchButton() {
581583
wholeWordSearchButton.setEnabled(findReplaceLogic.isAvailable(SearchOptions.WHOLE_WORD));
582584
updateIncrementalSearch();
583585
updateContentAssistAvailability();
586+
decorate();
584587
}).withShortcuts(KeyboardShortcuts.OPTION_REGEX).build();
585588
regexSearchButton.setSelection(findReplaceLogic.isActive(SearchOptions.REGEX));
586589
}
@@ -650,6 +653,7 @@ private void createSearchBar() {
650653
HistoryStore searchHistory = new HistoryStore(getDialogSettings(), "searchhistory", //$NON-NLS-1$
651654
HISTORY_SIZE);
652655
searchBar = new HistoryTextWrapper(searchHistory, searchBarContainer, SWT.SINGLE);
656+
searchBarDecoration = new ControlDecoration(searchBar, SWT.BOTTOM | SWT.LEFT);
653657
GridDataFactory.fillDefaults().grab(true, true).align(GridData.FILL, GridData.FILL).applyTo(searchBar);
654658
searchBar.forceFocus();
655659
searchBar.selectAll();
@@ -674,6 +678,9 @@ public void focusLost(FocusEvent e) {
674678
});
675679
searchBar.setMessage(FindReplaceMessages.FindReplaceOverlay_searchBar_message);
676680
contentAssistSearchField = createContentAssistField(searchBar, true);
681+
searchBar.addModifyListener(Event -> {
682+
decorate();
683+
});
677684
}
678685

679686
private void updateIncrementalSearch() {
@@ -1061,4 +1068,13 @@ private void setContentAssistsEnablement(boolean enable) {
10611068
private void updateContentAssistAvailability() {
10621069
setContentAssistsEnablement(findReplaceLogic.isAvailableAndActive(SearchOptions.REGEX));
10631070
}
1071+
1072+
private void decorate() {
1073+
if (regexSearchButton.getSelection()) {
1074+
SearchDecoration.validateRegex(getFindString(), searchBarDecoration);
1075+
} else {
1076+
searchBarDecoration.hide();
1077+
}
1078+
}
1079+
10641080
}

bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.eclipse.jface.dialogs.Dialog;
5050
import org.eclipse.jface.dialogs.IDialogSettings;
5151
import org.eclipse.jface.fieldassist.ComboContentAdapter;
52+
import org.eclipse.jface.fieldassist.ControlDecoration;
5253
import org.eclipse.jface.fieldassist.FieldDecoration;
5354
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
5455
import org.eclipse.jface.resource.JFaceColors;
@@ -64,13 +65,15 @@
6465

6566
import org.eclipse.ui.PlatformUI;
6667
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
68+
import org.eclipse.ui.internal.SearchDecoration;
6769
import org.eclipse.ui.internal.findandreplace.FindReplaceLogic;
6870
import org.eclipse.ui.internal.findandreplace.FindReplaceLogicMessageGenerator;
6971
import org.eclipse.ui.internal.findandreplace.FindReplaceMessages;
7072
import org.eclipse.ui.internal.findandreplace.HistoryStore;
7173
import org.eclipse.ui.internal.findandreplace.IFindReplaceLogic;
7274
import org.eclipse.ui.internal.findandreplace.SearchOptions;
7375
import org.eclipse.ui.internal.findandreplace.status.IFindReplaceStatus;
76+
import org.eclipse.ui.internal.findandreplace.status.InvalidRegExStatus;
7477
import org.eclipse.ui.internal.texteditor.SWTUtil;
7578

7679
/**
@@ -147,7 +150,10 @@ public void modifyText(ModifyEvent e) {
147150
fIgnoreNextEvent = false;
148151
return;
149152
}
150-
evaluateFindReplaceStatus();
153+
modificationHandler.run();
154+
fFindField.addModifyListener(event -> {
155+
decorate();
156+
});
151157

152158
updateButtonState(!findReplaceLogic.isActive(SearchOptions.INCREMENTAL));
153159
}
@@ -178,6 +184,7 @@ public void modifyText(ModifyEvent e) {
178184
private Button fReplaceSelectionButton, fReplaceFindButton, fFindNextButton, fReplaceAllButton, fSelectAllButton;
179185
private Combo fFindField, fReplaceField;
180186
private InputModifyListener fFindModifyListener, fReplaceModifyListener;
187+
private boolean regexOk = true;
181188

182189
/**
183190
* Find and replace command adapters.
@@ -196,6 +203,7 @@ public void modifyText(ModifyEvent e) {
196203
* @since 3.0
197204
*/
198205
private boolean fGiveFocusToFindField = true;
206+
private ControlDecoration fFindFieldDecoration;
199207

200208
/**
201209
* Holds the mnemonic/button pairs for all buttons.
@@ -310,6 +318,7 @@ public void widgetSelected(SelectionEvent e) {
310318

311319
writeSelection();
312320
updateButtonState(!somethingFound);
321+
313322
updateFindHistory();
314323
evaluateFindReplaceStatus();
315324
}
@@ -345,6 +354,7 @@ public void widgetSelected(SelectionEvent e) {
345354
evaluateFindReplaceStatus();
346355
}
347356
});
357+
348358
setGridData(fReplaceFindButton, SWT.FILL, false, SWT.FILL, false);
349359

350360
fReplaceSelectionButton = makeButton(panel, FindReplaceMessages.FindReplace_ReplaceSelectionButton_label, 104,
@@ -634,6 +644,8 @@ private Composite createInputPanel(Composite parent) {
634644
FindReplaceDocumentAdapterContentProposalProvider findProposer = new FindReplaceDocumentAdapterContentProposalProvider(
635645
true);
636646
fFindField = new Combo(panel, SWT.DROP_DOWN | SWT.BORDER);
647+
fFindFieldDecoration = new ControlDecoration(fFindField, SWT.BOTTOM | SWT.LEFT);
648+
637649
fContentAssistFindField = new ContentAssistCommandAdapter(fFindField, contentAdapter, findProposer,
638650
ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, new char[0], true);
639651
setGridData(fFindField, SWT.FILL, true, SWT.CENTER, false);
@@ -750,6 +762,10 @@ public void widgetDefaultSelected(SelectionEvent e) {
750762
@Override
751763
public void widgetSelected(SelectionEvent e) {
752764
boolean newState = fIsRegExCheckBox.getSelection();
765+
decorate();
766+
if (!newState) {
767+
regexOk = true;
768+
}
753769
setupFindReplaceLogic();
754770
storeSettings();
755771
updateButtonState();
@@ -1050,9 +1066,10 @@ private void addDecorationMargin(Control control) {
10501066
if (!(layoutData instanceof GridData))
10511067
return;
10521068
GridData gd = (GridData) layoutData;
1053-
FieldDecoration dec = FieldDecorationRegistry.getDefault()
1069+
1070+
FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault()
10541071
.getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
1055-
gd.horizontalIndent = dec.getImage().getBounds().width;
1072+
gd.horizontalIndent = fieldDecoration.getImage().getBounds().width;
10561073
}
10571074

10581075
/**
@@ -1092,8 +1109,9 @@ private void updateButtonState(boolean disableReplace) {
10921109
|| !isRegExSearchAvailableAndActive;
10931110

10941111
fWholeWordCheckBox.setEnabled(findReplaceLogic.isAvailable(SearchOptions.WHOLE_WORD));
1095-
fFindNextButton.setEnabled(enable && isFindStringSet);
1096-
fSelectAllButton.setEnabled(enable && isFindStringSet && (target instanceof IFindReplaceTargetExtension4));
1112+
fFindNextButton.setEnabled(enable && isFindStringSet && regexOk);
1113+
fSelectAllButton.setEnabled(
1114+
enable && isFindStringSet && (target instanceof IFindReplaceTargetExtension4) && regexOk);
10971115
fReplaceSelectionButton.setEnabled(
10981116
!disableReplace && enable && isTargetEditable && hasActiveSelection && isSelectionGoodForReplace);
10991117
fReplaceFindButton.setEnabled(!disableReplace && enable && isTargetEditable && isFindStringSet
@@ -1102,7 +1120,6 @@ private void updateButtonState(boolean disableReplace) {
11021120
}
11031121
}
11041122

1105-
11061123
/**
11071124
* Updates the given combo with the given content.
11081125
*
@@ -1335,19 +1352,29 @@ private void activateInFindReplaceLogicIf(SearchOptions option, boolean shouldAc
13351352
}
13361353
}
13371354

1338-
/**
1339-
* Evaluate the status of the FindReplaceLogic object.
1340-
*/
1355+
private void decorate() {
1356+
if (fIsRegExCheckBox.getSelection()) {
1357+
regexOk = SearchDecoration.validateRegex(fFindField.getText(), fFindFieldDecoration);
1358+
updateButtonState(regexOk);
1359+
1360+
} else {
1361+
fFindFieldDecoration.hide();
1362+
}
1363+
}
1364+
13411365
private void evaluateFindReplaceStatus() {
13421366
IFindReplaceStatus status = findReplaceLogic.getStatus();
13431367

1344-
String dialogMessage = status.accept(new FindReplaceLogicMessageGenerator());
1345-
fStatusLabel.setText(dialogMessage);
1346-
if (status.isInputValid()) {
1347-
fStatusLabel.setForeground(fReplaceLabel.getForeground());
1348-
} else {
1349-
fStatusLabel.setForeground(JFaceColors.getErrorText(fStatusLabel.getDisplay()));
1368+
if (!(status instanceof InvalidRegExStatus)) {
1369+
String dialogMessage = status.accept(new FindReplaceLogicMessageGenerator());
1370+
fStatusLabel.setText(dialogMessage);
1371+
if (status.isInputValid()) {
1372+
fStatusLabel.setForeground(fReplaceLabel.getForeground());
1373+
} else {
1374+
fStatusLabel.setForeground(JFaceColors.getErrorText(fStatusLabel.getDisplay()));
1375+
}
13501376
}
1377+
13511378
}
13521379

13531380
private String getCurrentSelection() {

bundles/org.eclipse.ui/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Bundle-Activator: org.eclipse.ui.internal.UIPlugin
77
Bundle-ActivationPolicy: lazy
88
Bundle-Vendor: %Plugin.providerName
99
Bundle-Localization: plugin
10-
Export-Package: org.eclipse.ui.internal;x-internal:=true
10+
Export-Package: org.eclipse.ui.internal;x-friends:="org.eclipse.ui.workbench.texteditor,org.eclipse.search"
1111
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
1212
org.eclipse.swt;bundle-version="[3.126.0,4.0.0)";visibility:=reexport,
1313
org.eclipse.jface;bundle-version="[3.34.0,4.0.0)";visibility:=reexport,

0 commit comments

Comments
 (0)