diff --git a/docs/en/guide/editor.mdx b/docs/en/guide/editor.mdx index ca50156fb1..14ab8b75db 100644 --- a/docs/en/guide/editor.mdx +++ b/docs/en/guide/editor.mdx @@ -111,6 +111,8 @@ To format on every save, enable **Reformat code** (and optionally **Optimize imp **Window › Editor Tabs** also has **Pin Tab**, **Close Other Tabs**, **Close All Tabs** and **Reopen Closed Tab**, all without default shortcuts. **Window › Move Tab to New Window** detaches the current tab into its own window. +The tab context menu's **Close Unmodified Tabs**, **Close All but Pinned**, **Close Tabs to the Left**, **Close Tabs to the Right** and **Close All Read-Only Tabs** are also actions on the current tab and its group: run them from **Find Action** or assign a shortcut in **Settings › Keymap**. Pinned tabs stay open. + Tab placement, the tab limit and what happens when it is reached, the preview tab, sorting, and which tab is activated after one closes are set in **Settings › Editor › General › Editor Tabs**. With tabs placed left or right, the tab column is as wide as its longest name, up to a limit. Drag the column's inner edge (or focus it and press `←` / `→`) to change the limit; it applies to every editor group and is remembered. diff --git a/docs/zh/guide/editor.mdx b/docs/zh/guide/editor.mdx index 6cdf6c164e..6f8e05055b 100644 --- a/docs/zh/guide/editor.mdx +++ b/docs/zh/guide/editor.mdx @@ -111,6 +111,8 @@ description: "多光标与选区、行编辑、剪贴板历史、代码折叠、 **窗口 › 编辑器标签页** 中还有 **固定标签页**、**关闭其他标签页**、**关闭所有标签页** 和 **重新打开已关闭的标签页**,默认都没有快捷键。在查找操作中运行 **将标签页移到新窗口**,可以把当前标签页拆到一个独立窗口。 +标签页右键菜单中的 **关闭未修改的标签页**、**关闭除已固定外的所有标签页**、**关闭左侧标签页**、**关闭右侧标签页** 和 **关闭所有只读标签页** 也是作用于当前标签页及其所在组的操作:可在查找操作中运行,或在 **设置 › 按键映射** 中分配快捷键。已固定的标签页会保留。 + 标签页的位置、数量上限以及达到上限后的处理方式、预览标签页、排序方式,以及关闭标签页后激活哪一个,都在 **设置 › 编辑器 › 常规 › 编辑器标签页** 中设置。 标签页放在左侧或右侧时,标签页列的宽度随最长的名称变化,但不超过一个上限。拖动该列靠近编辑器的边缘(或聚焦后按 `←` / `→`)可以调整上限;调整对所有编辑器组生效,并会被记住。 diff --git a/src/editor/core/actions.ts b/src/editor/core/actions.ts index 1867f0b8ac..81cb9581f3 100644 --- a/src/editor/core/actions.ts +++ b/src/editor/core/actions.ts @@ -37,6 +37,7 @@ import { type SimpleRange, } from "./model/selectionStack"; import type { EditorSlot } from "./model/slot"; +import type { Tab } from "./model/tabs"; import { findAgain, findPanelTarget, @@ -791,6 +792,32 @@ export function registerEditorActions(): () => void { const pane = s.panes[s.activePane]; return pane?.active ? { paneId: pane.id, tabId: pane.active } : null; }; + /** + * A tab menu close item as a Window action on the active tab of the active group; `canClose` + * mirrors when the menu disables or hides the item. + */ + const closeTabs = ( + id: string, + title: string, + close: (paneId: string, tabId: string) => Promise, + canClose: (tabs: Tab[], index: number) => boolean = () => true, + ): Action => ({ + id, + title, + category: "Window", + when: TABS_WHEN, + enabled: () => { + const s = useTabs.getState(); + const pane = s.panes[s.activePane]; + const tabs = pane?.tabs ?? []; + const index = tabs.findIndex((t) => t.id === pane?.active); + return index >= 0 && canClose(tabs, index); + }, + run: async () => { + const t = activePaneTab(); + if (t) await close(t.paneId, t.tabId); + }, + }); const occurrence = (id: string, title: string, run: (view: EditorView) => unknown): Action => ({ id, title, @@ -1263,6 +1290,32 @@ export function registerEditorActions(): () => void { await ctrl.closeEverything(); }, }, + closeTabs("editor.closeUnmodifiedTabs", "Close Unmodified Tabs", (pane) => ctrl.closeUnmodified(pane)), + // Without a pinned tab this would be Close All; IDEA disables it, the tab menu hides it. + closeTabs( + "editor.closeAllButPinned", + "Close All but Pinned", + (pane) => ctrl.closeAll(pane, true), + (tabs) => tabs.some((t) => t.pinned), + ), + closeTabs( + "editor.closeTabsToLeft", + "Close Tabs to the Left", + (pane, tab) => ctrl.closeToLeft(pane, tab), + (_tabs, index) => index > 0, + ), + closeTabs( + "editor.closeTabsToRight", + "Close Tabs to the Right", + (pane, tab) => ctrl.closeToRight(pane, tab), + (tabs, index) => index < tabs.length - 1, + ), + closeTabs( + "editor.closeReadOnlyTabs", + "Close All Read-Only Tabs", + (pane) => ctrl.closeReadOnly(pane), + (tabs) => tabs.some((t) => !t.pinned && !!t.input.path && useDocMeta.getState()[t.input.path]?.readOnly), + ), { id: "editor.pinTab", title: "Pin / Unpin Tab", diff --git a/src/editor/core/tabCloseActions.test.ts b/src/editor/core/tabCloseActions.test.ts new file mode 100644 index 0000000000..c51c2643c1 --- /dev/null +++ b/src/editor/core/tabCloseActions.test.ts @@ -0,0 +1,117 @@ +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; +import { executeAction, getAction } from "@/platform/actions"; +import { setFileDecorations } from "@/workbench/contributions"; +import { resolveActionId } from "@/features/vim/rc"; +import { registerEditorActions } from "./actions"; +import { editorController, useTabs } from "./controller"; +import { useDocMeta } from "./model/document"; +import { initialTabsState } from "./model/tabs"; + +const ctrl = editorController(); +let dispose: () => void; +beforeAll(() => { + dispose = registerEditorActions(); + ctrl.start(); +}); +afterAll(() => { + ctrl.stop(); + dispose(); +}); + +const pane = () => useTabs.getState().panes[useTabs.getState().activePane]!; +const names = () => pane().tabs.map((t) => t.input.path?.slice("/tabs/".length)); +const tabOf = (name: string) => pane().tabs.find((t) => t.input.path === `/tabs/${name}`)!; +const activate = (name: string) => ctrl.activate(pane().id, tabOf(name).id, false); +const pin = (name: string) => ctrl.togglePin(pane().id, tabOf(name).id); + +beforeEach(async () => { + // oxlint-disable-next-line no-await-in-loop -- the tab order is what these tests assert + for (const name of ["a", "b", "c", "d", "e"]) await ctrl.openFile(`/tabs/${name}`, { focus: false }); +}); +afterEach(async () => { + await ctrl.closeEverything(); + useTabs.setState(initialTabsState(), true); + setFileDecorations("vcs", new Map()); + useDocMeta.setState({}, true); +}); + +describe("Window › Editor Tabs close actions", () => { + it.each([ + ["editor.closeTabsToLeft", "Close Tabs to the Left", "CloseAllToTheLeft"], + ["editor.closeTabsToRight", "Close Tabs to the Right", "CloseAllToTheRight"], + ["editor.closeUnmodifiedTabs", "Close Unmodified Tabs", "CloseAllUnmodifiedEditors"], + ["editor.closeAllButPinned", "Close All but Pinned", "CloseAllUnpinnedEditors"], + ["editor.closeReadOnlyTabs", "Close All Read-Only Tabs", "CloseAllReadonly"], + ])("%s is %s in Window, mapped from IDEA's %s", (id, title, ideaId) => { + expect(getAction(id)).toMatchObject({ title, category: "Window" }); + expect(resolveActionId(ideaId)).toBe(id); + }); + + it("closes the tabs left of the active one, keeping pinned tabs", async () => { + pin("a"); + activate("c"); + expect(await executeAction("editor.closeTabsToLeft")).toBe(true); + expect(names()).toEqual(["a", "c", "d", "e"]); + expect(pane().active).toBe(tabOf("c").id); + }); + + it("closes the tabs right of the active one, keeping pinned tabs", async () => { + activate("b"); + pin("e"); + // Pinned tabs move to the front: e, a, b, c, d. + expect(await executeAction("editor.closeTabsToRight")).toBe(true); + expect(names()).toEqual(["e", "a", "b"]); + }); + + it("is unavailable at the edges, as the tab menu items are disabled", async () => { + activate("a"); + expect(await executeAction("editor.closeTabsToLeft")).toBe(false); + activate("e"); + expect(await executeAction("editor.closeTabsToRight")).toBe(false); + expect(names()).toEqual(["a", "b", "c", "d", "e"]); + }); + + it("closes unmodified tabs, keeping VCS-changed and pinned ones", async () => { + setFileDecorations("vcs", new Map([["/tabs/b", { color: "modified" }]])); + pin("d"); + expect(await executeAction("editor.closeUnmodifiedTabs")).toBe(true); + expect(names()).toEqual(["d", "b"]); + }); + + it("closes every tab but the pinned ones", async () => { + pin("c"); + expect(await executeAction("editor.closeAllButPinned")).toBe(true); + expect(names()).toEqual(["c"]); + }); + + it("is unavailable without a pinned tab instead of closing everything", async () => { + expect(await executeAction("editor.closeAllButPinned")).toBe(false); + expect(names()).toEqual(["a", "b", "c", "d", "e"]); + }); + + it("is unavailable without an unpinned read-only tab", async () => { + expect(await executeAction("editor.closeReadOnlyTabs")).toBe(false); + useDocMeta.setState({ "/tabs/b": { readOnly: true } } as never); + pin("b"); + expect(await executeAction("editor.closeReadOnlyTabs")).toBe(false); + expect(names()).toEqual(["b", "a", "c", "d", "e"]); + }); + + it("closes read-only tabs, keeping pinned ones", async () => { + useDocMeta.setState({ "/tabs/b": { readOnly: true }, "/tabs/c": { readOnly: true } } as never); + pin("c"); + expect(await executeAction("editor.closeReadOnlyTabs")).toBe(true); + expect(names()).toEqual(["c", "a", "d", "e"]); + }); + + it("acts on the active split only", async () => { + const left = pane().id; + ctrl.split(left, "row"); + await ctrl.openFile("/tabs/x", { focus: false }); + expect(useTabs.getState().activePane).not.toBe(left); + expect(names()).toEqual(["e", "x"]); + await executeAction("editor.closeTabsToLeft"); + expect(names()).toEqual(["x"]); + expect(useTabs.getState().panes[left]!.tabs).toHaveLength(5); + }); +}); diff --git a/src/platform/ideaActions.ts b/src/platform/ideaActions.ts index 6386e8d8d6..efe6d0745b 100644 --- a/src/platform/ideaActions.ts +++ b/src/platform/ideaActions.ts @@ -91,6 +91,11 @@ export const IDEA_ACTIONS: Readonly> = { CloseContent: "editor.closeTab", CloseEditor: "editor.closeTab", CloseAllEditors: "editor.closeAllTabs", + CloseAllUnmodifiedEditors: "editor.closeUnmodifiedTabs", + CloseAllUnpinnedEditors: "editor.closeAllButPinned", + CloseAllToTheLeft: "editor.closeTabsToLeft", + CloseAllToTheRight: "editor.closeTabsToRight", + CloseAllReadonly: "editor.closeReadOnlyTabs", ReopenClosedTab: "editor.reopenClosedTab", NextTab: "editor.nextTab", PreviousTab: "editor.prevTab", diff --git a/src/platform/keymaps/intellij-macos.ts b/src/platform/keymaps/intellij-macos.ts index 1e0db09748..46126380d3 100644 --- a/src/platform/keymaps/intellij-macos.ts +++ b/src/platform/keymaps/intellij-macos.ts @@ -197,6 +197,11 @@ export const intellijMacos: Record = { "editor.closeTab": ["Meta+W"], "toolwindow.closeContent": ["Meta+W", "Ctrl+Shift+F4"], "editor.reopenClosedTab": [], + "editor.closeUnmodifiedTabs": [], + "editor.closeAllButPinned": [], + "editor.closeTabsToLeft": [], + "editor.closeTabsToRight": [], + "editor.closeReadOnlyTabs": [], "file.toggleReadOnly": [], "editor.nextTab": ["Ctrl+Right", "Meta+Shift+]"], "editor.prevTab": ["Ctrl+Left", "Meta+Shift+["], diff --git a/src/platform/keymaps/intellij-windows.ts b/src/platform/keymaps/intellij-windows.ts index 18ef8396b1..01af351eb9 100644 --- a/src/platform/keymaps/intellij-windows.ts +++ b/src/platform/keymaps/intellij-windows.ts @@ -198,6 +198,11 @@ export const intellijWindows: Record = { "editor.closeTab": ["Ctrl+F4"], "toolwindow.closeContent": ["Ctrl+F4", "Ctrl+Shift+F4"], "editor.reopenClosedTab": [], + "editor.closeUnmodifiedTabs": [], + "editor.closeAllButPinned": [], + "editor.closeTabsToLeft": [], + "editor.closeTabsToRight": [], + "editor.closeReadOnlyTabs": [], "file.toggleReadOnly": [], "editor.nextTab": ["Alt+Right"], "editor.prevTab": ["Alt+Left"], diff --git a/src/platform/keymaps/vscode-macos.ts b/src/platform/keymaps/vscode-macos.ts index ac737b335d..8ffc4e6a47 100644 --- a/src/platform/keymaps/vscode-macos.ts +++ b/src/platform/keymaps/vscode-macos.ts @@ -214,6 +214,11 @@ export const vscodeMacos: Record = { "editor.closeTab": ["Meta+W"], "toolwindow.closeContent": ["Meta+W"], "editor.reopenClosedTab": ["Meta+Shift+T"], + "editor.closeUnmodifiedTabs": [], + "editor.closeAllButPinned": [], + "editor.closeTabsToLeft": [], + "editor.closeTabsToRight": [], + "editor.closeReadOnlyTabs": [], "file.toggleReadOnly": [], "editor.nextTab": ["Meta+Alt+Right", "Meta+Shift+]"], "editor.prevTab": ["Meta+Alt+Left", "Meta+Shift+["],