From 24ef4143e6cbae0820fa48b4cb8b85f8196ddd4b Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Tue, 24 Nov 2026 20:50:23 +0800 Subject: [PATCH] fix(markdown): run a code block's current commands after it is edited (#5634) Gutter markers compared only the entry name (block:), 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. --- src/features/run/markdownCommands.test.ts | 51 ++++++++++++++++++++++- src/features/run/npmGutter.ts | 5 ++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/features/run/markdownCommands.test.ts b/src/features/run/markdownCommands.test.ts index d2d2ec33f..c72ec84af 100644 --- a/src/features/run/markdownCommands.test.ts +++ b/src/features/run/markdownCommands.test.ts @@ -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('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(); + } + }); +}); diff --git a/src/features/run/npmGutter.ts b/src/features/run/npmGutter.ts index 418b7efe1..3209ef4e8 100644 --- a/src/features/run/npmGutter.ts +++ b/src/features/run/npmGutter.ts @@ -309,6 +309,8 @@ const setEntries = StateEffect.define(); type EntryMenu = (entry: ScriptEntry, path: string, content: string) => Promise; +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");