Skip to content

Commit db41fb4

Browse files
ptzieglervogella
authored andcommitted
Replace TextMatcher in favor of extended StringMatcher
The functionality of the TextMatcher has been integrated in the StringMatcher class from the Equinox bundle, which allows this class to be used without a dependency to the Eclipse UI bundle. Note: To get the same behavior as the TextMatcher, one needs to call matchWords() instead of match(). Furthermore, the pattern needs to be trimmed explicitly, where needed. Contributes to #2567
1 parent 58bb65c commit db41fb4

File tree

8 files changed

+27
-300
lines changed

8 files changed

+27
-300
lines changed

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/dialogs/FilteredList.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.eclipse.core.runtime.IProgressMonitor;
2424
import org.eclipse.core.runtime.IStatus;
2525
import org.eclipse.core.runtime.Status;
26+
import org.eclipse.core.text.StringMatcher;
2627
import org.eclipse.jface.viewers.ILabelProvider;
2728
import org.eclipse.swt.SWT;
2829
import org.eclipse.swt.SWTException;
@@ -36,7 +37,6 @@
3637
import org.eclipse.swt.widgets.Table;
3738
import org.eclipse.swt.widgets.TableItem;
3839
import org.eclipse.ui.internal.WorkbenchMessages;
39-
import org.eclipse.ui.internal.misc.TextMatcher;
4040
import org.eclipse.ui.internal.util.Util;
4141
import org.eclipse.ui.progress.WorkbenchJob;
4242

@@ -73,16 +73,16 @@ public interface FilterMatcher {
7373
}
7474

7575
private class DefaultFilterMatcher implements FilterMatcher {
76-
private TextMatcher fMatcher;
76+
private StringMatcher fMatcher;
7777

7878
@Override
7979
public void setFilter(String pattern, boolean ignoreCase, boolean ignoreWildCards) {
80-
fMatcher = new TextMatcher(pattern + '*', ignoreCase, ignoreWildCards);
80+
fMatcher = new StringMatcher(pattern.trim() + '*', ignoreCase, ignoreWildCards);
8181
}
8282

8383
@Override
8484
public boolean match(Object element) {
85-
return fMatcher.match(fLabelProvider.getText(element));
85+
return fMatcher.matchWords(fLabelProvider.getText(element));
8686
}
8787
}
8888

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/dialogs/PatternFilter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
import java.util.HashMap;
1919
import java.util.Map;
20+
import org.eclipse.core.text.StringMatcher;
2021
import org.eclipse.jface.viewers.AbstractTreeViewer;
2122
import org.eclipse.jface.viewers.ContentViewer;
2223
import org.eclipse.jface.viewers.ILabelProvider;
2324
import org.eclipse.jface.viewers.ITreeContentProvider;
2425
import org.eclipse.jface.viewers.Viewer;
2526
import org.eclipse.jface.viewers.ViewerFilter;
26-
import org.eclipse.ui.internal.misc.TextMatcher;
2727

2828
/**
2929
* A filter used in conjunction with <code>FilteredTree</code>. In order to
@@ -57,7 +57,7 @@ public class PatternFilter extends ViewerFilter {
5757
/**
5858
* The string pattern matcher used for this pattern filter.
5959
*/
60-
private TextMatcher matcher;
60+
private StringMatcher matcher;
6161

6262
private boolean useEarlyReturnIfMatcherIsNull = true;
6363

@@ -173,7 +173,7 @@ public void setPattern(String patternString) {
173173
if (includeLeadingWildcard) {
174174
pattern = "*" + pattern; //$NON-NLS-1$
175175
}
176-
matcher = new TextMatcher(pattern, true, false);
176+
matcher = new StringMatcher(pattern.trim(), true, false);
177177
}
178178
}
179179

@@ -197,7 +197,7 @@ private boolean match(String string) {
197197
if (matcher == null) {
198198
return true;
199199
}
200-
return matcher.match(string);
200+
return matcher.matchWords(string);
201201
}
202202

