(
client: Pick<RpcIntelligenceClient, 'queryRows'>,
project: string,
path: string,
)
| 26 | } |
| 27 | |
| 28 | export async function loadFileSymbols( |
| 29 | client: Pick<RpcIntelligenceClient, 'queryRows'>, |
| 30 | project: string, |
| 31 | path: string, |
| 32 | ): Promise<FileSymbolResult> { |
| 33 | if (project.trim().length === 0 || path.trim().length === 0) { |
| 34 | throw new Error('Select a project and file before loading symbols.'); |
| 35 | } |
| 36 | const results = await Promise.allSettled(LABELS.map(async ([label]) => { |
| 37 | const query = declarationsInFiles(label, [path], FILE_SYMBOL_LIMIT); |
| 38 | if (!query) throw new Error('The file path could not be queried.'); |
| 39 | return client.queryRows(project, query); |
| 40 | })); |
| 41 | const symbols = new Map<string, SymbolRef>(); |
| 42 | const failed: string[] = []; |
| 43 | const absent: string[] = []; |
| 44 | const capped: string[] = []; |
| 45 | let answered = 0; |
| 46 | let invalid = 0; |
| 47 | for (const [index, result] of results.entries()) { |
| 48 | const [label, kind] = LABELS[index]; |
| 49 | if (result.status === 'rejected') { |
| 50 | (missingLabel(result.reason, label) ? absent : failed).push(label); |
| 51 | continue; |
| 52 | } |
| 53 | answered++; |
| 54 | if (result.value.length >= FILE_SYMBOL_LIMIT) capped.push(label); |
| 55 | for (const row of result.value) { |
| 56 | const name = row[COLUMNS.declarations[0]]; |
| 57 | const qualifiedName = row[COLUMNS.declarations[1]] || undefined; |
| 58 | const filePath = row[COLUMNS.declarations[2]]; |
| 59 | const startLine = Number(row[COLUMNS.declarations[3]]); |
| 60 | if (!name || filePath !== path || !Number.isSafeInteger(startLine) || startLine < 1) { |
| 61 | invalid++; |
| 62 | continue; |
| 63 | } |
| 64 | const target = twinTargetOf({ name, qualifiedName, filePath, startLine, kind }); |
| 65 | if (!target) { invalid++; continue; } |
| 66 | const key = `${filePath}:${qualifiedName ?? `${name}:${startLine}`}`; |
| 67 | if (!symbols.has(key)) symbols.set(key, { ...target, projectName: project }); |
| 68 | } |
| 69 | } |
| 70 | if (answered === 0) { |
| 71 | throw new Error(failed.length > 0 |
| 72 | ? `Could not load file symbols. Queries failed for ${failed.join(', ')}.${absent.length ? ` Labels unavailable: ${absent.join(', ')}.` : ''}` |
| 73 | : `The current index does not provide these symbol labels: ${absent.join(', ')}.`); |
| 74 | } |
| 75 | const ordered = [...symbols.values()].sort((a, b) => a.range.start.line - b.range.start.line |
| 76 | || (a.qualifiedName ?? a.name).localeCompare(b.qualifiedName ?? b.name)); |
| 77 | const notes: string[] = []; |
| 78 | if (failed.length > 0) notes.push(`Partial results: queries failed for ${failed.join(', ')}.`); |
| 79 | if (absent.length > 0) notes.push(`Labels unavailable in this index: ${absent.join(', ')}.`); |
| 80 | if (capped.length > 0) notes.push(`Some symbols may be omitted: ${capped.join(', ')} reached the ${FILE_SYMBOL_LIMIT}-row query limit.`); |
| 81 | if (ordered.length > FILE_SYMBOL_LIMIT) notes.push(`Showing the first ${FILE_SYMBOL_LIMIT} returned symbols by declaration line.`); |
| 82 | if (invalid > 0) notes.push(`${invalid} result${invalid === 1 ? '' : 's'} without a valid declaration in this file omitted.`); |
| 83 | return { symbols: ordered.slice(0, FILE_SYMBOL_LIMIT), message: notes.join(' ') }; |
| 84 | } |
no test coverage detected