(query: string, projectNameTokens: Set<string> | undefined)
| 329 | const PATH_RELEVANCE_QUERY_CACHE = 8; |
| 330 | |
| 331 | function pathRelevanceQuery(query: string, projectNameTokens: Set<string> | undefined): PathRelevanceQuery { |
| 332 | const cached = pathRelevanceQueries.get(query); |
| 333 | if (cached && cached.projectNameTokens === projectNameTokens) return cached; |
| 334 | |
| 335 | // Score per original query WORD, not per sub-token. A single PascalCase word |
| 336 | // splits into many sub-tokens (a project name "SuperBizAgent" → |
| 337 | // superbizagent / super / biz / agent) that all match the SAME path segment, |
| 338 | // so summing per sub-token boosted that path 4× for one concept — enough to |
| 339 | // bury the rest of the query's stack (#720). A word matches a path level if |
| 340 | // ANY of its sub-tokens do, and counts ONCE; distinct words still each add. |
| 341 | // Split the ORIGINAL-case query into words; extractSearchTerms does the |
| 342 | // camelCase/snake split per word (so `getUserName` still matches a |
| 343 | // `get_user_name` path) — we just attribute each word's matches once. |
| 344 | const allWords = query.split(/\s+/).filter((w) => w.length > 0); |
| 345 | |
| 346 | // A query word that just names the PROJECT (its go.mod / package.json / repo |
| 347 | // name) carries no discriminative path signal — drop it so the rest of the |
| 348 | // query decides the ranking, instead of every file under a `<ProjectName>…/` |
| 349 | // tree winning on the project name alone (#720). Only when OTHER words remain, |
| 350 | // so a bare project-name query still scores on its path. |
| 351 | const kept = |
| 352 | projectNameTokens && projectNameTokens.size > 0 |
| 353 | ? allWords.filter((w) => !projectNameTokens.has(normalizeNameToken(w))) |
| 354 | : allWords; |
| 355 | const scored = kept.length > 0 ? kept : allWords; |
| 356 | |
| 357 | // A word that occurs twice adds its score twice; words with the same |
| 358 | // sub-tokens score alike, so each such group is matched once and counted. |
| 359 | const groups = new Map<string, { subtokens: string[]; count: number }>(); |
| 360 | for (const word of scored) { |
| 361 | // Use base terms only — stem variants inflate path scores by generating |
| 362 | // many near-duplicate terms that all match the same path segments. |
| 363 | const subtokens = extractSearchTerms(word, { stems: false }); |
| 364 | if (subtokens.length === 0) continue; |
| 365 | const key = subtokens.join('\u0000'); |
| 366 | const group = groups.get(key); |
| 367 | if (group) group.count++; |
| 368 | else groups.set(key, { subtokens, count: 1 }); |
| 369 | } |
| 370 | |
| 371 | const queryLower = query.toLowerCase(); |
| 372 | const prepared: PathRelevanceQuery = { |
| 373 | projectNameTokens, |
| 374 | empty: allWords.length === 0, |
| 375 | words: [...groups.values()], |
| 376 | isTestQuery: queryLower.includes('test') || queryLower.includes('spec'), |
| 377 | }; |
| 378 | pathRelevanceQueries.delete(query); |
| 379 | pathRelevanceQueries.set(query, prepared); |
| 380 | if (pathRelevanceQueries.size > PATH_RELEVANCE_QUERY_CACHE) { |
| 381 | pathRelevanceQueries.delete(pathRelevanceQueries.keys().next().value!); |
| 382 | } |
| 383 | return prepared; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Score path relevance to a query |
no test coverage detected