Changesets · Branches · Tags · Compare
| Changeset | bda3211c08b4940cb2d52ba03cc0dc245991e451ebe5ab98df7b56ccb41c186b |
|---|---|
| Author | chezsmithy <chezsmithy@me.com> · 2025-07-04 22:59 -07:00 |
| Committer | chezsmithy <chezsmithy@me.com> · 2025-07-04 22:59 -07:00 |
| Parents | f840a2b4e8a4 |
1 file changed
modified gui/src/context/SubmenuContextProviders.tsx +241 −3
| @@ -29,6 +29,14 @@ | ||
| 29 | 29 | |
| 30 | 30 | const MAX_LENGTH = 70; |
| 31 | 31 | |
| 32 | +// Enhanced search result interface for intelligent sorting | |
| 33 | +interface EnhancedSearchResult | |
| 34 | + extends SearchResult, | |
| 35 | + ContextSubmenuItemWithProvider { | |
| 36 | + sortPriority: number; | |
| 37 | + matchQuality: number; | |
| 38 | +} | |
| 39 | + | |
| 32 | 40 | interface SubtextContextProvidersContextType { |
| 33 | 41 | getSubmenuContextItems: ( |
| 34 | 42 | providerTitle: string | undefined, |
| @@ -43,6 +51,91 @@ | ||
| 43 | 51 | const SubmenuContextProvidersContext = |
| 44 | 52 | createContext<SubtextContextProvidersContextType>(initialContextProviders); |
| 45 | 53 | |
| 54 | +// Helper functions for intelligent file sorting | |
| 55 | +function hasExactWordMatch(text: string, query: string): boolean { | |
| 56 | + const words = text.split(/[^a-zA-Z0-9]+/); | |
| 57 | + return words.some((word) => word === query); | |
| 58 | +} | |
| 59 | + | |
| 60 | +function isCommonDevFile(fileName: string): boolean { | |
| 61 | + const commonExtensions = [ | |
| 62 | + ".ts", | |
| 63 | + ".tsx", | |
| 64 | + ".js", | |
| 65 | + ".jsx", | |
| 66 | + ".py", | |
| 67 | + ".java", | |
| 68 | + ".cpp", | |
| 69 | + ".c", | |
| 70 | + ".h", | |
| 71 | + ".vue", | |
| 72 | + ".svelte", | |
| 73 | + ]; | |
| 74 | + const commonNames = [ | |
| 75 | + "index", | |
| 76 | + "main", | |
| 77 | + "app", | |
| 78 | + "component", | |
| 79 | + "service", | |
| 80 | + "util", | |
| 81 | + "helper", | |
| 82 | + "config", | |
| 83 | + "types", | |
| 84 | + ]; | |
| 85 | + | |
| 86 | + const extension = fileName.substring(fileName.lastIndexOf(".")); | |
| 87 | + const baseName = fileName | |
| 88 | + .substring(0, fileName.lastIndexOf(".")) | |
| 89 | + .toLowerCase(); | |
| 90 | + | |
| 91 | + return commonExtensions.includes(extension) || commonNames.includes(baseName); | |
| 92 | +} | |
| 93 | + | |
| 94 | +function isInCommonDirectory(filePath: string): boolean { | |
| 95 | + const commonDirs = [ | |
| 96 | + "src/", | |
| 97 | + "lib/", | |
| 98 | + "components/", | |
| 99 | + "utils/", | |
| 100 | + "helpers/", | |
| 101 | + "services/", | |
| 102 | + "pages/", | |
| 103 | + "views/", | |
| 104 | + "hooks/", | |
| 105 | + "store/", | |
| 106 | + "types/", | |
| 107 | + "interfaces/", | |
| 108 | + "models/", | |
| 109 | + "api/", | |
| 110 | + ]; | |
| 111 | + return commonDirs.some((dir) => filePath.includes(dir)); | |
| 112 | +} | |
| 113 | + | |
| 114 | +function matchesCamelCaseOrAbbreviation( | |
| 115 | + fileName: string, | |
| 116 | + query: string, | |
| 117 | +): boolean { | |
| 118 | + // Extract capital letters for camel case matching | |
| 119 | + const capitals = fileName.match(/[A-Z]/g)?.join("").toLowerCase() || ""; | |
| 120 | + | |
| 121 | + // Check if query matches the capital letters pattern | |
| 122 | + if (capitals.length > 0 && capitals.includes(query)) { | |
| 123 | + return true; | |
| 124 | + } | |
| 125 | + | |
| 126 | + // Check for abbreviation matches (first letters of words) | |
| 127 | + const words = fileName.split(/[^a-zA-Z0-9]+/).filter((w) => w.length > 0); | |
| 128 | + if (words.length > 1) { | |
| 129 | + const abbreviation = words | |
| 130 | + .map((word) => word[0]) | |
| 131 | + .join("") | |
| 132 | + .toLowerCase(); | |
| 133 | + return abbreviation.includes(query); | |
| 134 | + } | |
| 135 | + | |
| 136 | + return false; | |
| 137 | +} | |
| 138 | + | |
| 46 | 139 | export const SubmenuContextProvidersProvider = ({ |
| 47 | 140 | children, |
| 48 | 141 | }: { |
| @@ -128,6 +221,98 @@ | ||
| 128 | 221 | new Map<ContextProviderName, AbortController>(), |
| 129 | 222 | ).current; |
| 130 | 223 | |
| 224 | + const calculateFileSortPriority = useCallback( | |
| 225 | + ( | |
| 226 | + result: SearchResult & ContextSubmenuItemWithProvider, | |
| 227 | + query: string, | |
| 228 | + openFiles: ContextSubmenuItemWithProvider[], | |
| 229 | + ): number => { | |
| 230 | + const fileName = result.title.toLowerCase(); | |
| 231 | + const filePath = result.description.toLowerCase(); | |
| 232 | + const queryLower = query.toLowerCase(); | |
| 233 | + | |
| 234 | + // Priority 1: Exact filename match | |
| 235 | + if (fileName === queryLower) { | |
| 236 | + return 1; | |
| 237 | + } | |
| 238 | + | |
| 239 | + // Priority 2: Recently opened files | |
| 240 | + if (openFiles.some((f) => f.id === result.id)) { | |
| 241 | + return 2; | |
| 242 | + } | |
| 243 | + | |
| 244 | + // Priority 3: Filename starts with query | |
| 245 | + if (fileName.startsWith(queryLower)) { | |
| 246 | + return 3; | |
| 247 | + } | |
| 248 | + | |
| 249 | + // Priority 4: Exact word match in filename | |
| 250 | + if (hasExactWordMatch(fileName, queryLower)) { | |
| 251 | + return 4; | |
| 252 | + } | |
| 253 | + | |
| 254 | + // Priority 5: Common development files | |
| 255 | + if (isCommonDevFile(fileName)) { | |
| 256 | + return 5; | |
| 257 | + } | |
| 258 | + | |
| 259 | + // Priority 6: Files in common directories | |
| 260 | + if (isInCommonDirectory(filePath)) { | |
| 261 | + return 6; | |
| 262 | + } | |
| 263 | + | |
| 264 | + // Priority 7: CamelCase/abbreviation matches | |
| 265 | + if (matchesCamelCaseOrAbbreviation(fileName, queryLower)) { | |
| 266 | + return 7; | |
| 267 | + } | |
| 268 | + | |
| 269 | + // Priority 8: Path starts with query | |
| 270 | + if (filePath.startsWith(queryLower)) { | |
| 271 | + return 8; | |
| 272 | + } | |
| 273 | + | |
| 274 | + // Priority 9: Default | |
| 275 | + return 9; | |
| 276 | + }, | |
| 277 | + [], | |
| 278 | + ); | |
| 279 | + | |
| 280 | + const calculateMatchQuality = useCallback( | |
| 281 | + ( | |
| 282 | + result: SearchResult & ContextSubmenuItemWithProvider, | |
| 283 | + query: string, | |
| 284 | + ): number => { | |
| 285 | + const fileName = result.title.toLowerCase(); | |
| 286 | + const queryLower = query.toLowerCase(); | |
| 287 | + let quality = 0; | |
| 288 | + | |
| 289 | + // Exact match bonus | |
| 290 | + if (fileName === queryLower) quality += 100; | |
| 291 | + | |
| 292 | + // Prefix match bonus | |
| 293 | + if (fileName.startsWith(queryLower)) quality += 50; | |
| 294 | + | |
| 295 | + // Contains match bonus | |
| 296 | + if (fileName.includes(queryLower)) quality += 25; | |
| 297 | + | |
| 298 | + // Shorter filename bonus (prefer concise names) | |
| 299 | + quality += Math.max(0, 50 - fileName.length); | |
| 300 | + | |
| 301 | + // Extension preference (common dev files) | |
| 302 | + if ( | |
| 303 | + fileName.endsWith(".ts") || | |
| 304 | + fileName.endsWith(".tsx") || | |
| 305 | + fileName.endsWith(".js") || | |
| 306 | + fileName.endsWith(".jsx") | |
| 307 | + ) { | |
| 308 | + quality += 10; | |
| 309 | + } | |
| 310 | + | |
| 311 | + return quality; | |
| 312 | + }, | |
| 313 | + [], | |
| 314 | + ); | |
| 315 | + | |
| 131 | 316 | const getSubmenuContextItems = useCallback( |
| 132 | 317 | ( |
| 133 | 318 | providerTitle: string | undefined, |
| @@ -168,9 +353,57 @@ | ||
| 168 | 353 | }); |
| 169 | 354 | } |
| 170 | 355 | } |
| 171 | - searchResults.sort((a, b) => b.score - a.score); | |
| 172 | 356 | |
| 173 | - // 2. Add fallback results if no search results | |
| 357 | + // 2. Enhanced sorting for file provider | |
| 358 | + if ( | |
| 359 | + providerTitle === "file" || | |
| 360 | + (!providerTitle && | |
| 361 | + searchResults.some((r) => r.providerTitle === "file")) | |
| 362 | + ) { | |
| 363 | + const enhancedResults: EnhancedSearchResult[] = searchResults.map( | |
| 364 | + (result): EnhancedSearchResult => ({ | |
| 365 | + ...result, | |
| 366 | + id: result.id, | |
| 367 | + title: result.title, | |
| 368 | + description: result.description, | |
| 369 | + providerTitle: result.providerTitle, | |
| 370 | + sortPriority: calculateFileSortPriority( | |
| 371 | + result, | |
| 372 | + query, | |
| 373 | + lastOpenFilesRef.current, | |
| 374 | + ), | |
| 375 | + matchQuality: calculateMatchQuality(result, query), | |
| 376 | + }), | |
| 377 | + ); | |
| 378 | + | |
| 379 | + // Sort by multiple criteria | |
| 380 | + enhancedResults.sort((a, b) => { | |
| 381 | + // Primary: Sort priority (lower = better) | |
| 382 | + if (a.sortPriority !== b.sortPriority) { | |
| 383 | + return a.sortPriority - b.sortPriority; | |
| 384 | + } | |
| 385 | + | |
| 386 | + // Secondary: Match quality (higher = better) | |
| 387 | + if (a.matchQuality !== b.matchQuality) { | |
| 388 | + return b.matchQuality - a.matchQuality; | |
| 389 | + } | |
| 390 | + | |
| 391 | + // Tertiary: MiniSearch relevance score (higher = better) | |
| 392 | + if (a.score !== b.score) { | |
| 393 | + return b.score - a.score; | |
| 394 | + } | |
| 395 | + | |
| 396 | + // Quaternary: Path length (shorter = better) | |
| 397 | + return a.description.length - b.description.length; | |
| 398 | + }); | |
| 399 | + | |
| 400 | + searchResults = enhancedResults; | |
| 401 | + } else { | |
| 402 | + // Default sorting for non-file providers | |
| 403 | + searchResults.sort((a, b) => b.score - a.score); | |
| 404 | + } | |
| 405 | + | |
| 406 | + // 3. Add fallback results if no search results | |
| 174 | 407 | if (searchResults.length === 0) { |
| 175 | 408 | const fallbackItems = ( |
| 176 | 409 | providerTitle ? (fallbackResults[providerTitle] ?? []) : [] |
| @@ -223,7 +456,12 @@ | ||
| 223 | 456 | return []; |
| 224 | 457 | } |
| 225 | 458 | }, |
| 226 | - [fallbackResults, minisearches], | |
| 459 | + [ | |
| 460 | + fallbackResults, | |
| 461 | + minisearches, | |
| 462 | + calculateFileSortPriority, | |
| 463 | + calculateMatchQuality, | |
| 464 | + ], | |
| 227 | 465 | ); |
| 228 | 466 | |
| 229 | 467 | const loadSubmenuItems = useCallback( |