From e244d5157916b054eadd1fb0cb8663d78d7ec685 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 13 Mar 2024 08:53:35 +0100 Subject: [PATCH] Simplify matcher interface --- src/filter.ts | 12 ++++++------ src/state.ts | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/filter.ts b/src/filter.ts index 820b46b..5fa0269 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -43,7 +43,7 @@ export class FuzzyMatcher { ret(score: number, matched: readonly number[]) { this.score = score this.matched = matched - return true + return this } // Matches a given word (completion) against the pattern (input). @@ -53,9 +53,9 @@ export class FuzzyMatcher { // // The score is a number that is more negative the worse the match // is. See `Penalty` above. - match(word: string): boolean { + match(word: string): {score: number, matched: readonly number[]} | null { if (this.pattern.length == 0) return this.ret(Penalty.NotFull, []) - if (word.length < this.pattern.length) return false + if (word.length < this.pattern.length) return null let {chars, folded, any, precise, byWord} = this // For single-character queries, only match when they occur right // at the start @@ -64,7 +64,7 @@ export class FuzzyMatcher { let score = firstSize == word.length ? 0 : Penalty.NotFull if (first == chars[0]) {} else if (first == folded[0]) score += Penalty.CaseFold - else return false + else return null return this.ret(score, [0, firstSize]) } let direct = word.indexOf(this.pattern) @@ -78,7 +78,7 @@ export class FuzzyMatcher { i += codePointSize(next) } // No match, exit immediately - if (anyTo < len) return false + if (anyTo < len) return null } // This tracks the extent of the precise (non-folded, not @@ -129,7 +129,7 @@ export class FuzzyMatcher { if (byWordTo == len) return this.result(Penalty.ByWord + (byWordFolded ? Penalty.CaseFold : 0) + Penalty.NotStart + (wordAdjacent ? 0 : Penalty.Gap), byWord, word) - return chars.length == 2 ? false + return chars.length == 2 ? null : this.result((any[0] ? Penalty.NotStart : 0) + Penalty.CaseFold + Penalty.Gap, any, word) } diff --git a/src/state.ts b/src/state.ts index 5867976..ad59435 100644 --- a/src/state.ts +++ b/src/state.ts @@ -35,10 +35,10 @@ function sortOptions(active: readonly ActiveSource[], state: EditorState) { addOption(new Option(option, a.source, getMatch ? getMatch(option) : [], 1e9 - options.length)) } } else { - let matcher = new FuzzyMatcher(state.sliceDoc(a.from, a.to)) - for (let option of a.result.options) if (matcher.match(option.label)) { - let matched = !option.displayLabel ? matcher.matched : getMatch ? getMatch(option, matcher.matched) : [] - addOption(new Option(option, a.source, matched, matcher.score + (option.boost || 0))) + let matcher = new FuzzyMatcher(state.sliceDoc(a.from, a.to)), match + for (let option of a.result.options) if (match = matcher.match(option.label)) { + let matched = !option.displayLabel ? match.matched : getMatch ? getMatch(option, match.matched) : [] + addOption(new Option(option, a.source, matched, match.score + (option.boost || 0))) } } }