From 89e119f12181b82fafa4bdc17c85480cbc38b1be Mon Sep 17 00:00:00 2001 From: sam hoang Date: Wed, 15 Jan 2025 01:23:18 +0700 Subject: [PATCH] feat(mentions): implement fuzzy search for file/folder mentions Adds fuzzy search functionality using Fuse.js to improve file and folder search in mentions. --- webview-ui/src/utils/context-mentions.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/webview-ui/src/utils/context-mentions.ts b/webview-ui/src/utils/context-mentions.ts index 6d846bf..7ee90b3 100644 --- a/webview-ui/src/utils/context-mentions.ts +++ b/webview-ui/src/utils/context-mentions.ts @@ -1,4 +1,5 @@ import { mentionRegex } from "../../../src/shared/context-mentions" +import Fuse from "fuse.js" export function insertMention( text: string, @@ -147,13 +148,21 @@ export function getContextMenuOptions( } } - // Get matching items, separating by type - const matchingItems = queryItems.filter((item) => - item.value?.toLowerCase().includes(lowerQuery) || - item.label?.toLowerCase().includes(lowerQuery) || - item.description?.toLowerCase().includes(lowerQuery) - ) + // Initialize Fuse instance for fuzzy search + const fuse = new Fuse(queryItems, { + keys: ["value", "label", "description"], + threshold: 0.6, + shouldSort: true, + isCaseSensitive: false, + ignoreLocation: false, + minMatchCharLength: 1, + }) + // Get fuzzy matching items + const fuseResults = query ? fuse.search(query) : [] + const matchingItems = fuseResults.map(result => result.item) + + // Separate matches by type const fileMatches = matchingItems.filter(item => item.type === ContextMenuOptionType.File || item.type === ContextMenuOptionType.Folder