(cg: CodeGraph, query: URLSearchParams)
| 92 | } |
| 93 | |
| 94 | export function buildSearch(cg: CodeGraph, query: URLSearchParams): unknown { |
| 95 | const raw = optionalTextParam(query, 'q'); |
| 96 | const limit = intParam(query, 'limit', { min: 1, max: 200, default: 60 }); |
| 97 | |
| 98 | // An empty search box is the palette's resting state, not a mistake — it |
| 99 | // answers with nothing rather than with an error the viewer has to special- |
| 100 | // case. A MISSING `q` is still a 400: that is a caller bug. |
| 101 | if (raw.trim() === '') return emptySearch(raw); |
| 102 | |
| 103 | // The filter grammar (`kind:function auth`) belongs to `searchNodes`; the |
| 104 | // name lookups only ever want the free-text part of what was typed. |
| 105 | const parsed = parseQuery(raw); |
| 106 | const text = parsed.text.trim(); |
| 107 | const needle = text.toLowerCase(); |
| 108 | |
| 109 | const candidates = new Map<string, Node>(); |
| 110 | const remember = (node: Node): void => { |
| 111 | if (!candidates.has(node.id)) candidates.set(node.id, node); |
| 112 | }; |
| 113 | |
| 114 | if (text.length > 0) { |
| 115 | for (const node of cg.getNodesByName(text)) remember(node); |
| 116 | for (const node of cg.getNodesByNameSubstring(text, { limit: CANDIDATE_POOL })) remember(node); |
| 117 | } |
| 118 | for (const result of cg.searchNodes(raw, { limit: CANDIDATE_POOL })) remember(result.node); |
| 119 | |
| 120 | const scored: Array<{ node: Node; match: MatchKind }> = []; |
| 121 | for (const node of candidates.values()) { |
| 122 | // `searchNodes` applies the filter grammar to its own results, but the two |
| 123 | // direct name lookups above know nothing about it — so `kind:class Cache` |
| 124 | // would otherwise pull in `CacheKey` and every `Cache` method through the |
| 125 | // substring lookup. The gate belongs to the merged candidate set. |
| 126 | if (!matchesFilters(node, parsed)) continue; |
| 127 | // An empty text portion means the query was pure filters (`kind:route`); |
| 128 | // everything `searchNodes` returned already satisfies them, so there is no |
| 129 | // name match to grade and every row is equally "related". |
| 130 | const match = needle.length === 0 ? 'related' : classify(node, needle) ?? 'related'; |
| 131 | scored.push({ node, match }); |
| 132 | } |
| 133 | |
| 134 | scored.sort((a, b) => { |
| 135 | const byMatch = MATCH_RANK[a.match] - MATCH_RANK[b.match]; |
| 136 | if (byMatch !== 0) return byMatch; |
| 137 | const byKind = kindRank(a.node.kind) - kindRank(b.node.kind); |
| 138 | if (byKind !== 0) return byKind; |
| 139 | // Production code before tests and fixtures: both are real answers, but one |
| 140 | // of them is the one someone searching for a symbol usually means. |
| 141 | const aTest = isTestPath(a.node.filePath); |
| 142 | const bTest = isTestPath(b.node.filePath); |
| 143 | if (aTest !== bTest) return aTest ? 1 : -1; |
| 144 | // Shorter names are closer to what was typed (`get` before `getOrCreate`). |
| 145 | const byLength = a.node.name.length - b.node.name.length; |
| 146 | if (byLength !== 0) return byLength; |
| 147 | return ( |
| 148 | a.node.filePath.localeCompare(b.node.filePath) || a.node.startLine - b.node.startLine |
| 149 | ); |
| 150 | }); |
| 151 |
no test coverage detected