дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
Gutter markers compared only the entry name (block:<fence line>), so after editing a block CodeMirror kept the old run button and its click ran the old commands until the file was reopened. ScriptMarker.eq now compares the whole entry, covering the npm script and launch profile gutters that share it. Part of #5624.
418 рядки
13 KiB
TypeScript
418 рядки
13 KiB
TypeScript
/**
|
||
* Gutter ▶ icons on `package.json` `scripts` entries (like IDEA's package.json
|
||
* gutter): Run / Debug launch the npm run configuration the project detected
|
||
* for the script, or a temporary one when it has none.
|
||
*/
|
||
import { RangeSet, StateEffect, StateField, type Extension } from "@codemirror/state";
|
||
import { EditorView, GutterMarker, ViewPlugin, type ViewUpdate } from "@codemirror/view";
|
||
import { iconAreaSource, mapLineStart, type EditorDocContext } from "@/editor/api";
|
||
import { executeAction } from "@/platform/actions";
|
||
import { tx } from "@/platform/i18n";
|
||
import { call, commands, type RunConfig } from "@/platform/ipc";
|
||
import { shortcutLabel } from "@/platform/keymap";
|
||
import { useProject } from "@/platform/project";
|
||
import { gutterIconMarkup, showContextMenu, type MenuItem } from "@/ui";
|
||
import { runIconsShown } from "./runGutter";
|
||
import { loadConfigs, useRun } from "./store";
|
||
import styles from "./Run.module.css";
|
||
|
||
/** One `scripts` entry: the script name and its 0-based line. */
|
||
export interface ScriptEntry {
|
||
name: string;
|
||
line: number;
|
||
}
|
||
|
||
/** The top-level `scripts` entries of `package.json` text, in document order. */
|
||
export const scriptEntries = (text: string): ScriptEntry[] => objectEntries(text, "scripts");
|
||
|
||
/**
|
||
* The keys of the top-level object `section` of JSON text, in document order
|
||
* (a small JSON scanner: values may contain braces, colons, escapes). Entries
|
||
* parsed so far survive malformed text, so a trailing comma keeps earlier ones visible.
|
||
*/
|
||
export function objectEntries(text: string, section: string): ScriptEntry[] {
|
||
const entries: ScriptEntry[] = [];
|
||
let i = 0;
|
||
let line = 0;
|
||
const BROKEN = new Error("broken");
|
||
|
||
const ws = () => {
|
||
while (i < text.length) {
|
||
const c = text[i]!;
|
||
if (c === "\n") {
|
||
line++;
|
||
i++;
|
||
} else if (c === " " || c === "\t" || c === "\r" || c === "") {
|
||
i++;
|
||
} else {
|
||
break;
|
||
}
|
||
}
|
||
};
|
||
/** The JSON string at `i`, with escapes; advances past it. */
|
||
const str = (): string => {
|
||
if (text[i] !== '"') throw BROKEN;
|
||
i++;
|
||
let out = "";
|
||
for (;;) {
|
||
const c = text[i];
|
||
if (c === undefined) throw BROKEN;
|
||
if (c === '"') {
|
||
i++;
|
||
return out;
|
||
}
|
||
if (c === "\\") {
|
||
const e = text[i + 1];
|
||
if (e === undefined) throw BROKEN;
|
||
if (e === "\n") line++;
|
||
if (e === "u") {
|
||
const hex = text.slice(i + 2, i + 6);
|
||
if (!/^[0-9a-fA-F]{4}$/.test(hex)) throw BROKEN;
|
||
out += String.fromCodePoint(Number.parseInt(hex, 16));
|
||
i += 6;
|
||
} else {
|
||
out += { n: "\n", t: "\t", r: "\r", b: "\b", f: "\f" }[e] ?? e;
|
||
i += 2;
|
||
}
|
||
continue;
|
||
}
|
||
if (c === "\n") line++;
|
||
out += c;
|
||
i++;
|
||
}
|
||
};
|
||
/** Skips one JSON value (objects and arrays recursively), tracking lines. */
|
||
const value = () => {
|
||
ws();
|
||
const c = text[i];
|
||
if (c === '"') {
|
||
str();
|
||
return;
|
||
}
|
||
if (c === "{" || c === "[") {
|
||
const close = c === "{" ? "}" : "]";
|
||
i++;
|
||
ws();
|
||
if (text[i] === close) {
|
||
i++;
|
||
return;
|
||
}
|
||
for (;;) {
|
||
if (c === "{") {
|
||
ws();
|
||
str();
|
||
ws();
|
||
if (text[i] !== ":") throw BROKEN;
|
||
i++;
|
||
}
|
||
value();
|
||
ws();
|
||
if (text[i] === ",") {
|
||
i++;
|
||
continue;
|
||
}
|
||
if (text[i] === close) {
|
||
i++;
|
||
return;
|
||
}
|
||
throw BROKEN;
|
||
}
|
||
}
|
||
const m = /^(?:-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?|true|false|null)/.exec(text.slice(i));
|
||
if (!m) throw BROKEN;
|
||
i += m[0].length;
|
||
};
|
||
try {
|
||
ws();
|
||
if (text[i] !== "{") return entries;
|
||
i++;
|
||
ws();
|
||
if (text[i] !== "}") {
|
||
for (;;) {
|
||
ws();
|
||
const key = str();
|
||
ws();
|
||
if (text[i] !== ":") break;
|
||
i++;
|
||
ws();
|
||
if (key === section && text[i] === "{") {
|
||
i++;
|
||
ws();
|
||
if (text[i] !== "}") {
|
||
for (;;) {
|
||
ws();
|
||
const scriptLine = line;
|
||
const name = str();
|
||
ws();
|
||
if (text[i] !== ":") throw BROKEN;
|
||
i++;
|
||
value();
|
||
entries.push({ name, line: scriptLine });
|
||
ws();
|
||
if (text[i] === ",") {
|
||
i++;
|
||
continue;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
i++; // the closing `}` of the section
|
||
} else {
|
||
value();
|
||
}
|
||
ws();
|
||
if (text[i] === ",") {
|
||
i++;
|
||
continue;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
} catch (e) {
|
||
if (e !== BROKEN) throw e;
|
||
}
|
||
return entries;
|
||
}
|
||
|
||
type PackageJson = { name?: unknown; packageManager?: unknown };
|
||
|
||
function safeParse(text: string): PackageJson | null {
|
||
try {
|
||
return JSON.parse(text) as PackageJson;
|
||
} catch {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/** Same precedence as backend detection for a package: declaration, then lockfile. */
|
||
export function scriptPackageManager(pkg: PackageJson | null, files: readonly string[]): string {
|
||
const pm = typeof pkg?.packageManager === "string" ? pkg.packageManager.split("@")[0] : "";
|
||
if (pm === "pnpm" || pm === "yarn" || pm === "bun" || pm === "npm") return pm;
|
||
if (files.includes("pnpm-lock.yaml")) return "pnpm";
|
||
if (files.includes("yarn.lock")) return "yarn";
|
||
if (files.includes("bun.lock") || files.includes("bun.lockb")) return "bun";
|
||
return "npm";
|
||
}
|
||
|
||
const normCwd = (cwd: string | null) => (cwd ?? "").replace(/^\.\/?/, "").replace(/\/+$/, "");
|
||
|
||
/** Backend ids for a script: the root package's, the member's by its `name` or its directory. */
|
||
export function scriptConfigIds(name: string, pkgName: string | null, dir: string): string[] {
|
||
const ids = new Set([`npm:${name}`]);
|
||
if (pkgName) ids.add(`npm:${pkgName}:${name}`);
|
||
if (dir) ids.add(`npm:${dir}:${name}`);
|
||
return [...ids];
|
||
}
|
||
|
||
/**
|
||
* The project's configuration for the script in the package at `dir` (relative
|
||
* to the project root): the detected id first (a user override keeps it), then
|
||
* `npm run <name>` args in the same working directory.
|
||
*/
|
||
export function scriptConfigFor(
|
||
configs: readonly RunConfig[],
|
||
name: string,
|
||
dir: string,
|
||
pkgName: string | null,
|
||
): RunConfig | undefined {
|
||
const inDir = (c: RunConfig) => normCwd(c.cwd) === dir;
|
||
const ids = new Set(scriptConfigIds(name, pkgName, dir));
|
||
return (
|
||
configs.find((c) => inDir(c) && ids.has(c.id)) ??
|
||
configs.find((c) => inDir(c) && c.kind === "npm" && c.args[0] === "run" && c.args[1] === name)
|
||
);
|
||
}
|
||
|
||
/** What the backend would detect (`detect.rs`): test scripts render results in the test tree. */
|
||
const isTestScript = (name: string) =>
|
||
name === "test" || name === "vitest" || name === "jest" || name.startsWith("test:");
|
||
|
||
export async function scriptMenuItems(entry: ScriptEntry, path: string, content: string): Promise<MenuItem[]> {
|
||
const root = useProject.getState().project?.root ?? null;
|
||
const rel = root && path.startsWith(`${root}/`) ? path.slice(root.length + 1) : path;
|
||
const dir = rel.endsWith("/package.json") ? rel.slice(0, -"/package.json".length) : "";
|
||
try {
|
||
// Detection reads the disk: a script typed moments ago is already there.
|
||
await loadConfigs();
|
||
} catch {
|
||
// Fall back to a temporary configuration below.
|
||
}
|
||
const pkg = safeParse(content.slice(0, 400_000));
|
||
const matched = scriptConfigFor(
|
||
useRun.getState().configs,
|
||
entry.name,
|
||
dir,
|
||
typeof pkg?.name === "string" ? pkg.name : null,
|
||
);
|
||
// Reuse detection for sibling scripts, including a workspace's inherited manager.
|
||
const sibling = useRun
|
||
.getState()
|
||
.configs.find(
|
||
(c) =>
|
||
c.kind === "npm" && c.detected && normCwd(c.cwd) === dir && ["npm", "pnpm", "yarn", "bun"].includes(c.command),
|
||
);
|
||
let pm = matched?.command ?? sibling?.command;
|
||
if (!pm) {
|
||
// Workspace detection uses the root package's manager, even for a member's first script.
|
||
const rootPkg =
|
||
root && dir
|
||
? await call(commands.fsReadText(`${root}/package.json`))
|
||
.then(safeParse)
|
||
.catch(() => null)
|
||
: null;
|
||
const packageDir = rootPkg ? `${root}/` : path.slice(0, -"package.json".length);
|
||
const files = await call(commands.fsReadDir(packageDir)).catch(() => []);
|
||
pm = scriptPackageManager(
|
||
rootPkg ?? pkg,
|
||
files.filter((f) => f.kind === "file" || f.targetKind === "file").map((f) => f.name),
|
||
);
|
||
}
|
||
const label = matched?.name ?? `${pm} run ${entry.name}`;
|
||
const config = matched ?? {
|
||
id: `context:npm:${dir || "root"}:${entry.name}`,
|
||
name: label,
|
||
kind: "npm",
|
||
command: pm,
|
||
args: ["run", entry.name],
|
||
cwd: dir || null,
|
||
env: {},
|
||
detected: true,
|
||
isTest: isTestScript(entry.name),
|
||
debugAdapter: "js-debug",
|
||
};
|
||
return [
|
||
{
|
||
label: tx`Run '${label}'`,
|
||
preserveLabelEnd: true,
|
||
icon: "run",
|
||
shortcut: shortcutLabel("run.runContext"),
|
||
run: () =>
|
||
matched
|
||
? void executeAction("run.runConfig", matched.id)
|
||
: void executeAction("run.runConfig", { config, temporary: true }),
|
||
},
|
||
{
|
||
label: tx`Debug '${label}'`,
|
||
preserveLabelEnd: true,
|
||
icon: "debug",
|
||
shortcut: shortcutLabel("run.debugContext"),
|
||
run: () => void executeAction("run.debugConfig", config),
|
||
},
|
||
];
|
||
}
|
||
|
||
interface Placed extends ScriptEntry {
|
||
pos: number;
|
||
}
|
||
|
||
const setEntries = StateEffect.define<Placed[]>();
|
||
|
||
type EntryMenu = (entry: ScriptEntry, path: string, content: string) => Promise<MenuItem[]>;
|
||
|
||
const entryKey = (entry: ScriptEntry) => JSON.stringify({ ...entry, pos: undefined });
|
||
|
||
class ScriptMarker extends GutterMarker {
|
||
constructor(
|
||
readonly entry: ScriptEntry,
|
||
readonly path: string,
|
||
readonly menu: EntryMenu,
|
||
readonly title: (entry: ScriptEntry) => string,
|
||
) {
|
||
super();
|
||
}
|
||
override eq(other: GutterMarker): boolean {
|
||
// The whole entry, not just its name: a Markdown block keeps its name while its commands change.
|
||
return other instanceof ScriptMarker && entryKey(other.entry) === entryKey(this.entry);
|
||
}
|
||
override toDOM(view: EditorView): Node {
|
||
const el = document.createElement("button");
|
||
el.type = "button";
|
||
el.className = styles.runMarker ?? "";
|
||
el.title = this.title(this.entry);
|
||
el.setAttribute("aria-label", el.title);
|
||
el.innerHTML = gutterIconMarkup("gutter-run") ?? "";
|
||
el.addEventListener("click", () => {
|
||
const rect = el.getBoundingClientRect();
|
||
void this.menu(this.entry, this.path, view.state.doc.toString()).then((items) =>
|
||
showContextMenu({ x: rect.left, y: rect.bottom + 2 }, items),
|
||
);
|
||
});
|
||
return el;
|
||
}
|
||
}
|
||
|
||
const DEBOUNCE = 300;
|
||
|
||
export function npmGutter(ctx: EditorDocContext): Extension {
|
||
return entryGutter(ctx, /(^|\/)package\.json$/, scriptEntries, scriptMenuItems);
|
||
}
|
||
|
||
/**
|
||
* Run ▶ icons on the entries `entries` finds in files matching `file`; a click opens `menu`.
|
||
* Shared by `package.json` scripts and `launchSettings.json` profiles.
|
||
*/
|
||
export function entryGutter(
|
||
ctx: EditorDocContext,
|
||
file: RegExp,
|
||
entries: (text: string) => ScriptEntry[],
|
||
menu: EntryMenu,
|
||
title: (entry: ScriptEntry) => string = (entry) => tx`Run '${entry.name}'`,
|
||
): Extension {
|
||
const path = ctx.path;
|
||
if (ctx.virtual || !file.test(path) || !useProject.getState().project?.root) return [];
|
||
const entriesField = StateField.define<Placed[]>({
|
||
create: () => [],
|
||
update(value, tr) {
|
||
for (const e of tr.effects) if (e.is(setEntries)) return e.value;
|
||
if (!tr.docChanged) return value;
|
||
return value.map((p) => ({ ...p, pos: mapLineStart(tr, p.pos) }));
|
||
},
|
||
});
|
||
const detector = ViewPlugin.fromClass(
|
||
class {
|
||
timer: ReturnType<typeof setTimeout> | undefined;
|
||
constructor(readonly view: EditorView) {
|
||
this.schedule(0);
|
||
}
|
||
update(u: ViewUpdate) {
|
||
if (u.docChanged) this.schedule(DEBOUNCE);
|
||
}
|
||
schedule(delay: number) {
|
||
clearTimeout(this.timer);
|
||
this.timer = setTimeout(() => {
|
||
const view = this.view;
|
||
const found = entries(view.state.doc.toString())
|
||
.filter((e) => e.line < view.state.doc.lines)
|
||
.map((e) => ({ ...e, pos: view.state.doc.line(e.line + 1).from }));
|
||
view.dispatch({ effects: setEntries.of(found) });
|
||
}, delay);
|
||
}
|
||
destroy() {
|
||
clearTimeout(this.timer);
|
||
}
|
||
},
|
||
);
|
||
return [
|
||
entriesField,
|
||
detector,
|
||
// In the shared icon area with the other run icons; hidden with them.
|
||
iconAreaSource.of({
|
||
order: 10,
|
||
markers: (view) =>
|
||
runIconsShown()
|
||
? RangeSet.of(
|
||
view.state.field(entriesField).map((e) => new ScriptMarker(e, path, menu, title).range(e.pos)),
|
||
true,
|
||
)
|
||
: RangeSet.empty,
|
||
reserve: (state) => runIconsShown() && state.field(entriesField).length > 0,
|
||
// The marker's own click opens the menu; keep the press from moving the caret.
|
||
mousedown: (_view, _block, event) => {
|
||
if (event.button !== 0 || !(event.target as HTMLElement).closest?.(`.${styles.runMarker}`)) return false;
|
||
event.preventDefault();
|
||
return true;
|
||
},
|
||
}),
|
||
];
|
||
}
|