Файли
openidea/src/features/http-client/variableNavigation.ts
T
Orca 43c9053624 fix(http-client): open the variable of the selected environment (#7681)
Go to Declaration on {{name}} in a .http file listed the key of every environment. The lookup now receives the environment picked in Run with and follows the order a run reads it: the private file, the public file, $shared, then the last @name line of the file. With No Environment it still lists every key. Keys whose value is an object, an array or null are not variables and are skipped; escaped key names and a key written twice (the last one wins) are handled.

Closes #7672

Co-authored-by: og-orca <[email protected]>
2026-12-03 14:29:18 +08:00

71 рядки
3.1 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
/**
* 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();
};
}