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
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-04 13:04:44 +08:00
1 джерело 70a7932939
коміт 31e7b420ae
5 files changed
+125 -9

No files matched your search

+14 -8
Переглянути файл
@@ -2477,14 +2477,20 @@ export class EditorController implements SlotHost {
const kinds = useContributions.getState().editorKinds;
const out = new Map<string, OpenEditorTab>();
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);
}
+63
Переглянути файл
@@ -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<string, object>());
vi.mock("../store", async (actual) => ({
...(await actual<typeof import("../store")>()),
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();
});
+5 -1
Переглянути файл
@@ -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 });
+41
Переглянути файл
@@ -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<void> {
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);
}
}
+2
Переглянути файл
@@ -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. */