Skip to content

Commit

Permalink
Replace TextMatcher in favor of extended StringMatcher
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ptziegler authored and vogella committed Jan 8, 2025
1 parent 58bb65c commit db41fb4
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.text.StringMatcher;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
Expand All @@ -36,7 +37,6 @@
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.internal.WorkbenchMessages;
import org.eclipse.ui.internal.misc.TextMatcher;
import org.eclipse.ui.internal.util.Util;
import org.eclipse.ui.progress.WorkbenchJob;

Expand Down Expand Up @@ -73,16 +73,16 @@ public interface FilterMatcher {
}

private class DefaultFilterMatcher implements FilterMatcher {
private TextMatcher fMatcher;
private StringMatcher fMatcher;

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

@Override
public boolean match(Object element) {
return fMatcher.match(fLabelProvider.getText(element));
return fMatcher.matchWords(fLabelProvider.getText(element));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.text.StringMatcher;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.ContentViewer;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.ui.internal.misc.TextMatcher;

/**
* A filter used in conjunction with <code>FilteredTree</code>. In order to
Expand Down Expand Up @@ -57,7 +57,7 @@ public class PatternFilter extends ViewerFilter {
/**
* The string pattern matcher used for this pattern filter.
*/
private TextMatcher matcher;
private StringMatcher matcher;

private boolean useEarlyReturnIfMatcherIsNull = true;

Expand Down Expand Up @@ -173,7 +173,7 @@ public void setPattern(String patternString) {
if (includeLeadingWildcard) {
pattern = "*" + pattern; //$NON-NLS-1$
}
matcher = new TextMatcher(pattern, true, false);
matcher = new StringMatcher(pattern.trim(), true, false);
}
}

Expand All @@ -197,7 +197,7 @@ private boolean match(String string) {
if (matcher == null) {
return true;
}
return matcher.match(string);
return matcher.matchWords(string);
}

/**
Expand Down Expand Up @@ -289,7 +289,7 @@ protected boolean wordMatches(String text) {
}

// Otherwise check if any of the words of the text matches
String[] words = TextMatcher.getWords(text);
String[] words = StringMatcher.getWords(text);
for (String word : words) {
if (!match(word)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*******************************************************************************/
package org.eclipse.ui.dialogs;

import org.eclipse.core.text.StringMatcher;
import org.eclipse.jface.util.Util;
import org.eclipse.ui.internal.misc.TextMatcher;

/**
* A search pattern defines how search results are found.
Expand Down Expand Up @@ -120,7 +120,7 @@ public class SearchPattern {

private String initialPattern;

private TextMatcher stringMatcher;
private StringMatcher stringMatcher;

private static final char START_SYMBOL = '>';

Expand Down Expand Up @@ -200,7 +200,7 @@ public void setPattern(String stringPattern) {
initializePatternAndMatchRule(stringPattern);
matchRule = matchRule & this.allowedRules;
if (matchRule == RULE_PATTERN_MATCH) {
stringMatcher = new TextMatcher(this.stringPattern, true, false);
stringMatcher = new StringMatcher(this.stringPattern.trim(), true, false);
}
}

Expand All @@ -219,7 +219,7 @@ public boolean matches(String text) {
case RULE_BLANK_MATCH:
return true;
case RULE_PATTERN_MATCH:
return stringMatcher.match(text);
return stringMatcher.matchWords(text);
case RULE_EXACT_MATCH:
return stringPattern.equalsIgnoreCase(text);
case RULE_CAMELCASE_MATCH:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.text.StringMatcher;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.util.ConfigureColumns;
Expand Down Expand Up @@ -73,7 +74,6 @@
import org.eclipse.ui.internal.WorkbenchMessages;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.misc.StatusUtil;
import org.eclipse.ui.internal.misc.TextMatcher;
import org.eclipse.ui.internal.util.BundleUtility;
import org.eclipse.ui.progress.WorkbenchJob;
import org.eclipse.ui.statushandlers.StatusManager;
Expand Down Expand Up @@ -689,14 +689,14 @@ public void setAscending(boolean ascending) {
}
class BundlePatternFilter extends ViewerFilter {

private TextMatcher matcher;
private StringMatcher matcher;

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

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

if (element instanceof AboutBundleData) {
AboutBundleData data = (AboutBundleData) element;
return matcher.match(data.getName()) || matcher.match(data.getProviderName())
|| matcher.match(data.getId());
return matcher.matchWords(data.getName()) || matcher.matchWords(data.getProviderName())
|| matcher.matchWords(data.getId());
}
else if (element instanceof AboutBundleGroupData data) {
return matcher.match(data.getName()) || matcher.match(data.getProviderName())
|| matcher.match(data.getId());
return matcher.matchWords(data.getName()) || matcher.matchWords(data.getProviderName())
|| matcher.matchWords(data.getId());
}
return true;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -37,7 +37,6 @@
import org.eclipse.ui.tests.fieldassist.FieldAssistTestSuite;
import org.eclipse.ui.tests.filteredtree.FilteredTreeTests;
import org.eclipse.ui.tests.filteredtree.PatternFilterTest;
import org.eclipse.ui.tests.filteredtree.TextMatcherTest;
import org.eclipse.ui.tests.internal.InternalTestSuite;
import org.eclipse.ui.tests.intro.IntroTestSuite;
import org.eclipse.ui.tests.keys.KeysTestSuite;
Expand Down Expand Up @@ -91,7 +90,6 @@
ConcurrencyTestSuite.class,
FilteredTreeTests.class,
PatternFilterTest.class,
TextMatcherTest.class,
StatusHandlingTestSuite.class,
MenusTestSuite.class,
QuickAccessTestSuite.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ public void testAddAndRemovePattern() {
applyPattern("0-0-0-0 name-*");
assertNumberOfTopLevelItems(1);

applyPattern(" 0-0-0-0 name-*");
assertNumberOfTopLevelItems(1);

applyPattern("0-0-0-0 name-* ");
assertNumberOfTopLevelItems(1);

applyPattern("0-0-0-0 name unknownWord");
assertNumberOfTopLevelItems(0);

Expand Down
Loading

0 comments on commit db41fb4

Please sign in to comment.