[matcher] fallback typo-tolerant matcher to regular matcher for long patterns

After some optimizations, for the sake of performance, a matcher stopped matching anything for long patterns. Keeping this optimization, now we fall back to the regular matcher to match at least something, even without tolerance to typos

GitOrigin-RevId: 8c3be92bccd5eb6e382df32394cc27f95113e3e4
This commit is contained in:
Alexander Zolotov
2024-09-16 15:20:36 +02:00
committed by intellij-monorepo-bot
parent 3106110b50
commit 846941b571
3 changed files with 11 additions and 11 deletions

View File

@@ -8,7 +8,7 @@ import java.util.List;
import java.util.SplittableRandom;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
public class TypoTolerantMatcherTest {
@Test
@@ -42,4 +42,11 @@ public class TypoTolerantMatcherTest {
List<String> expected = List.of("foo", "bar", "buzz");
assertEquals(expected, matched);
}
@Test
public void testLongPattern() {
MinusculeMatcher matcher = NameUtil.buildMatcher("MyLongTestClassName").typoTolerant().build();
assertFalse(matcher instanceof TypoTolerantMatcher);
assertTrue(matcher.matches("MyLongTestClassName"));
}
}

View File

@@ -13,6 +13,8 @@ import java.util.function.Function;
public final class NameUtil {
private static final int MAX_LENGTH = 40;
//heuristics: 15 can take 10-20 ms in some cases, while 10 works in 1-5 ms
private static final int TYPO_AWARE_PATTERN_LIMIT = 13;
private NameUtil() {}
@@ -295,7 +297,7 @@ public final class NameUtil {
}
public MatcherBuilder typoTolerant() {
this.typoTolerant = true;
this.typoTolerant = pattern.length() <= TYPO_AWARE_PATTERN_LIMIT;
return this;
}

View File

@@ -15,10 +15,6 @@ import java.util.BitSet;
import java.util.List;
final class TypoTolerantMatcher extends MinusculeMatcher {
//heuristics: 15 can take 10-20 ms in some cases, while 10 works in 1-5 ms
private static final int TYPO_AWARE_PATTERN_LIMIT = 13;
private final char[] myPattern;
private final String myHardSeparators;
private final NameUtil.MatchingCaseSensitivity myOptions;
@@ -231,9 +227,6 @@ final class TypoTolerantMatcher extends MinusculeMatcher {
FList<TextRange> ranges = new Session(name, false, ascii).matchingFragments();
if (ranges != null) return ranges;
//do not apply typo aware matching for long patterns, it can take too much time
if (myPattern.length > TYPO_AWARE_PATTERN_LIMIT) return null;
return new Session(name, true, ascii).matchingFragments();
}
@@ -281,8 +274,6 @@ final class TypoTolerantMatcher extends MinusculeMatcher {
}
public @Nullable FList<TextRange> matchingFragments() {
if (myPattern.length > TYPO_AWARE_PATTERN_LIMIT) return null;
int length = myName.length();
if (length < myMinNameLength) {
return null;