From e3c9e489e347b81e6f50663336677edf0738b079 Mon Sep 17 00:00:00 2001 From: chezsmithy Date: Fri, 4 Jul 2025 22:59:02 -0700 Subject: [PATCH 1/2] feat: :sparkles: Enhance searching for context providers --- gui/src/context/SubmenuContextProviders.tsx | 244 +++++++++++++++++++- 1 file changed, 241 insertions(+), 3 deletions(-) diff --git a/gui/src/context/SubmenuContextProviders.tsx b/gui/src/context/SubmenuContextProviders.tsx index cf3939c48..b3d8ca21f 100644 --- a/gui/src/context/SubmenuContextProviders.tsx +++ b/gui/src/context/SubmenuContextProviders.tsx @@ -29,6 +29,14 @@ const MINISEARCH_OPTIONS = { const MAX_LENGTH = 70; +// Enhanced search result interface for intelligent sorting +interface EnhancedSearchResult + extends SearchResult, + ContextSubmenuItemWithProvider { + sortPriority: number; + matchQuality: number; +} + interface SubtextContextProvidersContextType { getSubmenuContextItems: ( providerTitle: string | undefined, @@ -43,6 +51,91 @@ const initialContextProviders: SubtextContextProvidersContextType = { const SubmenuContextProvidersContext = createContext(initialContextProviders); +// Helper functions for intelligent file sorting +function hasExactWordMatch(text: string, query: string): boolean { + const words = text.split(/[^a-zA-Z0-9]+/); + return words.some((word) => word === query); +} + +function isCommonDevFile(fileName: string): boolean { + const commonExtensions = [ + ".ts", + ".tsx", + ".js", + ".jsx", + ".py", + ".java", + ".cpp", + ".c", + ".h", + ".vue", + ".svelte", + ]; + const commonNames = [ + "index", + "main", + "app", + "component", + "service", + "util", + "helper", + "config", + "types", + ]; + + const extension = fileName.substring(fileName.lastIndexOf(".")); + const baseName = fileName + .substring(0, fileName.lastIndexOf(".")) + .toLowerCase(); + + return commonExtensions.includes(extension) || commonNames.includes(baseName); +} + +function isInCommonDirectory(filePath: string): boolean { + const commonDirs = [ + "src/", + "lib/", + "components/", + "utils/", + "helpers/", + "services/", + "pages/", + "views/", + "hooks/", + "store/", + "types/", + "interfaces/", + "models/", + "api/", + ]; + return commonDirs.some((dir) => filePath.includes(dir)); +} + +function matchesCamelCaseOrAbbreviation( + fileName: string, + query: string, +): boolean { + // Extract capital letters for camel case matching + const capitals = fileName.match(/[A-Z]/g)?.join("").toLowerCase() || ""; + + // Check if query matches the capital letters pattern + if (capitals.length > 0 && capitals.includes(query)) { + return true; + } + + // Check for abbreviation matches (first letters of words) + const words = fileName.split(/[^a-zA-Z0-9]+/).filter((w) => w.length > 0); + if (words.length > 1) { + const abbreviation = words + .map((word) => word[0]) + .join("") + .toLowerCase(); + return abbreviation.includes(query); + } + + return false; +} + export const SubmenuContextProvidersProvider = ({ children, }: { @@ -128,6 +221,98 @@ export const SubmenuContextProvidersProvider = ({ new Map(), ).current; + const calculateFileSortPriority = useCallback( + ( + result: SearchResult & ContextSubmenuItemWithProvider, + query: string, + openFiles: ContextSubmenuItemWithProvider[], + ): number => { + const fileName = result.title.toLowerCase(); + const filePath = result.description.toLowerCase(); + const queryLower = query.toLowerCase(); + + // Priority 1: Exact filename match + if (fileName === queryLower) { + return 1; + } + + // Priority 2: Recently opened files + if (openFiles.some((f) => f.id === result.id)) { + return 2; + } + + // Priority 3: Filename starts with query + if (fileName.startsWith(queryLower)) { + return 3; + } + + // Priority 4: Exact word match in filename + if (hasExactWordMatch(fileName, queryLower)) { + return 4; + } + + // Priority 5: Common development files + if (isCommonDevFile(fileName)) { + return 5; + } + + // Priority 6: Files in common directories + if (isInCommonDirectory(filePath)) { + return 6; + } + + // Priority 7: CamelCase/abbreviation matches + if (matchesCamelCaseOrAbbreviation(fileName, queryLower)) { + return 7; + } + + // Priority 8: Path starts with query + if (filePath.startsWith(queryLower)) { + return 8; + } + + // Priority 9: Default + return 9; + }, + [], + ); + + const calculateMatchQuality = useCallback( + ( + result: SearchResult & ContextSubmenuItemWithProvider, + query: string, + ): number => { + const fileName = result.title.toLowerCase(); + const queryLower = query.toLowerCase(); + let quality = 0; + + // Exact match bonus + if (fileName === queryLower) quality += 100; + + // Prefix match bonus + if (fileName.startsWith(queryLower)) quality += 50; + + // Contains match bonus + if (fileName.includes(queryLower)) quality += 25; + + // Shorter filename bonus (prefer concise names) + quality += Math.max(0, 50 - fileName.length); + + // Extension preference (common dev files) + if ( + fileName.endsWith(".ts") || + fileName.endsWith(".tsx") || + fileName.endsWith(".js") || + fileName.endsWith(".jsx") + ) { + quality += 10; + } + + return quality; + }, + [], + ); + const getSubmenuContextItems = useCallback( ( providerTitle: string | undefined, @@ -168,9 +353,57 @@ export const SubmenuContextProvidersProvider = ({ }); } } - searchResults.sort((a, b) => b.score - a.score); - // 2. Add fallback results if no search results + // 2. Enhanced sorting for file provider + if ( + providerTitle === "file" || + (!providerTitle && + searchResults.some((r) => r.providerTitle === "file")) + ) { + const enhancedResults: EnhancedSearchResult[] = searchResults.map( + (result): EnhancedSearchResult => ({ + ...result, + id: result.id, + title: result.title, + description: result.description, + providerTitle: result.providerTitle, + sortPriority: calculateFileSortPriority( + result, + query, + lastOpenFilesRef.current, + ), + matchQuality: calculateMatchQuality(result, query), + }), + ); + + // Sort by multiple criteria + enhancedResults.sort((a, b) => { + // Primary: Sort priority (lower = better) + if (a.sortPriority !== b.sortPriority) { + return a.sortPriority - b.sortPriority; + } + + // Secondary: Match quality (higher = better) + if (a.matchQuality !== b.matchQuality) { + return b.matchQuality - a.matchQuality; + } + + // Tertiary: MiniSearch relevance score (higher = better) + if (a.score !== b.score) { + return b.score - a.score; + } + + // Quaternary: Path length (shorter = better) + return a.description.length - b.description.length; + }); + + searchResults = enhancedResults; + } else { + // Default sorting for non-file providers + searchResults.sort((a, b) => b.score - a.score); + } + + // 3. Add fallback results if no search results if (searchResults.length === 0) { const fallbackItems = ( providerTitle ? (fallbackResults[providerTitle] ?? []) : [] @@ -223,7 +456,12 @@ export const SubmenuContextProvidersProvider = ({ return []; } }, - [fallbackResults, minisearches], + [ + fallbackResults, + minisearches, + calculateFileSortPriority, + calculateMatchQuality, + ], ); const loadSubmenuItems = useCallback( From 687e56d8dcaee6b9b810e0baa55e169e564024f2 Mon Sep 17 00:00:00 2001 From: chezsmithy Date: Sat, 5 Jul 2025 00:04:16 -0700 Subject: [PATCH 2/2] fixes --- gui/src/context/SubmenuContextProviders.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gui/src/context/SubmenuContextProviders.tsx b/gui/src/context/SubmenuContextProviders.tsx index b3d8ca21f..b82c36b6c 100644 --- a/gui/src/context/SubmenuContextProviders.tsx +++ b/gui/src/context/SubmenuContextProviders.tsx @@ -32,7 +32,7 @@ const MAX_LENGTH = 70; // Enhanced search result interface for intelligent sorting interface EnhancedSearchResult extends SearchResult, - ContextSubmenuItemWithProvider { + Omit { sortPriority: number; matchQuality: number; }