fix(markdown): run a code block's current commands after it is edited (#5634)

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.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-24 20:50:23 +08:00
1 джерело 30e72ffa57
коміт 24ef4143e6
2 files changed
+53 -3

No files matched your search

+49 -2
Переглянути файл
@@ -1,6 +1,16 @@
import { afterEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { EditorView } from "@codemirror/view";
import { iconAreaExtension } from "@/editor/core/cm/iconArea";
import * as actions from "@/platform/actions";
import { useProject } from "@/platform/project";
import { commandMenuItems, markdownCommands, trimPrompt, type CommandEntry } from "./markdownCommands";
import * as ui from "@/ui";
import {
commandMenuItems,
markdownCommandGutter,
markdownCommands,
trimPrompt,
type CommandEntry,
} from "./markdownCommands";
const accepts = (line: string) => /^(mvn|\.\/gradlew) /.test(line);
@@ -68,3 +78,40 @@ describe("Markdown command menu", () => {
]);
});
});
describe("Markdown command gutter", () => {
afterEach(() => {
vi.restoreAllMocks();
useProject.setState({ project: null, settings: null });
});
it("runs a block's current commands after it is edited", async () => {
useProject.setState({
project: { root: "/p", name: "p", settings: {}, trusted: true, isGit: false },
settings: {} as never,
});
const show = vi.spyOn(ui, "showContextMenu").mockImplementation(() => {});
const run = vi.spyOn(actions, "executeAction").mockResolvedValue(undefined as never);
const view = new EditorView({
doc: "```sh\nls\n```",
parent: document.body,
extensions: [
iconAreaExtension,
markdownCommandGutter({ doc: 1, path: "/p/README.md", language: "markdown", uiMode: null, readOnly: false }),
],
});
const button = () => view.dom.querySelector<HTMLButtonElement>('button[aria-label="Run in Terminal"]');
try {
await vi.waitFor(() => expect(button()).not.toBeNull());
view.dispatch({ changes: { from: 6, to: 8, insert: "pwd" } });
await new Promise((r) => setTimeout(r, 400));
button()!.click();
await vi.waitFor(() => expect(show).toHaveBeenCalled());
const [item] = show.mock.calls[0]![1];
(item as { run: () => void }).run();
expect(run).toHaveBeenCalledWith("terminal.runLines", { lines: ["pwd"], cwd: "/p" });
} finally {
view.destroy();
}
});
});
+4 -1
Переглянути файл
@@ -309,6 +309,8 @@ 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,
@@ -319,7 +321,8 @@ class ScriptMarker extends GutterMarker {
super();
}
override eq(other: GutterMarker): boolean {
return other instanceof ScriptMarker && other.entry.name === this.entry.name;
// 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");