Files
openidea/src/features/vcs/diffDialog.tsx
T
l0ng-ai 401c542647 feat(vcs): improve Git workflows (#5174)
Improve OpenIDEA's Git workflows across commit and staging, branch and remote operations, merge and rebase, Git Log and signing, worktrees, stash and shelf, patch application, and clone settings. Adds a one-click stash for tracked changes across Git roots and lets users open a worktree branch's associated GitHub review from its context menu. Cloning now includes submodules by default (Settings › Version Control › Git, can be turned off).

Repository entry points added here run in each repository's execution scope, so they use the project's own git executable.
2026-11-22 21:57:30 +08:00

166 lines
5.4 KiB
TypeScript

/**
* The diff "window" of the modal commit interface (#1863): diffs open in front, not in
* an editor tab that the modal Commit dialog would hide. A `Dialog` stacked on
* `openDialog`'s stack keeps whatever is underneath (the Commit dialog and its message,
* selection and options, a comparison list) mounted; Esc / the close button / the
* close-tab chord return to it. F7 / Shift+F7 and the diff navigation actions move
* along the file's change chain by swapping this dialog's content.
*/
import { useEffect, useRef, useState, type ReactNode } from "react";
import { bindingsFor, normalizeBinding, registerKeyClaim } from "@/platform/keymap";
import { Dialog } from "@/ui";
import type { EditorInput } from "@/workbench/contributions";
import { DiffEditor } from "./DiffEditor";
import { diffInput, diffTitle, type DiffData } from "./diffInput";
import { openDialog } from "./dialogs";
import { GitMergeDiff } from "./GitMergeDiff";
import { currentStagedDiffPath, stagedDocumentAtPath } from "./stagedDocument";
const SIZE_KEY = "openidea.vcs.diffDialogSize";
/** The diff window's last size (#2328); null on first run, clamped to the window by Dialog. */
export function loadDiffDialogSize(): { w: number; h: number } | null {
try {
const v = JSON.parse(localStorage.getItem(SIZE_KEY) ?? "null") as { w?: unknown; h?: unknown } | null;
return typeof v?.w === "number" && typeof v.h === "number" ? { w: v.w, h: v.h } : null;
} catch {
return null;
}
}
export function saveDiffDialogSize(size: { w: number; h: number }) {
try {
localStorage.setItem(SIZE_KEY, JSON.stringify(size));
} catch {
// Private mode etc. — the size just isn't remembered.
}
}
/** The open diff dialog's close function (one diff window at a time, as in IDEA). */
let shown: (() => void) | null = null;
/** Shows `render`'s window, replacing the open diff window instead of stacking another on top. */
export function showFront(render: (onClose: () => void) => ReactNode): () => void {
shown?.();
shown = null;
const close = openDialog((c) =>
render(() => {
if (shown === close) shown = null;
c();
}),
);
shown = close;
return () => {
if (shown === close) shown = null;
close();
};
}
/** Opens `data` in a front dialog; returns its close function. */
export function openDiffDialog(data: DiffData): () => void {
return showFront((onClose) => <DiffDialog data={data} onClose={onClose} />);
}
/** Opens a merge commit's file (parent ↔ merge result ↔ parent, a `git-merge-diff` input) in front. */
export function openMergeDiffDialog(input: EditorInput): () => void {
const title = (input.data as { path: string }).path;
return showFront((onClose) => (
<DiffDialogFrame title={title} onClose={onClose}>
<GitMergeDiff input={input} />
</DiffDialogFrame>
));
}
export function DiffDialogFrame({
title,
onClose,
children,
}: {
title: string;
onClose: () => void;
children: ReactNode;
}) {
const body = useRef<HTMLDivElement>(null);
// The size the user last resized the diff window to (#2328), else the usual default.
const [initialSize] = useState(
() =>
loadDiffDialogSize() ?? {
w: Math.min(1100, globalThis.innerWidth - 64),
h: Math.min(720, globalThis.innerHeight - 64),
},
);
useEffect(
() =>
registerKeyClaim((e, stroke) => {
const dialog = body.current?.closest('[role="dialog"]');
if (!dialog || !(e.target instanceof Element) || e.target.closest('[role="dialog"]') !== dialog) return false;
if (!bindingsFor("toolwindow.closeContent").some((b) => normalizeBinding(b) === stroke)) return false;
e.preventDefault();
e.stopPropagation();
onClose();
return true;
}),
[onClose],
);
return (
<Dialog
open
title={title}
onClose={onClose}
width="calc(100vw - 64px)"
resizable="both"
minWidth={480}
minHeight={280}
initialSize={initialSize}
onResized={saveDiffDialogSize}
buttons={[{ label: "Close", role: "cancel" }]}
>
<div
ref={body}
className="vcsDiffDialogBody"
// The dialog body is not a flex container: fill it so the editors reach the footer.
style={{ display: "flex", flexDirection: "column", height: "100%", minHeight: 0 }}
>
{children}
</div>
</Dialog>
);
}
function DiffDialog({ data: initial, onClose }: { data: DiffData; onClose: () => void }) {
const [data, setData] = useState(initial);
const input = diffInput({ ...data, embedded: true });
const transition = useRef(false);
const leave = async (next: () => void) => {
if (transition.current) return;
const path = currentStagedDiffPath(data);
const doc = path ? stagedDocumentAtPath(path) : undefined;
if (!doc?.dirty) {
next();
return;
}
transition.current = true;
const owner = `dialog-close:${input.key}`;
try {
await doc.prepareClose(owner);
await doc.save();
next();
} catch {
// The staged pane shows the error and still owns the draft.
} finally {
doc.cancelClose(owner);
transition.current = false;
}
};
return (
<DiffDialogFrame title={diffTitle(data)} onClose={() => void leave(onClose)}>
<DiffEditor
key={input.key}
input={input}
onNeighbour={(next) => void leave(() => setData(next))}
onJumpToSource={onClose}
/>
</DiffDialogFrame>
);
}