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");