/** * Navigate › Declaration (⌘-click, ⌘B) on `{{name}}` in a `.http` file: the definition a run in the * selected environment reads — its key in the nearest `http-client.private.env.json` or * `http-client.env.json`, else the last `@name = …` line of the same file. With no environment selected, * or none of these, the key `name` in each environment and the `@name = …` line. */ import type { Text } from "@codemirror/state"; import { invalidateNavigationProviders, registerNavigationProvider } from "@/editor/api"; import { docOf } from "@/editor/lsp/lsp"; import { call, commands, type Location } from "@/platform/ipc"; import { HTTP_LANGUAGE } from "./editor"; import { effectiveEnvironment, useHttpClient } from "./store"; /** The `{{name}}` reference whose name covers document `offset`, as document offsets. */ export function variableAt(doc: Text, offset: number): { name: string; from: number; to: number } | null { const line = doc.lineAt(offset); for (const m of line.text.matchAll(/\{\{\s*([^{}\s]+)\s*\}\}/g)) { const name = m[1]!; const from = line.from + m.index + m[0].indexOf(name, 2); const to = from + name.length; // Dynamic variables (`$uuid`, `$random.integer`…) have no definition. if (offset >= from && offset < to) return name.startsWith("$") ? null : { name, from, to }; } return null; } /** `@name = value` lines of the request file defining `name`. */ export function fileVariableDefinitions(doc: Text, path: string, name: string): Location[] { const out: Location[] = []; for (let n = 1; n <= doc.lines; n++) { const text = doc.line(n).text; const m = /^(\s*@)([^\s=]+)\s*=/.exec(text); if (m?.[2] !== name) continue; const col = m[1]!.length; out.push({ path, range: { start: { line: n - 1, col }, end: { line: n - 1, col: col + name.length } } }); } return out; } export function registerVariableNavigation(): () => void { // Results are cached per document: the ones found for another environment no longer apply. const offEnvironment = useHttpClient.subscribe((s, prev) => { if (s.envByFile !== prev.envByFile) invalidateNavigationProviders(); }); const offProvider = registerNavigationProvider(async (view, offset) => { const doc = docOf(view); if (!doc || doc.virtual || doc.language !== HTTP_LANGUAGE) return null; const text = view.state.doc; const at = variableAt(text, offset); if (!at) return null; const inFile = fileVariableDefinitions(text, doc.path, at.name); const match = (targets: Location[]) => ({ from: at.from, to: at.to, targets }); try { const env = effectiveEnvironment(doc.path); if (env) { const used = await call(commands.httpVariableDefinitions(doc.path, at.name, env)); if (used.length) return match(used); if (inFile.length) return match(inFile.slice(-1)); } return match([...(await call(commands.httpVariableDefinitions(doc.path, at.name, null))), ...inFile]); } catch { // No project or unreadable environment files: the file's own definitions still count. return match(inFile); } }); return () => { offEnvironment(); offProvider(); }; }