Skip to content

Commit

Permalink
Simplify matcher interface
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Mar 13, 2024
1 parent 5206edb commit e244d51
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
8 changes: 4 additions & 4 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
}
Expand Down

0 comments on commit e244d51

Please sign in to comment.