From 63db10a6889e1d917166da9ce34bde1db45e0156 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Tue, 1 Dec 2026 03:47:32 +0800 Subject: [PATCH] fix(vcs): reuse one diff tab with the preview tab turned off (#7108) Diffs from the version control views reused one tab only through the preview tab, so turning off Opening Policy > Enable preview tab opened every diff in a new tab. Editor kinds can now have their previews granted regardless of that setting (reusesPreviewTab); the diff, combined diff and merge commit diff kinds do, so Commit / Local Changes, the Git Log, shelves and stashes keep one diff tab while project files still open as regular tabs. Pinned and kept diff tabs stay, and activeInput({ previewOnly }) uses the same check so the Git Log's selection-following preview works too. Fixes #7096 --- src/editor/core/controller.ts | 11 +++++++-- src/editor/core/file-open-order.test.ts | 30 +++++++++++++++++++++++++ src/features/vcs/diffViewer.ts | 2 ++ src/features/vcs/index.ts | 1 + src/workbench/contributions.ts | 5 +++++ 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/editor/core/controller.ts b/src/editor/core/controller.ts index 084c31b0b..7c80f3715 100644 --- a/src/editor/core/controller.ts +++ b/src/editor/core/controller.ts @@ -43,6 +43,7 @@ import { useTextEditorWrappers, type EditorCloseSession, type EditorInput, + type EditorKindContribution, type OpenOptions, type FileEditorResolution, type OpenEditorTab, @@ -310,6 +311,11 @@ function tabModified(tab: Tab): boolean { ); } +/** Whether `input` opens in the preview tab when asked: always for kinds that reuse one, else per the setting. */ +function previewTabsFor(input: EditorInput, kinds: ReadonlyMap): boolean { + return editorPrefs().tabs.previewTab || !!kinds.get(input.kind)?.reusesPreviewTab; +} + export class EditorController implements SlotHost { manager: DocumentManager; readonly slots = new Map(); @@ -1529,7 +1535,7 @@ export class EditorController implements SlotHost { const kinds = useContributions.getState().editorKinds; const lifecycle = documentLifecycle(input, kinds); const canPreview = !lifecycle || (lifecycle.allowPreview && !documentDirty({ input, lifecycle })); - const preview = !!o.preview && editorPrefs().tabs.previewTab && canPreview; + const preview = !!o.preview && previewTabsFor(input, kinds) && canPreview; if (!activate && preview) return; const kind = kinds.get(input.kind); let paneId = s.panes[generation.pane] ? generation.pane : activate ? s.activePane : null; @@ -2447,7 +2453,8 @@ export class EditorController implements SlotHost { activeInput(options?: { previewOnly?: boolean }): EditorInput | null { const tab = this.activeTabOf(); - if (options?.previewOnly && (!tab?.preview || !editorPrefs().tabs.previewTab)) return null; + const kinds = useContributions.getState().editorKinds; + if (options?.previewOnly && (!tab?.preview || !previewTabsFor(tab.input, kinds))) return null; return tab?.input ?? null; } diff --git a/src/editor/core/file-open-order.test.ts b/src/editor/core/file-open-order.test.ts index f160ff194..cd6efc393 100644 --- a/src/editor/core/file-open-order.test.ts +++ b/src/editor/core/file-open-order.test.ts @@ -151,6 +151,36 @@ describe("file open order", () => { expect(activePath()).toBe("/p/b.ts"); }); + it("reuses one tab for kinds that ask for it while the preview tab is off", async () => { + useProject.setState({ settings: { editor: { tabs: { previewTab: false } } } }); + dispose = registerEditorKind({ + kind: "test-diff", + reusesPreviewTab: true, + component: () => null, + title: (input) => input.key, + }); + const controller = setup(); + const diff = (key: string) => ({ kind: "test-diff", key }); + await controller.openInput(diff("one"), { preview: true, focus: false }); + await controller.openInput(diff("two"), { preview: true, focus: false }); + const tabs = () => Object.values(useTabs.getState().panes).flatMap((pane) => pane.tabs); + expect(tabs().map((t) => [t.input.key, t.preview])).toEqual([["two", true]]); + expect(controller.activeInput({ previewOnly: true })?.key).toBe("two"); + // Files still don't preview. + await controller.openFile("/p/a.ts", { preview: true, focus: false }); + await controller.openFile("/p/b.ts", { preview: true, focus: false }); + expect(openFilePaths(useTabs.getState()).sort()).toEqual(["/p/a.ts", "/p/b.ts"]); + // A kept diff tab stays. + const pane = useTabs.getState().activePane; + controller.keepTab(pane, tabs().find((t) => t.input.key == "two")!.id); + await controller.openInput(diff("three"), { preview: true, focus: false }); + expect( + tabs() + .filter((t) => t.input.kind == "test-diff") + .map((t) => t.input.key), + ).toEqual(["two", "three"]); + }); + it("keeps an earlier regular file without removing the newer preview", async () => { const { started, response } = delayProvider(); const controller = setup(); diff --git a/src/features/vcs/diffViewer.ts b/src/features/vcs/diffViewer.ts index d7bbf3305..4c458d151 100644 --- a/src/features/vcs/diffViewer.ts +++ b/src/features/vcs/diffViewer.ts @@ -16,6 +16,7 @@ export function activateDiffViewer(): () => void { const single = registerEditorKind({ kind: DIFF_KIND, standalone: true, + reusesPreviewTab: true, // Switching back shows the diff where it was left (scroll, caret, expanded lines), // without reloading the revisions. keepAlive: true, @@ -28,6 +29,7 @@ export function activateDiffViewer(): () => void { const combined = registerEditorKind({ kind: COMBINED_DIFF_KIND, standalone: true, + reusesPreviewTab: true, keepAlive: true, component: lazyComponent(() => import("./CombinedDiff").then((m) => m.CombinedDiffTab)), title: (input) => (input.data as CombinedDiffData).title ?? tx("Combined Changes Diff"), diff --git a/src/features/vcs/index.ts b/src/features/vcs/index.ts index e6bc26318..df656717d 100644 --- a/src/features/vcs/index.ts +++ b/src/features/vcs/index.ts @@ -426,6 +426,7 @@ export function activate(): () => void { registerEditorKind({ kind: "git-merge-diff", standalone: true, + reusesPreviewTab: true, component: lazyComponent(() => import("./GitMergeDiff").then((m) => m.GitMergeDiff)), title: (input) => (input.data as { path: string }).path, icon: () => "diff", diff --git a/src/workbench/contributions.ts b/src/workbench/contributions.ts index 0872545db..41000c0cd 100644 --- a/src/workbench/contributions.ts +++ b/src/workbench/contributions.ts @@ -169,6 +169,11 @@ export interface EditorKindContribution { * `splitInput`, or moves the tab instead. */ singleton?: boolean; + /** + * A preview tab its opener asks for is granted even with Opening Policy › Enable preview tab + * off, so stepping through a change set's diffs reuses one tab. Pinned or kept tabs stay. + */ + reusesPreviewTab?: boolean; /** Fresh input opened when splitting a `singleton` tab (e.g. a new shell in the same cwd). */ splitInput?(input: EditorInput): EditorInput | Promise | null; /** Last tab showing this input closed (including split tabs and tab-limit eviction). */