The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.
TypeScript code-editorcross-platformdapdatabase-clientdebuggerdesktop-appdeveloper-toolsgithttp-clientideintellijlsprusttauriterminal
feat(editor): expose existing Git context menu actions (#2404)
l0ng-ai22a56bfc36d8
diff --git a/e2e/editorGitMenu.spec.ts b/e2e/editorGitMenu.spec.tsnew file mode 100644index 000000000..dfb2d3601--- /dev/null+++ b/e2e/editorGitMenu.spec.ts@@ -0,0 +1,64 @@+import { expect, openFromTree, test } from "./fixtures";++test("editor Git submenu offers the registered file and repository actions in groups", async ({ workbench: page }) => {+ const editor = await openFromTree(page, "README.md");+ await editor.locator(".cm-content").click({ button: "right" });+ await page.getByRole("menuitem", { name: "Git", exact: true }).hover();+ const git = page.getByRole("menu").filter({ has: page.getByRole("menuitem", { name: "Clone…", exact: true }) });+ await expect(git.locator('[role="menuitem"], [role="menuitemcheckbox"]')).toHaveText([+ /Commit File…/,+ /Add/,+ /Annotate with Git Blame/,+ /Show Diff/,+ /Compare with Branch or Revision…/,+ /Show History$/,+ /Show History for Selection/,+ /Rollback…/,+ /Push…/,+ /Pull…/,+ /Fetch/,+ /Merge…/,+ /Rebase…/,+ /Branches…/,+ /New Branch…/,+ /New Tag…/,+ /Reset HEAD…/,+ /Stash Changes…/,+ /Unstash Changes…/,+ /GitHub/,+ /Manage Remotes…/,+ /Clone…/,+ ]);+ await expect(git.getByRole("separator")).toHaveCount(6);+ await git.getByRole("menuitem", { name: "GitHub", exact: true }).hover();+ for (const name of ["View Pull Requests", "Create Pull Request…", "Checkout Pull Request…", "Open on GitHub"]) {+ await expect(page.getByRole("menuitem", { name: new RegExp(`^${name}`) })).toBeEnabled();+ }+});++test("editor Git submenu keeps repository actions disabled without Git", async ({ workbench: page }) => {+ const editor = await openFromTree(page, "README.md");+ await page.evaluate(async () => {+ const projectModule = "/src/platform/project.ts";+ const contextModule = "/src/platform/context.ts";+ const { useProject } = await import(/* @vite-ignore */ projectModule);+ const { setContext } = await import(/* @vite-ignore */ contextModule);+ useProject.setState((s: { project: object }) => ({ project: { ...s.project, isGit: false } }));+ setContext("vcsAvailable", false);+ });+ await editor.locator(".cm-content").click({ button: "right" });+ await page.getByRole("menuitem", { name: "Git", exact: true }).hover();+ for (const name of [+ "Merge…",+ "Rebase…",+ "New Branch…",+ "New Tag…",+ "Reset HEAD…",+ "Stash Changes…",+ "Unstash Changes…",+ "Manage Remotes…",+ ]) {+ await expect(page.getByRole("menuitem", { name: new RegExp(`^${name}`) })).toBeDisabled();+ }+ await expect(page.getByRole("menuitem", { name: "Clone…", exact: true })).toBeEnabled();+});diff --git a/src/editor/core/controller.test.ts b/src/editor/core/controller.test.tsindex 97fa23f42..28ad80b52 100644--- a/src/editor/core/controller.test.ts+++ b/src/editor/core/controller.test.ts@@ -8,8 +8,8 @@ import { registerTextEditorWrapper, setProjectViewLead, useProjectViewLead } froimport { registerActions } from "@/platform/actions";import { useProject } from "@/platform/project";import { useRemote } from "@/platform/remote";-import { resolveMenuItems } from "@/ui";-import { getContext } from "@/platform/context";+import { hideContextMenu, resolveMenuItems, useContextMenuStore } from "@/ui";+import { getContext, setContext } from "@/platform/context";import { installKeyboardDispatcher } from "@/platform/keymap";import { TEXT_WHEN } from "./actions";@@ -352,6 +352,98 @@ describe("EditorController", () => {useProject.setState({ project: null, settings: null });});+ it.each(["editor", "tab"])(+ "offers grouped Git actions in the %s menu and preserves action enablement",+ async (menu) => {+ const groups = [+ ["vcs.commit", "vcs.addToVcs"],+ [+ "vcs.annotate",+ "vcs.showDiff",+ "vcs.compareWithBranch",+ "vcs.showHistory",+ "vcs.showHistoryForSelection",+ "vcs.rollback",+ ],+ ["vcs.push", "vcs.update", "vcs.fetch"],+ ["vcs.merge", "vcs.rebase"],+ ["vcs.branches", "vcs.newBranch", "vcs.newTag", "vcs.resetHead"],+ ["vcs.stash", "vcs.unstash"],+ ["GitHub", "vcs.manageRemotes", "vcs.clone"],+ ];+ const github = [+ "github.pullRequests",+ "github.createPullRequest",+ "github.checkoutPullRequest",+ "github.openInBrowser",+ ];+ const ids = [...groups.flat().filter((id) => id !== "GitHub"), ...github];+ const dispose = registerActions(+ ids.map((id) => ({+ id,+ title: id,+ when: id === "vcs.clone" ? undefined : id.startsWith("github.") ? "projectOpen" : "vcsAvailable",+ run: () => undefined,+ })),+ );+ useProject.setState({ project: { name: "p", root: "/p", trusted: true, isGit: true, settings: {} } as never });+ ctrl = new EditorController(createMemoryBackend({ "/p/a.ts": "one\n" }));+ ctrl.start();+ await ctrl.openFile("/p/a.ts", { focus: false });+ const slot = ctrl.activeSlot!;+ const host = document.createElement("div");+ document.body.appendChild(host);+ const view = slot.mount(host);+ vi.spyOn(view, "posAtCoords").mockReturnValue(null);+ const items = () => {+ if (menu === "editor") {+ ctrl!.onContextMenu(slot, new MouseEvent("contextmenu"));+ return useContextMenuStore.getState().menu!.items;+ }+ const state = useTabs.getState();+ const pane = state.panes[state.activePane]!;+ return ctrl!.tabMenu(pane.id, pane.tabs[0]!);+ };+ const gitItems = () => resolveMenuItems(items()).find((item) => item.label === "Git")!.submenu!;+ try {+ setContext("projectOpen", true);+ setContext("vcsAvailable", true);+ expect(+ gitItems().map((item) =>+ item.kind === "separator" ? "-" : item.kind === "action" ? item.action : item.label,+ ),+ ).toEqual(groups.flatMap((group, i) => (i ? ["-", ...group] : group)));+ const resolved = resolveMenuItems(gitItems());+ expect(resolved.filter((item) => item.kind === "item").every((item) => !item.disabled)).toBe(true);+ const githubItems = resolved.find((item) => item.label === "GitHub")!.submenu!;+ expect(resolveMenuItems(githubItems).map((item) => item.key)).toEqual(github);+ expect(gitItems()).toContainEqual({+ kind: "action",+ action: "vcs.compareWithBranch",+ arg: { path: "/p/a.ts" },+ });++ setContext("vcsAvailable", false);+ expect(+ resolveMenuItems(gitItems())+ .filter((item) => item.kind === "item" && item.key.startsWith("vcs.") && item.key !== "vcs.clone")+ .every((item) => item.disabled),+ ).toBe(true);+ expect(resolveMenuItems(gitItems()).find((item) => item.key === "vcs.clone")?.disabled).toBe(false);+ setContext("projectOpen", false);+ expect(resolveMenuItems(githubItems).every((item) => item.disabled)).toBe(true);+ } finally {+ dispose();+ hideContextMenu();+ slot.unmount();+ host.remove();+ useProject.setState({ project: null, settings: null });+ setContext("vcsAvailable", false);+ setContext("projectOpen", false);+ }+ },+ );+it("switches an open image tab to a text tab when a matching text editor wrapper registers", async () => {ctrl = new EditorController(createMemoryBackend({ "/p/arch.diagram.svg": "<svg/>" }));ctrl.start();diff --git a/src/editor/core/controller.ts b/src/editor/core/controller.tsindex 42912925c..4b685db22 100644--- a/src/editor/core/controller.ts+++ b/src/editor/core/controller.ts@@ -1978,7 +1978,7 @@ function gitMenu(path: string): MenuItem {{ kind: "separator" },{ kind: "action", action: "vcs.annotate", arg: path, label: "Annotate with Git Blame" },{ kind: "action", action: "vcs.showDiff", arg: [path], label: "Show Diff" },- { kind: "action", action: "vcs.compareWithBranch", arg: { path }, label: "Compare with Branch…" },+ { kind: "action", action: "vcs.compareWithBranch", arg: { path } },{ kind: "action", action: "vcs.showHistory", arg: [path], label: "Show History" },{ kind: "action", action: "vcs.showHistoryForSelection", label: "Show History for Selection" },{ kind: "action", action: "vcs.rollback", arg: [path], label: "Rollback…" },@@ -1987,7 +1987,29 @@ function gitMenu(path: string): MenuItem {{ kind: "action", action: "vcs.update", label: "Pull…" },{ kind: "action", action: "vcs.fetch", label: "Fetch" },{ kind: "separator" },+ { kind: "action", action: "vcs.merge" },+ { kind: "action", action: "vcs.rebase" },+ { kind: "separator" },{ kind: "action", action: "vcs.branches", label: "Branches…" },+ { kind: "action", action: "vcs.newBranch" },+ { kind: "action", action: "vcs.newTag" },+ { kind: "action", action: "vcs.resetHead" },+ { kind: "separator" },+ { kind: "action", action: "vcs.stash" },+ { kind: "action", action: "vcs.unstash" },+ { kind: "separator" },+ {+ label: "GitHub",+ icon: "github",+ submenu: [+ { kind: "action", action: "github.pullRequests" },+ { kind: "action", action: "github.createPullRequest" },+ { kind: "action", action: "github.checkoutPullRequest" },+ { kind: "action", action: "github.openInBrowser" },+ ],+ },+ { kind: "action", action: "vcs.manageRemotes" },+ { kind: "action", action: "vcs.clone", label: "Clone…" },],};}