From 31e7b420ae1e362d14d3cf3fd37724b424ee1db0 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Fri, 4 Dec 2026 13:04:44 +0800 Subject: [PATCH] feat(vcs): close a committed file's local changes diff after the commit (#7942) When a commit leaves a file with no local changes, its HEAD/index vs working copy diff tab closes. Pinned tabs, files with changes left after a partial commit, and other diffs (Log, Compare with Branch) stay open. Closes #7914 --- src/editor/core/controller.ts | 22 ++++--- .../vcs/__tests__/committedDiffs.test.ts | 63 +++++++++++++++++++ src/features/vcs/changeOps.tsx | 6 +- src/features/vcs/committedDiffs.ts | 41 ++++++++++++ src/workbench/contributions.ts | 2 + 5 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 src/features/vcs/__tests__/committedDiffs.test.ts create mode 100644 src/features/vcs/committedDiffs.ts diff --git a/src/editor/core/controller.ts b/src/editor/core/controller.ts index 9047662a4d..0361102e6c 100644 --- a/src/editor/core/controller.ts +++ b/src/editor/core/controller.ts @@ -2477,14 +2477,20 @@ export class EditorController implements SlotHost { const kinds = useContributions.getState().editorKinds; const out = new Map(); for (const id of paneIds(s)) - for (const t of s.panes[id]?.tabs ?? []) - if (!out.has(t.input.key)) - out.set(t.input.key, { - input: t.input, - title: tabTitle(t.input, kinds), - icon: tabIcon(t.input, kinds), - lastActive: this.lastActive.get(t.input.key) ?? 0, - }); + for (const t of s.panes[id]?.tabs ?? []) { + const seen = out.get(t.input.key); + if (seen) { + if (t.pinned) seen.pinned = true; + continue; + } + out.set(t.input.key, { + input: t.input, + title: tabTitle(t.input, kinds), + icon: tabIcon(t.input, kinds), + lastActive: this.lastActive.get(t.input.key) ?? 0, + pinned: !!t.pinned, + }); + } return [...out.values()].sort((a, b) => b.lastActive - a.lastActive); } diff --git a/src/features/vcs/__tests__/committedDiffs.test.ts b/src/features/vcs/__tests__/committedDiffs.test.ts new file mode 100644 index 0000000000..6e066a3694 --- /dev/null +++ b/src/features/vcs/__tests__/committedDiffs.test.ts @@ -0,0 +1,63 @@ +import { afterEach, expect, it, vi } from "vitest"; +import { provideEditorService, type EditorService, type OpenEditorTab } from "@/workbench/contributions"; +import { closeCommittedDiffs } from "../committedDiffs"; +import { diffInput, type DiffData } from "../diffInput"; + +const status = vi.hoisted(() => new Map()); +vi.mock("../store", async (actual) => ({ + ...(await actual()), + refreshPathsNow: vi.fn(async () => {}), + statusOf: (path: string) => status.get(path), +})); + +afterEach(() => { + status.clear(); + provideEditorService(undefined); +}); + +const tab = (data: DiffData, pinned = false): OpenEditorTab => ({ + input: diffInput(data), + title: "", + icon: "", + lastActive: 0, + pinned, +}); + +function editorWith(tabs: OpenEditorTab[]) { + const closeInput = vi.fn(async (_key: string) => true); + provideEditorService({ openTabs: () => tabs, closeInput } as unknown as EditorService); + return closeInput; +} + +it("closes the local changes diffs of committed files with nothing left to commit", async () => { + const committed = tab({ path: "/p/a.ts", left: { type: "rev", rev: "HEAD" }, right: { type: "worktree" } }); + const staged = tab({ path: "/p/b.ts", left: { type: "rev", rev: "HEAD" }, right: { type: "rev", rev: ":0" } }); + const added = tab({ + path: "/p/new.ts", + left: { type: "text", text: "", label: "(does not exist)", absent: true }, + right: { type: "worktree" }, + }); + const partly = tab({ path: "/p/c.ts", left: { type: "rev", rev: "HEAD" }, right: { type: "worktree" } }); + status.set("/p/c.ts", { path: "/p/c.ts" }); + const notCommitted = tab({ path: "/p/d.ts", left: { type: "rev", rev: "HEAD" }, right: { type: "worktree" } }); + const pinned = tab({ path: "/p/e.ts", left: { type: "rev", rev: "HEAD" }, right: { type: "worktree" } }, true); + const fromLog = tab({ path: "/p/a.ts", left: { type: "rev", rev: "abc123" }, right: { type: "rev", rev: "def456" } }); + const branch = tab({ path: "/p/a.ts", left: { type: "rev", rev: "main" }, right: { type: "worktree" } }); + const closeInput = editorWith([committed, staged, added, partly, notCommitted, pinned, fromLog, branch]); + + await closeCommittedDiffs(["/p/a.ts", "/p/b.ts", "/p/new.ts", "/p/c.ts", "/p/e.ts"]); + + expect(closeInput.mock.calls.map(([key]) => key)).toEqual([committed.input.key, staged.input.key, added.input.key]); +}); + +it("leaves other editor tabs alone", async () => { + const file: OpenEditorTab = { + input: { kind: "file", key: "file:/p/a.ts", path: "/p/a.ts" }, + title: "", + icon: "", + lastActive: 0, + }; + const closeInput = editorWith([file]); + await closeCommittedDiffs(["/p/a.ts"]); + expect(closeInput).not.toHaveBeenCalled(); +}); diff --git a/src/features/vcs/changeOps.tsx b/src/features/vcs/changeOps.tsx index 2eeb998c52..d704e3c973 100644 --- a/src/features/vcs/changeOps.tsx +++ b/src/features/vcs/changeOps.tsx @@ -37,6 +37,7 @@ import { confirmLeftOutOfMerge, leftOutOfMerge } from "./mergeCommitLeftOut"; import { controlLabel, controlOperation } from "./operations"; import { FileChecklist } from "./shelf"; import { reloadLog } from "./logStore"; +import { closeCommittedDiffs } from "./committedDiffs"; import { currentRepo, loadStashes, refreshNow, repoName, repoOf, rootOfKind, useVcs, vcsKindOf } from "./store"; import styles from "./common.module.css"; import rollbackStyles from "./RollbackDialog.module.css"; @@ -462,7 +463,10 @@ async function prepareAndCommit( }, ); if (testSelection?.enabled && testTargets.length && projectIsOpen()) startPostCommitTests(testTargets, testSelection); - if (committed !== undefined) finishCommitDraft(messageDraft, plan.included, clearMessage); + if (committed !== undefined) { + finishCommitDraft(messageDraft, plan.included, clearMessage); + void closeCommittedDiffs(plan.included.map((file) => file.path)); + } // A Git commit moves HEAD, which reloads the Log; another system's working copy has no HEAD to watch. if (committed?.some(({ repo }) => vcsKindOf(repo ?? useVcs.getState().root) !== "git")) void reloadLog({ preserve: true }); diff --git a/src/features/vcs/committedDiffs.ts b/src/features/vcs/committedDiffs.ts new file mode 100644 index 0000000000..967623a2f8 --- /dev/null +++ b/src/features/vcs/committedDiffs.ts @@ -0,0 +1,41 @@ +import { maybeEditorService } from "@/workbench/contributions"; +import { DIFF_KIND, type DiffData, type DiffSide } from "./diffInput"; +import { stripSlash } from "./model"; +import { refreshPathsNow, statusOf } from "./store"; + +const base = (s: DiffSide) => + (s.type === "rev" && !s.svn && (s.rev === "HEAD" || s.rev === ":0")) || (s.type === "text" && !!s.absent); +const local = (s: DiffSide) => + s.type === "worktree" || (s.type === "rev" && !s.svn && s.rev === ":0") || (s.type === "text" && !!s.absent); + +/** A diff of the file's local changes (HEAD or the index ↔ the working copy or the index), as the Commit window opens it. */ +export function isLocalChangesDiff(data: DiffData): boolean { + return !data.context && !data.embedded && base(data.left) && local(data.right); +} + +/** + * After a commit, closes the local changes diffs of committed files that have no changes left, + * as IDEA closes its diff preview once a commit leaves nothing to show. Pinned tabs stay open, + * and so does every other diff (Log, Compare with Branch…). + */ +export async function closeCommittedDiffs(paths: readonly string[]): Promise { + const editor = maybeEditorService(); + if (!paths.length || !editor?.openTabs || !editor.closeInput) return; + const committed = new Set(paths.map(stripSlash)); + const tabs = editor + .openTabs() + .filter( + (t) => + t.input.kind === DIFF_KIND && + !t.pinned && + !!t.input.path && + committed.has(stripSlash(t.input.path)) && + isLocalChangesDiff(t.input.data as DiffData), + ); + if (!tabs.length) return; + await refreshPathsNow(tabs.map((t) => t.input.path!)); + for (const t of tabs) { + // oxlint-disable-next-line no-await-in-loop -- tabs close one at a time, each moving the focus on + if (!statusOf(t.input.path!)) await editor.closeInput(t.input.key); + } +} diff --git a/src/workbench/contributions.ts b/src/workbench/contributions.ts index 9b05f53ba7..aae9828ee6 100644 --- a/src/workbench/contributions.ts +++ b/src/workbench/contributions.ts @@ -854,6 +854,8 @@ export interface OpenEditorTab { icon: string; /** Activation order this session (higher is more recent); 0 if never active. */ lastActive: number; + /** Pinned in some pane. */ + pinned?: boolean; } /** MIME of a dragged editor tab: the payload is `{ paneId, tabId }` as JSON. */