Skip to content

Commit

Permalink
perf(autocomplete): better order with fuzzy search. (#2789)
Browse files Browse the repository at this point in the history
  • Loading branch information
chizukicn committed Jun 23, 2023
1 parent f611dca commit c68895f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/autocomplete/src/create.ts
@@ -1,7 +1,7 @@
import type { AutoCompleteExtractorResult, AutoCompleteFunction, AutoCompleteTemplate, SuggestResult, UnoGenerator, Variant } from '@unocss/core'
import { escapeRegExp, toArray, uniq } from '@unocss/core'
import { LRUCache } from 'lru-cache'
import { Fzf } from 'fzf'
import { Fzf, byLengthAsc, byStartAsc } from 'fzf'
import { parseAutocomplete } from './parse'
import type { AutocompleteOptions, ParsedAutocompleteTemplate, UnocssAutocomplete } from './types'
import { searchAttrKey, searchUsageBoundary } from './utils'
Expand All @@ -11,7 +11,6 @@ export function createAutocomplete(uno: UnoGenerator, options: AutocompleteOptio
const cache = new LRUCache<string, string[]>({ max: 5000 })

let staticUtils: string[] = []
let staticFzf = new Fzf<string[]>([])

const templates: (AutoCompleteTemplate | AutoCompleteFunction)[] = []

Expand Down Expand Up @@ -77,7 +76,7 @@ export function createAutocomplete(uno: UnoGenerator, options: AutocompleteOptio
const variantPrefix = input.slice(0, idx)
const variantSuffix = input.slice(idx + input.length)

const result = processSuggestions(
let result = processSuggestions(
await Promise.all([
suggestSelf(processed),
suggestStatic(processed),
Expand All @@ -89,6 +88,12 @@ export function createAutocomplete(uno: UnoGenerator, options: AutocompleteOptio
variantSuffix,
)

if (matchType === 'fuzzy') {
const fzf = new Fzf(result, {
tiebreakers: [byStartAsc, byLengthAsc],
})
result = fzf.find(input).map(i => i.item)
}
cache.set(input, result)
return result
}
Expand Down Expand Up @@ -139,7 +144,7 @@ export function createAutocomplete(uno: UnoGenerator, options: AutocompleteOptio

async function suggestStatic(input: string) {
if (matchType === 'fuzzy')
return staticFzf.find(input).map(i => i.item)
return staticUtils
return staticUtils.filter(i => i.startsWith(input))
}

Expand Down Expand Up @@ -175,7 +180,6 @@ export function createAutocomplete(uno: UnoGenerator, options: AutocompleteOptio
...Object.keys(uno.config.rulesStaticMap),
...uno.config.shortcuts.filter(i => typeof i[0] === 'string').map(i => i[0] as string),
]
staticFzf = new Fzf(staticUtils)
templates.length = 0
templates.push(
...uno.config.autocomplete.templates || [],
Expand Down
5 changes: 5 additions & 0 deletions test/autocomplete-fuzzy.test.ts
Expand Up @@ -43,4 +43,9 @@ describe('autocomplete-fuzzy', () => {
expect(await ac.suggest('bmc'))
.includes('bg-mode-color')
})

it('order', async () => {
const suggestions = await ac.suggest('ga')
expect(suggestions.indexOf('gap-0')).toBeLessThan(suggestions.indexOf('gap-1'))
})
})

0 comments on commit c68895f

Please sign in to comment.