diff --git a/e2e/projectTree.spec.ts b/e2e/projectTree.spec.ts index 288196e21..e47cd1654 100644 --- a/e2e/projectTree.spec.ts +++ b/e2e/projectTree.spec.ts @@ -37,6 +37,21 @@ test("type-ahead jumps to a matching node", async ({ workbench: page }) => { await expect(tree.locator('[role="treeitem"][aria-selected="true"]')).toHaveText("README.md"); }); +test("Rename targets a file or folder directly from its context menu", async ({ workbench: page }) => { + await page.setViewportSize({ width: 720, height: 480 }); + const menu = page.getByRole("menu", { name: "Context menu" }); + const dialog = page.getByRole("dialog", { name: "Rename", exact: true }); + for (const name of ["README.md", "src"]) { + const row = page.getByRole("treeitem", { name, exact: true }); + await row.click({ button: "right" }); + await menu.getByRole("menuitem", { name: /^Rename…/ }).click(); + await expect(dialog.getByRole("textbox", { name: "New name" })).toHaveValue(name); + await dialog.getByRole("button", { name: "Cancel", exact: true }).click(); + await expect(dialog).toHaveCount(0); + await expect(row).toBeVisible(); + } +}); + test("the file deletion form keeps its option and buttons visible in a narrow window", async ({ workbench: page }) => { const file = page.getByRole("treeitem", { name: "README.md", exact: true }); await file.click({ button: "right" }); diff --git a/src/features/project-tree/contextMenu.test.ts b/src/features/project-tree/contextMenu.test.ts index 90e5670f2..feb77e9ad 100644 --- a/src/features/project-tree/contextMenu.test.ts +++ b/src/features/project-tree/contextMenu.test.ts @@ -134,7 +134,7 @@ describe("project view context menu", () => { ]); }); - it("follows IDEA's order for a file", () => { + it("groups clipboard, search, file changes, formatting and history actions for a file", () => { const items = contextMenuItems([node(`${ROOT}/src/a.py`)]); expect(labels(items)).toEqual([ "New", @@ -143,17 +143,18 @@ describe("project view context menu", () => { "Copy", "Copy Path/Reference…", "Paste", - "-", "Jump to Source", + "-", "Find Usages", "Find in Files…", "Replace in Files…", "-", + "Rename…", "Refactor", + "Delete…", "-", "Reformat Code", "Optimize Imports", - "Delete…", "-", "Run 'a'", "-", @@ -161,11 +162,11 @@ describe("project view context menu", () => { "Open In", "-", "Local History", + "Git", "Reload from Disk", "-", - "Git", - "-", "Compare With…", + "-", "File Color", ]); expect(sub(items, "Refactor")).toEqual(["Rename…", "Move File…", "Copy File…", "-", "Safe Delete…"]); @@ -270,7 +271,8 @@ describe("project view context menu", () => { it("shows Mark Directory as for folders, without file-only items", () => { const l = labels(contextMenuItems([node(`${ROOT}/src`, true)])); - expect(l).toContain("Mark Directory as"); + expect(l.slice(l.indexOf("Rename…"), l.indexOf("Rename…") + 4)).toEqual(["Rename…", "Refactor", "Delete…", "-"]); + expect(l.slice(-4)).toEqual(["Compare With…", "-", "Mark Directory as", "File Color"]); expect(sub(contextMenuItems([node(`${ROOT}/src`, true)]), "Refactor")).toContain("Move Directory…"); for (const fileOnly of ["Find Usages", "Optimize Imports", "Run 'a'", "Open in Right Split"]) expect(l).not.toContain(fileOnly); @@ -322,6 +324,7 @@ describe("project view context menu", () => { it("keeps the project root menu short and disables what cannot apply", () => { const items = contextMenuItems([node(ROOT, true, true)]); const l = labels(items); + expect(l).not.toContain("Rename…"); expect(l).not.toContain("Refactor"); expect(l).not.toContain("Mark Directory as"); const resolved = resolveMenuItems(items); @@ -339,6 +342,7 @@ describe("project view context menu", () => { it("offers Compare Files for two files", () => { const l = labels(contextMenuItems([node(`${ROOT}/a.py`), node(`${ROOT}/b.py`)])); + expect(l).not.toContain("Rename…"); expect(l).toContain("Compare Files"); expect(l).not.toContain("Compare With…"); }); diff --git a/src/features/project-tree/contextMenu.ts b/src/features/project-tree/contextMenu.ts index 37f2caf92..14a194564 100644 --- a/src/features/project-tree/contextMenu.ts +++ b/src/features/project-tree/contextMenu.ts @@ -160,12 +160,7 @@ function submenu(id: string, label: string, entries: Entry[], icon?: string): Me return { id, label, icon, submenu: items }; } -/** - * IDEA's ProjectViewPopupMenu: New / clipboard / find / Refactor / edit / - * Run group / Open In / Local History / Git / Compare / Mark Directory as. - * Items that cannot apply to the selection are hidden (or disabled, like IDEA's - * clipboard group), so the project root and Scratches get a shorter menu. - */ +/** Project view actions available for the selected files and directories. */ export function contextMenuItems(nodes: ProjectNode[]): MenuItem[] { if (nodes.some((n) => n.readOnly)) return libraryMenuItems(nodes.filter((n) => !n.group)); const lead = nodes[0] ?? null; @@ -205,7 +200,7 @@ export function contextMenuItems(nodes: ProjectNode[]): MenuItem[] { { kind: "separator" }, ); - // Cut / Copy / Copy Path/Reference… / Paste + // Clipboard and opening files. push( { id: "cut", @@ -247,11 +242,6 @@ export function contextMenuItems(nodes: ProjectNode[]): MenuItem[] { disabled: !canPaste(), run: () => void paste(folder), }, - { kind: "separator" }, - ); - - // Jump to Source / Find Usages / Find in Files… / Replace in Files… - push( files.length > 0 && { id: "jumpToSource", label: "Jump to Source", @@ -259,6 +249,10 @@ export function contextMenuItems(nodes: ProjectNode[]): MenuItem[] { shortcut: keyLabel("project.open"), run: () => files.forEach((f) => void openFile(f.path)), }, + { kind: "separator" }, + ); + + push( inWorkspace && singleFile && { id: "findUsages", @@ -271,16 +265,17 @@ export function contextMenuItems(nodes: ProjectNode[]): MenuItem[] { { kind: "separator" }, ); - // Refactor › + const rename: Entry = single && + !single.root && { + id: "rename", + label: "Rename…", + shortcut: keyLabel("project.rename"), + run: () => void openRenameDialog(single), + }; push( + rename, submenu("refactor", "Refactor", [ - single && - !single.root && { - id: "rename", - label: "Rename…", - shortcut: keyLabel("project.rename"), - run: () => void openRenameDialog(single), - }, + rename, nonRoot.length > 0 && { id: "move", label: nonRoot.length === 1 ? (nonRoot[0]!.dir ? "Move Directory…" : "Move File…") : "Move…", @@ -301,10 +296,16 @@ export function contextMenuItems(nodes: ProjectNode[]): MenuItem[] { run: () => void deleteSelection(nodes), }, ]), + { + id: "delete", + label: "Delete…", + shortcut: keyLabel("project.delete"), + disabled: !nonRoot.length, + run: () => void deleteSelection(nodes), + }, { kind: "separator" }, ); - // Reformat Code (one file at once, directories or several files through its dialog) / Optimize Imports / Delete… push( nodes.some((n) => !n.group) && getAction(singleFile ? "code.reformatFile" : "code.reformatFiles") && { @@ -320,13 +321,6 @@ export function contextMenuItems(nodes: ProjectNode[]): MenuItem[] { shortcut: keyLabel("project.optimizeImports"), run: () => void delegate("code.optimizeImportsFile", { path: singleFile.path }), }, - { - id: "delete", - label: "Delete…", - shortcut: keyLabel("project.delete"), - disabled: !nonRoot.length, - run: () => void deleteSelection(nodes), - }, { kind: "separator" }, ); @@ -367,21 +361,12 @@ export function contextMenuItems(nodes: ProjectNode[]): MenuItem[] { { kind: "separator" }, ); - // Local History › / Reload from Disk push( single && submenu("history", "Local History", [ delegated("vcs.localHistory", single.path, "Show History"), delegated("localHistory.putLabel", undefined, "Put Label…"), ]), - { - id: "reload", - label: "Reload from Disk", - icon: "refresh", - shortcut: keyLabel("project.reloadFromDisk"), - run: () => void reloadFromDisk(nodes), - }, - { kind: "separator" }, ); // Git › (`vcsAvailable` also covers repositories nested below a non-Git project root) @@ -432,10 +417,20 @@ export function contextMenuItems(nodes: ProjectNode[]): MenuItem[] { ], "branch", ), - { kind: "separator" }, ); } + push( + { + id: "reload", + label: "Reload from Disk", + icon: "refresh", + shortcut: keyLabel("project.reloadFromDisk"), + run: () => void reloadFromDisk(nodes), + }, + { kind: "separator" }, + ); + const compare = compareTarget(nodes); if (compare) push( @@ -445,6 +440,8 @@ export function contextMenuItems(nodes: ProjectNode[]): MenuItem[] { }), ); + push({ kind: "separator" }); + // An attached project keeps its own excluded folders; its other roles aren't applied yet. const attached = !inProject && single?.dir ? attachedProjectOf(single.path) : null; if ((inProject || attached) && single?.dir && !single.root && !single.placeholder) {