203203
/**
@@ -289,7 +289,7 @@ protected boolean wordMatches(String text) {
289289
}
290290

291291
// Otherwise check if any of the words of the text matches
292-
String[] words = TextMatcher.getWords(text);
292+
String[] words = StringMatcher.getWords(text);
293293
for (String word : words) {
294294
if (!match(word)) {
295295
return false;

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/dialogs/SearchPattern.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.dialogs;
1515

16+
import org.eclipse.core.text.StringMatcher;
1617
import org.eclipse.jface.util.Util;
17-
import org.eclipse.ui.internal.misc.TextMatcher;
1818

1919
/**
2020
* A search pattern defines how search results are found.
@@ -120,7 +120,7 @@ public class SearchPattern {
120120

121121
private String initialPattern;
122122

123-
private TextMatcher stringMatcher;
123+
private StringMatcher stringMatcher;
124124

125125
private static final char START_SYMBOL = '>';
126126

@@ -200,7 +200,7 @@ public void setPattern(String stringPattern) {
200200
initializePatternAndMatchRule(stringPattern);
201201
matchRule = matchRule & this.allowedRules;
202202
if (matchRule == RULE_PATTERN_MATCH) {
203-
stringMatcher = new TextMatcher(this.stringPattern, true, false);
203+
stringMatcher = new StringMatcher(this.stringPattern.trim(), true, false);
204204
}
205205
}
206206

@@ -219,7 +219,7 @@ public boolean matches(String text) {
219219
case RULE_BLANK_MATCH:
220220
return true;
221221
case RULE_PATTERN_MATCH:
222-
return stringMatcher.match(text);
222+
return stringMatcher.matchWords(text);
223223
case RULE_EXACT_MATCH:
224224
return stringPattern.equalsIgnoreCase(text);
225225
case RULE_CAMELCASE_MATCH:

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/about/AboutPluginsPage.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.eclipse.core.runtime.Status;
4040
import org.eclipse.core.runtime.SubMonitor;
4141
import org.eclipse.core.runtime.jobs.Job;
42+
import org.eclipse.core.text.StringMatcher;
4243
import org.eclipse.jface.dialogs.IDialogConstants;
4344
import org.eclipse.jface.layout.GridDataFactory;
4445
import org.eclipse.jface.util.ConfigureColumns;
@@ -73,7 +74,6 @@
7374
import org.eclipse.ui.internal.WorkbenchMessages;
7475
import org.eclipse.ui.internal.WorkbenchPlugin;
7576
import org.eclipse.ui.internal.misc.StatusUtil;
76-
import org.eclipse.ui.internal.misc.TextMatcher;
7777
import org.eclipse.ui.internal.util.BundleUtility;
7878
import org.eclipse.ui.progress.WorkbenchJob;
7979
import org.eclipse.ui.statushandlers.StatusManager;
@@ -689,14 +689,14 @@ public void setAscending(boolean ascending) {
689689
}
690690
class BundlePatternFilter extends ViewerFilter {
691691

692-
private TextMatcher matcher;
692+
private StringMatcher matcher;
693693

694694
public void setPattern(String searchPattern) {
695695
if (searchPattern == null || searchPattern.isEmpty()) {
696696
this.matcher = null;
697697
} else {
698698
String pattern = "*" + searchPattern + "*"; //$NON-NLS-1$//$NON-NLS-2$
699-
this.matcher = new TextMatcher(pattern, true, false);
699+
this.matcher = new StringMatcher(pattern, true, false);
700700
}
701701
}
702702

@@ -708,12 +708,12 @@ public boolean select(Viewer viewer, Object parentElement, Object element) {
708708

709709
if (element instanceof AboutBundleData) {
710710
AboutBundleData data = (AboutBundleData) element;
711-
return matcher.match(data.getName()) || matcher.match(data.getProviderName())
712-
|| matcher.match(data.getId());
711+
return matcher.matchWords(data.getName()) || matcher.matchWords(data.getProviderName())
712+
|| matcher.matchWords(data.getId());
713713
}
714714
else if (element instanceof AboutBundleGroupData data) {
715-
return matcher.match(data.getName()) || matcher.match(data.getProviderName())
716-
|| matcher.match(data.getId());
715+
return matcher.matchWords(data.getName()) || matcher.matchWords(data.getProviderName())
716+
|| matcher.matchWords(data.getId());
717717
}
718718
return true;
719719
}

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/misc/TextMatcher.java

Lines changed: 0 additions & 169 deletions
This file was deleted.

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/UiTestSuite.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2019 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -37,7 +37,6 @@
3737
import org.eclipse.ui.tests.fieldassist.FieldAssistTestSuite;
3838
import org.eclipse.ui.tests.filteredtree.FilteredTreeTests;
3939
import org.eclipse.ui.tests.filteredtree.PatternFilterTest;
40-
import org.eclipse.ui.tests.filteredtree.TextMatcherTest;
4140
import org.eclipse.ui.tests.internal.InternalTestSuite;
4241
import org.eclipse.ui.tests.intro.IntroTestSuite;
4342
import org.eclipse.ui.tests.keys.KeysTestSuite;
@@ -91,7 +90,6 @@
9190
ConcurrencyTestSuite.class,
9291
FilteredTreeTests.class,
9392
PatternFilterTest.class,
94-
TextMatcherTest.class,
9593
StatusHandlingTestSuite.class,
9694
MenusTestSuite.class,
9795
QuickAccessTestSuite.class,

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/filteredtree/FilteredTreeTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ public void testAddAndRemovePattern() {
117117
applyPattern("0-0-0-0 name-*");
118118
assertNumberOfTopLevelItems(1);
119119

120+
applyPattern(" 0-0-0-0 name-*");
121+
assertNumberOfTopLevelItems(1);
122+
123+
applyPattern("0-0-0-0 name-* ");
124+
assertNumberOfTopLevelItems(1);
125+
120126
applyPattern("0-0-0-0 name unknownWord");
121127
assertNumberOfTopLevelItems(0);
122128

0 commit comments

Comments
 (0)