mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
Deployment actions run by shortcut, Find Action or the Tools menu used the Project view's selection (else the editor's file) even in the Commit tool window, where only the context menu passed the selected changes. The changes tree now provides its selection while Commit or Git > Local Changes is focused, without deleted files, and Deployment takes it after a menu's paths. Co-authored-by: GGVidal <52431033+GGVidal@users.noreply.github.com>
1558 lines
60 KiB
TypeScript
1558 lines
60 KiB
TypeScript
/**
|
||
* Commit tool window: change list with inclusion checkboxes, message, stashes. Its
|
||
* parts also make the Git window's Local Changes / Stash tabs and the Commit dialog.
|
||
*/
|
||
import { writeClipboardText } from "@/platform/clipboard";
|
||
import { useWorkflowSettings } from "@/platform/workflowSettings";
|
||
import { formatBinding, isMacHost, macStroke, normalizeBinding, shortcutLabel, strokeOf } from "@/platform/keymap";
|
||
import {
|
||
useCallback,
|
||
useEffect,
|
||
useLayoutEffect,
|
||
useMemo,
|
||
useRef,
|
||
useState,
|
||
type MouseEvent,
|
||
type PointerEvent,
|
||
} from "react";
|
||
import { tx, txTemplate } from "@/platform/i18n";
|
||
import clsx from "clsx";
|
||
import { executeAction } from "@/platform/actions";
|
||
import { fontWeightOf } from "@/platform/editorSettings";
|
||
import { resolveMonoFont } from "@/platform/fonts";
|
||
import { basename, dirname, useProject } from "@/platform/project";
|
||
import { isTestFile, useEffectiveRoots } from "@/platform/projectRoots";
|
||
import {
|
||
Button,
|
||
Checkbox,
|
||
EmptyState,
|
||
Icon,
|
||
IconButton,
|
||
Menu,
|
||
Select,
|
||
SplitGroup,
|
||
SplitHandle,
|
||
SplitPanel,
|
||
Tabs,
|
||
Tree,
|
||
fileIconName,
|
||
registerDropTarget,
|
||
showContextMenu,
|
||
startPointerDrag,
|
||
tooltipAttrs,
|
||
type MenuItem,
|
||
type TabItem,
|
||
type TreeHandle,
|
||
T,
|
||
} from "@/ui";
|
||
import {
|
||
editorService,
|
||
maybeEditorService,
|
||
menuGroupItems,
|
||
type EditorInput,
|
||
useMenuSlotItems,
|
||
} from "@/workbench/contributions";
|
||
import { evaluateWhen, useContextKeys } from "@/platform/context";
|
||
import { useToolWindowLayout } from "@/workbench/toolwindows/store";
|
||
import {
|
||
aggregateCheck,
|
||
buildChangeTree,
|
||
checkOf,
|
||
filesOf,
|
||
groupOf,
|
||
ignoreTargets,
|
||
isPartiallyStaged,
|
||
planCommit,
|
||
toggledCheck,
|
||
type ChangeNode,
|
||
} from "./changes";
|
||
import { applyStash, commitChanges, deleteFiles, dropStash, showUnstashDialog, stashChanges } from "./changeOps";
|
||
import { CommitOptionsPopup } from "./CommitOptions";
|
||
import { DEFAULT_LIST_NAME, useChangelists, withListDefaults } from "./changelists";
|
||
import { inclusionChecks, isCommitAll, stagingAreaOn, useStagingArea } from "./staging";
|
||
import { addToChangelist, commitFiles } from "./changelistsUi";
|
||
import { acceptSide } from "./conflictsUi";
|
||
import { copyChangesAsPatch } from "./patches";
|
||
import {
|
||
messageHistory,
|
||
pendingFixup,
|
||
setAmend,
|
||
setCommitDiffPreview,
|
||
setDiffPreview,
|
||
setGroupByDir,
|
||
setGroupByRepo,
|
||
setGroupByStaging,
|
||
setShowUnversionedSeparately,
|
||
useCommit,
|
||
type CommitTab,
|
||
} from "./commitStore";
|
||
import { pushAfterCommit } from "./commitUi";
|
||
import { rebaseAfterFixup } from "./rebase";
|
||
import { stashTabOf, stashTabs, type ContentTab } from "./gitTabs";
|
||
import {
|
||
DIFF_KIND,
|
||
changeNodeDiff,
|
||
changeNodeDiffData,
|
||
changeTreeDiffData,
|
||
diffInput,
|
||
openDiff,
|
||
openVcsDiff,
|
||
opensDiffOnActivate,
|
||
vcsDiffInFront,
|
||
workingDiffData,
|
||
} from "./diffInput";
|
||
import { DiffEditor } from "./DiffEditor";
|
||
import { KIND_COLOR, KIND_LABEL, fileCount, formatTime, stashLabel, stripSlash } from "./model";
|
||
import { call, commands } from "@/platform/ipc";
|
||
import type { Commit, FileStatus, Stash } from "@/bindings";
|
||
import { showChangesDiff, toLogChanges, type LogChange } from "./logChanges";
|
||
import { OperationBanner } from "./operations";
|
||
import { runOp } from "./ops";
|
||
import { ShelfPanel, loadShelves, useShelf } from "./shelf";
|
||
import { existsOnDisk, refreshNow, repoName, selectRepo, useVcs } from "./store";
|
||
import { saveVcsSetting } from "./settingsUi";
|
||
import styles from "./CommitView.module.css";
|
||
|
||
/** MIME type of files dragged onto a change list (absolute paths, newline-separated). */
|
||
const CHANGES_MIME = "application/x-openidea-changes";
|
||
|
||
/** Shown text of a node: group names are translated, except the names of user change lists. */
|
||
function nodeText(n: ChangeNode): string {
|
||
return n.type === "group" && (!n.changelist || n.label === DEFAULT_LIST_NAME) ? tx(n.label) : n.label;
|
||
}
|
||
|
||
/** Content tabs as `Tabs` items, with their counts. */
|
||
export function tabItems(tabs: readonly ContentTab[]): TabItem[] {
|
||
return tabs.map((t) => ({
|
||
id: t.id,
|
||
label: t.label,
|
||
// `Tabs` treats a missing flag as closable once it has `onClose`.
|
||
closable: !!t.closable,
|
||
trailing: t.count ? <span className={styles.count}>{t.count}</span> : undefined,
|
||
}));
|
||
}
|
||
|
||
/** Shelf and stash counts for the tabs (loads the shelves once VCS is available). */
|
||
export function useStashTabCounts(available: boolean) {
|
||
const roots = useVcs((s) => s.roots);
|
||
const version = useVcs((s) => s.version);
|
||
const [stashCount, setStashCount] = useState(0);
|
||
useEffect(() => {
|
||
let live = true;
|
||
if (available)
|
||
void Promise.all(roots.map((r) => call(commands.vcsStashes(r))))
|
||
.then((lists) => live && setStashCount(lists.reduce((n, list) => n + list.length, 0)))
|
||
.catch(() => undefined);
|
||
return () => {
|
||
live = false;
|
||
};
|
||
}, [available, roots, version]);
|
||
const shelfCount = useShelf((s) => s.shelves.length);
|
||
useEffect(() => {
|
||
if (available) void loadShelves().catch(() => undefined);
|
||
}, [available]);
|
||
return { stashCount: available ? stashCount : 0, shelfCount };
|
||
}
|
||
|
||
/** "Commit" / "Shelf" / "Stash" content tabs; they stand in for the tool window title. */
|
||
export function CommitTabs() {
|
||
const available = useVcs((s) => s.available);
|
||
const combine = useWorkflowSettings((s) => s.vcs.git.combineStashAndShelf);
|
||
const tab = useCommit((s) => s.tab);
|
||
const counts = useStashTabCounts(available);
|
||
return (
|
||
<Tabs
|
||
variant="compact"
|
||
className={styles.tabs}
|
||
activeId={stashTabOf(tab, combine)}
|
||
onSelect={(id) => useCommit.setState({ tab: id as CommitTab })}
|
||
aria-label="Commit"
|
||
items={tabItems([
|
||
{ id: "changes", label: "Commit" },
|
||
...(available ? stashTabs({ combine, tab, ...counts }) : []),
|
||
])}
|
||
/>
|
||
);
|
||
}
|
||
|
||
export function CommitToolWindow() {
|
||
const available = useVcs((s) => s.available);
|
||
const combine = useWorkflowSettings((s) => s.vcs.git.combineStashAndShelf);
|
||
const tab = stashTabOf(
|
||
useCommit((s) => s.tab),
|
||
combine,
|
||
);
|
||
if (!available)
|
||
return (
|
||
<EmptyState icon="commit" title="Version control is not configured">
|
||
<T>Open a Git repository to commit changes.</T>
|
||
</EmptyState>
|
||
);
|
||
return <div className={styles.root}>{tab === "changes" ? <ChangesPanel /> : <StashContent tab={tab} />}</div>;
|
||
}
|
||
|
||
/** The Shelf, Stash or combined "Stashes and Shelves" tab. */
|
||
export function StashContent({ tab }: { tab: CommitTab }) {
|
||
const combine = useWorkflowSettings((s) => s.vcs.git.combineStashAndShelf);
|
||
if (combine)
|
||
return (
|
||
<SplitGroup orientation="vertical" className={styles.split} storageKey="openidea.vcs.stashShelfSplit">
|
||
<SplitPanel id="stashes" defaultSize="50%" minSize="80px">
|
||
<StashesPanel />
|
||
</SplitPanel>
|
||
<SplitHandle />
|
||
<SplitPanel id="shelf" defaultSize="50%" minSize="80px">
|
||
<ShelfPanel />
|
||
</SplitPanel>
|
||
</SplitGroup>
|
||
);
|
||
return tab === "shelf" ? <ShelfPanel /> : <StashesPanel />;
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
|
||
const bottomLeft = (el: HTMLElement) => {
|
||
const r = el.getBoundingClientRect();
|
||
return { x: r.left, y: r.bottom + 2 };
|
||
};
|
||
|
||
/**
|
||
* Activates a change (Enter, double-click, Show Diff); F7 continues through the other
|
||
* files of its group, in tree order (Enter and double-click: of the whole tree, on into the
|
||
* unversioned files, as IDEA's Commit diff preview). Enter and double-click follow IDEA's Show on
|
||
* Double-Click (opensDiffOnActivate): the source file, or the diff where the commit
|
||
* interface puts it (openVcsDiff: a preview editor tab, or under the modal interface the
|
||
* front diff window, behind the modal Commit dialog, #1863). `forceDiff` is Show Diff and
|
||
* the Commit dialog's own activation, which always show the diff in front.
|
||
*/
|
||
function openNode(n: ChangeNode, roots: readonly ChangeNode[], forceDiff = false) {
|
||
if (n.type !== "file" || !n.path) return;
|
||
if (!forceDiff && !opensDiffOnActivate()) {
|
||
void editorService().openFile(n.path, { focus: true });
|
||
return;
|
||
}
|
||
void openVcsDiff((forceDiff ? changeNodeDiffData : changeTreeDiffData)(roots, n), { preview: true });
|
||
}
|
||
|
||
/** The setting behind Show on Double-Click (`None` follows the commit interface). */
|
||
export const DOUBLE_CLICK_KEY = "vcs.commit.doubleClickOpensDiff";
|
||
|
||
/**
|
||
* IDEA's Show on Double-Click: the change lists' View Options items. An unset value
|
||
* (Follow the Interface) is written as `null`, which removes the key from the settings.
|
||
*/
|
||
export function doubleClickMenu(value: boolean | null | undefined): MenuItem[] {
|
||
const on = value ?? null;
|
||
const set = (v: boolean | null) => () => void saveVcsSetting(DOUBLE_CLICK_KEY, v);
|
||
return [
|
||
{ kind: "separator" },
|
||
{
|
||
label: tx("Show on Double-Click"),
|
||
submenu: [
|
||
{ label: tx("Follow the Interface"), checked: on === null, run: set(null) },
|
||
{ kind: "separator" },
|
||
{ label: tx("Diff"), checked: on === true, run: set(true) },
|
||
{ label: tx("Source"), checked: on === false, run: set(false) },
|
||
],
|
||
},
|
||
];
|
||
}
|
||
|
||
/** Context menu of a change list's header: the list's own actions, then the bulk ones on its files. */
|
||
export function listMenu(id: string, active: boolean, files: readonly FileStatus[]): MenuItem[] {
|
||
const paths = files.map((f) => stripSlash(f.path));
|
||
const count = paths.length;
|
||
return [
|
||
...(count ? [{ kind: "action" as const, action: "vcs.changelist.commit", arg: id, label: "Commit…" }] : []),
|
||
{ kind: "separator" },
|
||
{ kind: "action", action: "vcs.changelist.new" },
|
||
{ kind: "action", action: "vcs.changelist.edit", arg: id },
|
||
...(active
|
||
? []
|
||
: [
|
||
{ kind: "action" as const, action: "vcs.changelist.setActive", arg: id },
|
||
{ kind: "action" as const, action: "vcs.changelist.delete", arg: id },
|
||
]),
|
||
{ kind: "separator" },
|
||
// The whole list's files, as IDEA offers on a change list's header and as file
|
||
// and directory nodes below it already offer (issue #473).
|
||
{ kind: "action", action: "vcs.rollback", arg: paths, label: "Rollback…", disabled: !count },
|
||
{
|
||
label: "Stage",
|
||
icon: "plus",
|
||
disabled: !files.some((f) => f.unstaged),
|
||
run: () => void runOp(tx`Stage ${count} files`, () => call(commands.vcsStage(paths))),
|
||
},
|
||
{
|
||
label: "Unstage",
|
||
icon: "minus",
|
||
disabled: !files.some((f) => f.staged),
|
||
run: () => void runOp(tx`Unstage ${count} files`, () => call(commands.vcsUnstage(paths))),
|
||
},
|
||
{ kind: "separator" },
|
||
...(count
|
||
? [
|
||
{ kind: "action" as const, action: "vcs.shelve", arg: paths },
|
||
{ kind: "action" as const, action: "vcs.createPatch", arg: paths },
|
||
]
|
||
: []),
|
||
];
|
||
}
|
||
|
||
/** Paths Delete… applies to: only when every selected file is unversioned (versioned ones are rolled back). */
|
||
/** The selected files that still exist in the working tree (Delete… removes them from disk). */
|
||
/** Whether the change leaves the file in the working tree. */
|
||
const onDisk = existsOnDisk;
|
||
|
||
export function deletableFiles(nodes: ChangeNode[]): string[] {
|
||
const files = filesOf(nodes);
|
||
return files.length && files.every(onDisk) ? files.map((f) => f.path) : [];
|
||
}
|
||
|
||
/**
|
||
* Copies the selected files' absolute paths (`absolute`) or just their names
|
||
* (`name`) to the clipboard, one per line: Ctrl+C / Ctrl+Shift+C of the changes
|
||
* tree and the context menu's Copy items (#1877).
|
||
*/
|
||
export function copyChangePaths(nodes: ChangeNode[], kind: "absolute" | "name") {
|
||
const text = filesOf(nodes)
|
||
.map((f) => (kind === "name" ? basename(f.path) : f.path))
|
||
.join("\n");
|
||
if (text) void writeClipboardText(text);
|
||
}
|
||
|
||
export function nodeMenu(
|
||
nodes: ChangeNode[],
|
||
repos: readonly string[],
|
||
opts?: { roots?: readonly ChangeNode[] },
|
||
): MenuItem[] {
|
||
const files = filesOf(nodes);
|
||
const deletable = deletableFiles(nodes);
|
||
const paths = files.map((f) => f.path);
|
||
const first = nodes.find((n) => n.type === "file");
|
||
const untracked = files.filter((f) => f.unstaged === "untracked");
|
||
const toIgnore = ignoreTargets(nodes, repos);
|
||
const conflicted = files.filter((f) => f.unstaged === "conflicted");
|
||
const items: MenuItem[] = [
|
||
...(conflicted.length
|
||
? [
|
||
{
|
||
label: "Accept Yours",
|
||
run: () => void Promise.all(conflicted.map((f) => acceptSide(f, "ours"))).then(() => refreshNow()),
|
||
},
|
||
{
|
||
label: "Accept Theirs",
|
||
run: () => void Promise.all(conflicted.map((f) => acceptSide(f, "theirs"))).then(() => refreshNow()),
|
||
},
|
||
{ kind: "separator" as const },
|
||
]
|
||
: []),
|
||
// With the staging area a commit takes the whole index: there is no committing one file.
|
||
...(stagingAreaOn()
|
||
? []
|
||
: [
|
||
{
|
||
label: "Commit File…",
|
||
icon: "commit" as const,
|
||
disabled: !files.some((f) => groupOf(f) === "changes"),
|
||
run: () => commitFiles(paths),
|
||
},
|
||
{ kind: "separator" as const },
|
||
]),
|
||
// The node itself, like double-click: under Staged or Unstaged its side of the index.
|
||
opts?.roots && first
|
||
? {
|
||
label: "Show Diff",
|
||
icon: "diff",
|
||
shortcut: shortcutLabel("vcs.showDiff"),
|
||
// An explicit Show Diff always opens the diff, whatever Show on Double-Click says.
|
||
run: () => openNode(first, opts.roots!, true),
|
||
}
|
||
: { kind: "action", action: "vcs.showDiff", arg: first?.path },
|
||
{
|
||
kind: "action",
|
||
action: "vcs.showDiffInNewWindow",
|
||
arg: first?.path ? (opts?.roots ? changeNodeDiffData(opts.roots, first) : changeNodeDiff(first)) : undefined,
|
||
disabled: !first?.path,
|
||
},
|
||
{
|
||
label: "Jump to Source",
|
||
icon: "external-link",
|
||
shortcut: "F4",
|
||
disabled: !first?.path || first.kind === "deleted",
|
||
run: () => first?.path && void editorService().openFile(first.path, { focus: true }),
|
||
},
|
||
{ kind: "separator" },
|
||
];
|
||
if (conflicted.length) items.push({ kind: "action", action: "vcs.resolveConflicts", label: "Resolve Conflicts…" });
|
||
items.push(
|
||
{ kind: "action", action: "vcs.rollback", arg: paths, label: "Rollback…" },
|
||
...(untracked.length
|
||
? [{ kind: "action" as const, action: "vcs.addToVcs", arg: untracked.map((f) => f.path) }]
|
||
: []),
|
||
...(deletable.length
|
||
? [
|
||
{
|
||
label: "Delete…",
|
||
icon: "trash",
|
||
shortcut: formatBinding(isMacHost() ? "Backspace" : "Delete"),
|
||
run: () => void deleteFiles(deletable),
|
||
},
|
||
]
|
||
: []),
|
||
// Both destinations, as in the project view's Git menu.
|
||
...(toIgnore.length
|
||
? [
|
||
{
|
||
label: "Add to .gitignore",
|
||
submenu: [
|
||
{ kind: "action" as const, action: "vcs.ignoreShared", arg: toIgnore, label: ".gitignore" },
|
||
{ kind: "action" as const, action: "vcs.ignoreLocal", arg: toIgnore, label: ".git/info/exclude" },
|
||
],
|
||
},
|
||
]
|
||
: []),
|
||
{
|
||
label: "Stage",
|
||
icon: "plus",
|
||
// Under Staged Changes, a partially staged file only offers Unstage.
|
||
disabled: !files.some((f) => f.unstaged) || nodes.every((n) => n.group === "staged"),
|
||
run: () => void runOp(tx`Stage ${paths.length} files`, () => call(commands.vcsStage(paths))),
|
||
},
|
||
{
|
||
label: "Unstage",
|
||
icon: "minus",
|
||
disabled: !files.some((f) => f.staged) || nodes.every((n) => n.group === "unstaged" || n.group === "unversioned"),
|
||
run: () => void runOp(tx`Unstage ${paths.length} files`, () => call(commands.vcsUnstage(paths))),
|
||
},
|
||
{ kind: "separator" },
|
||
...(nodes.some((n) => n.changelist)
|
||
? [{ kind: "action" as const, action: "vcs.changelist.move", arg: paths, label: "Move to Another Changelist…" }]
|
||
: []),
|
||
{ kind: "action", action: "vcs.shelve", arg: paths },
|
||
{ kind: "action", action: "vcs.createPatch", arg: paths },
|
||
{ kind: "action", action: "vcs.stashSelected", arg: paths, disabled: !paths.length },
|
||
{ label: "Copy as Patch to Clipboard", run: () => void copyChangesAsPatch(paths) },
|
||
{ kind: "separator" },
|
||
{
|
||
label: "Show History",
|
||
icon: "history",
|
||
disabled: !first?.path,
|
||
run: () => void executeAction("vcs.showLog", { path: first?.path }),
|
||
},
|
||
{ kind: "action", action: "vcs.localHistory", arg: first?.path },
|
||
{ kind: "action", action: "vcs.annotate", arg: first?.path },
|
||
{ kind: "separator" },
|
||
// Other features' groups (Deployment) for the files that exist.
|
||
...menuGroupItems("changes", {
|
||
paths: files.filter(onDisk).map((f) => stripSlash(f.path)),
|
||
}),
|
||
{ label: "Copy Path", icon: "copy", run: () => copyChangePaths(nodes, "absolute") },
|
||
{ label: "Copy File Name", icon: "copy", run: () => copyChangePaths(nodes, "name") },
|
||
);
|
||
return items;
|
||
}
|
||
|
||
/**
|
||
* The staging operation of a node of the staging area's tree: Unstage under Staged, Stage
|
||
* under Unstaged and Unversioned Files, applied to every file below the node.
|
||
*/
|
||
export function stageOperation(n: ChangeNode): { label: string; icon: "plus" | "minus"; paths: string[] } | null {
|
||
const paths = n.files.map((f) => stripSlash(f.path));
|
||
if (!paths.length) return null;
|
||
if (n.group === "staged") return { label: "Unstage", icon: "minus", paths };
|
||
if (n.group === "unstaged" || n.group === "unversioned") return { label: "Stage", icon: "plus", paths };
|
||
return null;
|
||
}
|
||
|
||
function StageButton({ node }: { node: ChangeNode }) {
|
||
const op = stageOperation(node);
|
||
if (!op) return null;
|
||
const { label, icon, paths } = op;
|
||
const count = paths.length;
|
||
return (
|
||
<IconButton
|
||
icon={icon}
|
||
title={label}
|
||
size="small"
|
||
className={styles.stageButton}
|
||
onMouseDown={(e) => e.stopPropagation()}
|
||
onDoubleClick={(e) => e.stopPropagation()}
|
||
onClick={(e) => {
|
||
e.stopPropagation();
|
||
void runOp(icon === "plus" ? tx`Stage ${count} files` : tx`Unstage ${count} files`, () =>
|
||
call(icon === "plus" ? commands.vcsStage(paths) : commands.vcsUnstage(paths)),
|
||
).then(() => refreshNow());
|
||
}}
|
||
/>
|
||
);
|
||
}
|
||
|
||
function CheckMark({
|
||
checked,
|
||
indeterminate,
|
||
label,
|
||
onToggle,
|
||
}: {
|
||
checked: boolean;
|
||
indeterminate: boolean;
|
||
label: string;
|
||
onToggle: () => void;
|
||
}) {
|
||
return (
|
||
<span
|
||
role="checkbox"
|
||
aria-checked={indeterminate ? "mixed" : checked}
|
||
aria-label={label}
|
||
className={clsx(styles.check, (checked || indeterminate) && styles.checkOn)}
|
||
onMouseDown={(e) => {
|
||
if (e.button !== 0) return;
|
||
e.stopPropagation();
|
||
e.preventDefault();
|
||
onToggle();
|
||
}}
|
||
onDoubleClick={(e) => e.stopPropagation()}
|
||
>
|
||
{indeterminate ? <Icon name="minus" size={10} /> : checked ? <Icon name="check" size={10} /> : null}
|
||
</span>
|
||
);
|
||
}
|
||
|
||
/** Inclusion state of every change (user overrides over the change list defaults). */
|
||
function useCommitChecks() {
|
||
const status = useVcs((s) => s.status);
|
||
const root = useVcs((s) => s.root) ?? "";
|
||
const userChecks = useCommit((s) => s.checks);
|
||
const lists = useChangelists((s) => s.state);
|
||
const staging = useStagingArea();
|
||
return useMemo(
|
||
() => inclusionChecks(status, root, lists, userChecks, staging),
|
||
[status, root, lists, userChecks, staging],
|
||
);
|
||
}
|
||
|
||
/** Number of changes the commit would include. */
|
||
export function useIncludedCount(): number {
|
||
const status = useVcs((s) => s.status);
|
||
const checks = useCommitChecks();
|
||
return useMemo(() => planCommit(status, checks).included.length, [status, checks]);
|
||
}
|
||
|
||
/**
|
||
* The Commit tool window's Commit tab: changes with inclusion checkboxes above the
|
||
* message, with an optional embedded preview of the selected file's diff beside them
|
||
* (issue #1599).
|
||
*/
|
||
function ChangesPanel() {
|
||
const included = useIncludedCount();
|
||
const preview = useCommit((s) => s.commitDiffPreview);
|
||
const path = useCommit((s) => (s.selection.length === 1 ? s.selection[0] : null));
|
||
const hasChanges = useVcs((s) => s.status.length > 0);
|
||
return (
|
||
<SplitGroup
|
||
orientation="horizontal"
|
||
className={styles.split}
|
||
storageKey={preview ? "openidea.vcs.commitPreviewSplit" : undefined}
|
||
>
|
||
<SplitPanel id="commit" defaultSize="62%" minSize="20%">
|
||
<SplitGroup orientation="vertical" className={styles.split} storageKey="openidea.vcs.commitMessageSplit">
|
||
<SplitPanel id="changes" defaultSize="62%" minSize="20%">
|
||
<ChangesTree mode="commit" />
|
||
</SplitPanel>
|
||
<SplitHandle />
|
||
<SplitPanel id="message" defaultSize="38%" minSize="120px">
|
||
<CommitPanel included={included} />
|
||
</SplitPanel>
|
||
</SplitGroup>
|
||
</SplitPanel>
|
||
{preview && (
|
||
<>
|
||
<SplitHandle />
|
||
<SplitPanel id="preview" defaultSize="38%" minSize="120px">
|
||
{path ? (
|
||
<DiffEditor input={diffInput(workingDiffData(path))} />
|
||
) : (
|
||
<EmptyState>
|
||
<T>{hasChanges ? "Select a file to preview its changes" : "No changes to preview"}</T>
|
||
</EmptyState>
|
||
)}
|
||
</SplitPanel>
|
||
</>
|
||
)}
|
||
</SplitGroup>
|
||
);
|
||
}
|
||
|
||
/**
|
||
* The Git window's Local Changes tab: a diff preview beside the tree when
|
||
* bottom-docked, below it when side-docked (off via View Options › Show Diff Preview).
|
||
*/
|
||
export function LocalChangesView() {
|
||
const preview = useCommit((s) => s.diffPreview);
|
||
const path = useCommit((s) => (s.selection.length === 1 ? s.selection[0] : null));
|
||
const anchor = useToolWindowLayout((s) => s.placements.git?.anchor ?? "bottom");
|
||
return (
|
||
<SplitGroup
|
||
orientation={anchor === "bottom" ? "horizontal" : "vertical"}
|
||
className={styles.split}
|
||
storageKey={
|
||
preview ? `openidea.vcs.localChangesSplit.${anchor === "bottom" ? "horizontal" : "vertical"}` : undefined
|
||
}
|
||
>
|
||
<SplitPanel id="changes" defaultSize="50%" minSize="20%">
|
||
<ChangesTree mode="local" />
|
||
</SplitPanel>
|
||
{preview && (
|
||
<>
|
||
<SplitHandle />
|
||
<SplitPanel id="preview" defaultSize="50%" minSize="120px">
|
||
{path ? (
|
||
<DiffEditor input={diffInput(workingDiffData(path))} />
|
||
) : (
|
||
<EmptyState>
|
||
<T>Select a file to preview its changes</T>
|
||
</EmptyState>
|
||
)}
|
||
</SplitPanel>
|
||
</>
|
||
)}
|
||
</SplitGroup>
|
||
);
|
||
}
|
||
|
||
/**
|
||
* The change tree with its toolbar. `commit`: the Commit tool window's (inclusion
|
||
* checkboxes); `local`: the Git tool window's Local Changes tab of the modal commit
|
||
* interface (no checkboxes, a Commit… button); `dialog`: the Commit dialog's.
|
||
*/
|
||
export function ChangesTree({ mode }: { mode: "commit" | "local" | "dialog" }) {
|
||
const status = useVcs((s) => s.status);
|
||
const root = useVcs((s) => s.root) ?? "";
|
||
const groupByDir = useCommit((s) => s.groupByDir);
|
||
// With the staging area the Commit tool window shows the index: Staged / Unstaged, no lists or checkboxes.
|
||
const staging = useStagingArea() && mode === "commit";
|
||
const groupByStaging = useCommit((s) => s.groupByStaging) || staging;
|
||
const groupByRepo = useCommit((s) => s.groupByRepo);
|
||
const showUnversionedSeparately = useCommit((s) => s.showUnversionedSeparately);
|
||
const diffPreview = useCommit((s) => s.diffPreview);
|
||
const commitDiffPreview = useCommit((s) => s.commitDiffPreview);
|
||
const doubleClickOpensDiff = useWorkflowSettings((s) => s.vcs.commit.doubleClickOpensDiff);
|
||
const changelists = useChangelists((s) => s.state);
|
||
const lists = staging ? undefined : changelists;
|
||
const checks = useCommitChecks();
|
||
const checkboxes = mode !== "local" && !staging;
|
||
const [dropTarget, setDropTarget] = useState<string | null>(null);
|
||
// IDEA starts with Unversioned Files collapsed.
|
||
const [collapsed, setCollapsed] = useState<Set<string>>(() => new Set(["group:unversioned"]));
|
||
const [selected, setSelected] = useState<Set<string>>(() => new Set());
|
||
const treeRef = useRef<TreeHandle>(null);
|
||
const viewRef = useRef<HTMLButtonElement>(null);
|
||
|
||
const repos = useVcs((s) => s.roots);
|
||
const heads = useVcs((s) => s.heads);
|
||
const projectRoots = useEffectiveRoots();
|
||
const roots = useMemo(
|
||
() =>
|
||
buildChangeTree(
|
||
status,
|
||
root,
|
||
groupByDir,
|
||
lists,
|
||
repos,
|
||
groupByStaging,
|
||
heads,
|
||
groupByRepo,
|
||
showUnversionedSeparately,
|
||
),
|
||
[status, root, groupByDir, lists, repos, groupByStaging, heads, groupByRepo, showUnversionedSeparately],
|
||
);
|
||
const [byKey, parents] = useMemo(() => {
|
||
const m = new Map<string, ChangeNode>();
|
||
const up = new Map<string, string>();
|
||
const walk = (ns: ChangeNode[], parent?: string) =>
|
||
ns.forEach((n) => {
|
||
m.set(n.key, n);
|
||
if (parent) up.set(n.key, parent);
|
||
if (n.children) walk(n.children, n.key);
|
||
});
|
||
walk(roots);
|
||
return [m, up] as const;
|
||
}, [roots]);
|
||
|
||
const [nodeToReveal, setNodeToReveal] = useState<{ key: string } | null>(null);
|
||
const revealNode = useCallback(
|
||
(node: ChangeNode) => {
|
||
const ancestors = new Set<string>();
|
||
for (let key = parents.get(node.key); key; key = parents.get(key)) ancestors.add(key);
|
||
setCollapsed((current) => new Set([...current].filter((key) => !ancestors.has(key))));
|
||
setNodeToReveal({ key: node.key });
|
||
},
|
||
[parents],
|
||
);
|
||
|
||
const followedDiff = useRef<string | null>(null);
|
||
useEffect(() => {
|
||
const editor = maybeEditorService();
|
||
const followDiff = (input: EditorInput | null) => {
|
||
if (input?.kind !== DIFF_KIND) {
|
||
followedDiff.current = null;
|
||
return;
|
||
}
|
||
if (input.key === followedDiff.current) return;
|
||
const node = [...byKey.values()].find((node) => {
|
||
if (node.type !== "file" || node.path !== input.path) return false;
|
||
return diffInput(changeNodeDiff(node)).key === input.key;
|
||
});
|
||
if (node) {
|
||
followedDiff.current = input.key;
|
||
revealNode(node);
|
||
} else {
|
||
followedDiff.current = null;
|
||
}
|
||
};
|
||
followDiff(editor?.activeInput?.() ?? null);
|
||
return editor?.onActiveInputChange?.(followDiff);
|
||
}, [byKey, revealNode]);
|
||
|
||
// While the active tab shows one of this tree's diffs, a single selected file replaces it,
|
||
// keeping focus in the tree (a click or arrow key walks the changes).
|
||
const followSelection = (nodes: readonly ChangeNode[]) => {
|
||
const node = nodes.length === 1 ? nodes[0] : undefined;
|
||
if (mode === "dialog" || node?.type !== "file" || !node.path) return;
|
||
const active = maybeEditorService()?.activeInput?.();
|
||
if (active?.kind !== DIFF_KIND || active.key !== followedDiff.current) return;
|
||
if (diffInput(changeNodeDiff(node)).key === active.key) return;
|
||
void openDiff(changeTreeDiffData(roots, node), { preview: true, focus: false });
|
||
};
|
||
|
||
// Reveal only after the ancestors have expanded, without moving keyboard focus.
|
||
useLayoutEffect(() => {
|
||
if (nodeToReveal) treeRef.current?.reveal(nodeToReveal.key);
|
||
}, [nodeToReveal]);
|
||
|
||
const selectOpened = () => {
|
||
const path = editorService().activePath();
|
||
const node = path ? [...byKey.values()].find((node) => node.type === "file" && node.path === path) : undefined;
|
||
if (node) revealNode(node);
|
||
};
|
||
const expanded = useMemo(() => {
|
||
const s = new Set<string>();
|
||
for (const k of byKey.keys()) if (!collapsed.has(k)) s.add(k);
|
||
return s;
|
||
}, [byKey, collapsed]);
|
||
|
||
// Forget overrides for files that are no longer changed.
|
||
useEffect(() => {
|
||
const live = new Set(status.map((f) => f.path));
|
||
const cur = useCommit.getState().checks;
|
||
if ([...cur.keys()].some((k) => !live.has(k))) {
|
||
useCommit.setState({ checks: new Map([...cur].filter(([k]) => live.has(k))) });
|
||
}
|
||
}, [status]);
|
||
|
||
const toggle = useCallback((nodes: ChangeNode[]) => {
|
||
const files = filesOf(nodes);
|
||
const { status: st, root: r } = useVcs.getState();
|
||
const cur = withListDefaults(st, r ?? "", useChangelists.getState().state, useCommit.getState().checks);
|
||
const agg = aggregateCheck(files, cur);
|
||
const target = agg.on === files.length && !agg.indeterminate ? "none" : "all";
|
||
const next = new Map(cur);
|
||
for (const f of files) next.set(f.path, files.length === 1 ? toggledCheck(checkOf(f, cur)) : target);
|
||
useCommit.setState({ checks: next });
|
||
}, []);
|
||
|
||
const selectedNodes = () => [...selected].map((k) => byKey.get(k)).filter((n): n is ChangeNode => !!n);
|
||
|
||
return (
|
||
<div className={styles.changes}>
|
||
<div className={clsx(styles.toolbar, styles.changesToolbar)} role="toolbar" aria-label={tx("Changes")}>
|
||
<div className={styles.changesTools}>
|
||
<IconButton icon="refresh" title="Refresh" size="small" onClick={() => void refreshNow()} />
|
||
{mode === "local" && <IconButton icon="commit" action="vcs.commit" size="small" />}
|
||
<IconButton icon="undo" action="vcs.rollback" size="small" />
|
||
{mode === "local" && <IconButton icon="changelist" action="vcs.changelist.new" size="small" />}
|
||
{mode !== "dialog" && <IconButton icon="git-pull" action="vcs.update" size="small" />}
|
||
<IconButton
|
||
ref={viewRef}
|
||
icon="eye"
|
||
title="View Options"
|
||
size="small"
|
||
aria-haspopup="menu"
|
||
onClick={() =>
|
||
viewRef.current &&
|
||
showContextMenu(bottomLeft(viewRef.current), [
|
||
{ kind: "header", label: "Group By" },
|
||
{ label: "Directory", checked: groupByDir, run: () => setGroupByDir(!groupByDir) },
|
||
// IDEA offers it once the project has several repositories.
|
||
...(repos.length > 1
|
||
? [{ label: "Repository", checked: groupByRepo, run: () => setGroupByRepo(!groupByRepo) }]
|
||
: []),
|
||
...(staging
|
||
? []
|
||
: [
|
||
{
|
||
label: "Staging State",
|
||
checked: groupByStaging,
|
||
run: () => setGroupByStaging(!groupByStaging),
|
||
},
|
||
]),
|
||
{ kind: "separator" },
|
||
{
|
||
label: "Show Unversioned Files Separately",
|
||
checked: showUnversionedSeparately,
|
||
run: () => setShowUnversionedSeparately(!showUnversionedSeparately),
|
||
},
|
||
...(mode === "local"
|
||
? [
|
||
{ kind: "separator" } as const,
|
||
{ label: "Show Diff Preview", checked: diffPreview, run: () => setDiffPreview(!diffPreview) },
|
||
]
|
||
: []),
|
||
// Not in the Commit dialog, whose files always open as source.
|
||
...(mode === "dialog" ? [] : doubleClickMenu(doubleClickOpensDiff)),
|
||
])
|
||
}
|
||
/>
|
||
{/* WebStorm's Preview Diff: an embedded diff of the selection beside the changes (#1599). */}
|
||
{mode === "commit" && (
|
||
<IconButton
|
||
icon="editor-preview"
|
||
title="Preview Diff"
|
||
size="small"
|
||
pressed={commitDiffPreview}
|
||
onClick={() => setCommitDiffPreview(!commitDiffPreview)}
|
||
/>
|
||
)}
|
||
</div>
|
||
<div className={styles.changesTools}>
|
||
{mode !== "dialog" && (
|
||
<IconButton icon="locate" title="Select Opened File" size="small" onClick={selectOpened} />
|
||
)}
|
||
<IconButton icon="expand-all" title="Expand All" size="small" onClick={() => setCollapsed(new Set())} />
|
||
<IconButton
|
||
icon="collapse-all"
|
||
title="Collapse All"
|
||
size="small"
|
||
onClick={() =>
|
||
setCollapsed(new Set([...byKey.values()].filter((n) => n.type !== "file").map((n) => n.key)))
|
||
}
|
||
/>
|
||
</div>
|
||
</div>
|
||
{mode !== "dialog" && <OperationBanner />}
|
||
<div
|
||
className={styles.treeWrap}
|
||
data-changes-tree={mode}
|
||
onKeyDownCapture={(e) => {
|
||
if (checkboxes && e.key === " " && !e.metaKey && !e.ctrlKey && selected.size) {
|
||
e.preventDefault();
|
||
e.stopPropagation();
|
||
toggle(selectedNodes());
|
||
} else if ((e.key === "Delete" || e.key === "Backspace") && !e.metaKey) {
|
||
const nodes = selectedNodes();
|
||
const group = nodes.find((x) => x.type === "group" && x.changelist);
|
||
const deletable = group ? [] : deletableFiles(nodes);
|
||
if (group?.changelist) {
|
||
e.preventDefault();
|
||
e.stopPropagation();
|
||
void executeAction("vcs.changelist.delete", group.changelist);
|
||
} else if (deletable.length) {
|
||
e.preventDefault();
|
||
e.stopPropagation();
|
||
void deleteFiles(deletable);
|
||
}
|
||
} else {
|
||
// IDEA: Ctrl+C copies the file names, Ctrl+Shift+C the absolute paths (#1877).
|
||
const stroke = strokeOf(e.nativeEvent);
|
||
const copy = stroke && normalizeBinding(macStroke(stroke));
|
||
if (copy === "Meta+C" || copy === "Shift+Meta+C") {
|
||
e.preventDefault();
|
||
e.stopPropagation();
|
||
copyChangePaths(selectedNodes(), copy === "Meta+C" ? "name" : "absolute");
|
||
}
|
||
}
|
||
}}
|
||
>
|
||
<Tree<ChangeNode>
|
||
handleRef={treeRef}
|
||
roots={roots}
|
||
getKey={(n) => n.key}
|
||
getLabel={nodeText}
|
||
getDirectory={(n) => (n.path && n.type !== "group" ? (n.type === "dir" ? n.path : dirname(n.path)) : null)}
|
||
getChildren={(n) => n.children}
|
||
rowClassName={(n) =>
|
||
n.type !== "group" && n.path && isTestFile(n.path, projectRoots) ? styles.testFile : undefined
|
||
}
|
||
isExpandable={(n) => n.type !== "file"}
|
||
expanded={expanded}
|
||
onExpandedChange={(_next, node, open) =>
|
||
setCollapsed((c) => {
|
||
const s = new Set(c);
|
||
if (open) s.delete(node.key);
|
||
else s.add(node.key);
|
||
return s;
|
||
})
|
||
}
|
||
selected={selected}
|
||
onSelectionChange={(sel) => {
|
||
setSelected(sel);
|
||
const nodes = [...sel].map((k) => byKey.get(k)).filter((n): n is ChangeNode => !!n);
|
||
useCommit.setState({
|
||
selection: filesOf(nodes).map((f) => f.path),
|
||
selectionKeys: [...sel],
|
||
selectedList: nodes.find((n) => n.changelist)?.changelist ?? null,
|
||
});
|
||
followSelection(nodes);
|
||
}}
|
||
onActivate={(n) => openNode(n, roots, mode === "dialog")}
|
||
onJumpToSource={async (n) => {
|
||
if (n.type === "group") {
|
||
const files = n.files.filter((f) => onDisk(f) && !f.path.endsWith("/"));
|
||
for (const [i, file] of files.entries()) {
|
||
await editorService().openFile(file.path, { focus: i === files.length - 1 });
|
||
}
|
||
return;
|
||
}
|
||
const file = n.type === "file" ? n : selectedNodes().find((x) => x.type === "file");
|
||
if (file?.path && file.kind !== "deleted") void editorService().openFile(file.path, { focus: true });
|
||
}}
|
||
onContextMenu={(node, sel, e: MouseEvent) => {
|
||
if (!node) return;
|
||
if (node.type === "group" && node.changelist) {
|
||
showContextMenu(e, listMenu(node.changelist, !!node.active, node.files));
|
||
return;
|
||
}
|
||
const nodes = [...sel].map((k) => byKey.get(k)).filter((n): n is ChangeNode => !!n);
|
||
showContextMenu(e, nodeMenu(nodes.length ? nodes : [node], repos, { roots }));
|
||
}}
|
||
aria-label="Changes"
|
||
empty={
|
||
<span className={styles.empty}>
|
||
<T>No local changes</T>
|
||
</span>
|
||
}
|
||
renderNode={(n, st) => {
|
||
const agg = aggregateCheck(n.files, checks);
|
||
const label = (
|
||
<span
|
||
className={clsx(
|
||
styles.label,
|
||
n.type === "group" && styles.groupLabel,
|
||
n.type === "group" && n.changelist && !n.active && styles.inactiveList,
|
||
)}
|
||
style={n.type === "file" && n.kind ? { color: KIND_COLOR[n.kind] } : undefined}
|
||
{...(n.type === "file" && n.kind && tooltipAttrs(KIND_LABEL[n.kind]))}
|
||
>
|
||
{nodeText(n)}
|
||
</span>
|
||
);
|
||
// Pointer-driven (HTML5 drag and drop never reaches the Tauri webview):
|
||
// file and folder rows drag (unversioned ones onto a changelist, which
|
||
// adds them to Git); any row of a changelist takes dropped files into it.
|
||
const list = n.changelist;
|
||
const dnd = {
|
||
...(list !== undefined && {
|
||
ref: (el: HTMLSpanElement | null) =>
|
||
el
|
||
? registerDropTarget(el, {
|
||
accepts: (type) => type === CHANGES_MIME,
|
||
over: () => setDropTarget(list),
|
||
leave: () => setDropTarget(null),
|
||
drop: (data) => {
|
||
setDropTarget(null);
|
||
void addToChangelist(data.payload.split("\n"), list);
|
||
},
|
||
})
|
||
: undefined,
|
||
}),
|
||
...(n.type !== "group" &&
|
||
n.files.length > 0 && {
|
||
onPointerDown: (e: PointerEvent<HTMLSpanElement>) => {
|
||
const paths =
|
||
selected.has(n.key) && selected.size > 1
|
||
? filesOf(selectedNodes()).map((f) => f.path)
|
||
: n.files.map((f) => f.path);
|
||
startPointerDrag(e, e.currentTarget, {
|
||
type: CHANGES_MIME,
|
||
payload: paths.map(stripSlash).join("\n"),
|
||
});
|
||
},
|
||
}),
|
||
};
|
||
return (
|
||
<span
|
||
className={clsx(styles.nodeRow, n.type === "group" && dropTarget === n.changelist && styles.dropTarget)}
|
||
{...dnd}
|
||
>
|
||
{checkboxes && (
|
||
<CheckMark
|
||
checked={agg.checked}
|
||
indeterminate={agg.indeterminate}
|
||
label={tx`Include ${n.label} in commit`}
|
||
onToggle={() => toggle(selected.has(n.key) && selected.size > 1 ? selectedNodes() : [n])}
|
||
/>
|
||
)}
|
||
{n.type === "group" ? null : (
|
||
<Icon
|
||
name={n.type === "dir" ? (st.expanded ? "folder-open" : "folder") : fileIconName(n.label)}
|
||
size={16}
|
||
className={styles.icon}
|
||
/>
|
||
)}
|
||
{label}
|
||
{n.type === "group" && <span className={styles.detail}>{fileCount(n.files.length)}</span>}
|
||
{!staging && n.type === "file" && n.files[0] && isPartiallyStaged(n.files[0]) && (
|
||
<span className={styles.partial} title={tx("Partially staged: only staged hunks are committed")}>
|
||
<T>partial</T>
|
||
</span>
|
||
)}
|
||
{n.detail && n.detailIcon && <Icon name={n.detailIcon} size={14} className={styles.detailIcon} />}
|
||
{n.detail && <span className={styles.detail}>{n.detail}</span>}
|
||
{staging && <StageButton node={n} />}
|
||
</span>
|
||
);
|
||
}}
|
||
/>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const MISSING_BLANK_LINE = "Missing blank line between subject and body";
|
||
|
||
/** The quick fix for {@link MISSING_BLANK_LINE}: an empty line after the subject. */
|
||
export function addBlankLineAfterSubject(message: string): string {
|
||
const end = message.indexOf("\n");
|
||
return end < 0 ? message : `${message.slice(0, end)}\n${message.slice(end)}`;
|
||
}
|
||
|
||
/** Commit message inspections (Settings › Version Control › Commit). */
|
||
export function messageProblems(
|
||
message: string,
|
||
c: {
|
||
blankLineAfterSubject: boolean;
|
||
limitSubject: boolean;
|
||
subjectLimit: number;
|
||
limitBody: boolean;
|
||
bodyLimit: number;
|
||
},
|
||
): string[] {
|
||
const lines = message.split("\n");
|
||
const out: string[] = [];
|
||
if (c.limitSubject && (lines[0]?.length ?? 0) > c.subjectLimit)
|
||
out.push(`Subject line is longer than ${c.subjectLimit} characters`);
|
||
if (c.blankLineAfterSubject && lines.length > 1 && lines[1]!.trim() !== "") out.push(MISSING_BLANK_LINE);
|
||
if (c.limitBody) {
|
||
const long = lines.slice(1).findIndex((l) => l.length > c.bodyLimit);
|
||
if (long >= 0) out.push(`Body line ${long + 2} is longer than ${c.bodyLimit} characters`);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
/** "Wrap when typing reaches right margin": breaks the caret line at the last space before `limit`. */
|
||
export function wrapAtMargin(text: string, caret: number, limit: number): { text: string; caret: number } | null {
|
||
const start = text.lastIndexOf("\n", caret - 1) + 1;
|
||
const end = text.indexOf("\n", caret);
|
||
const line = text.slice(start, end < 0 ? text.length : end);
|
||
if (start === 0 || line.length <= limit) return null; // the subject line is never wrapped
|
||
const cut = line.lastIndexOf(" ", limit);
|
||
if (cut <= 0) return null;
|
||
const at = start + cut;
|
||
return { text: `${text.slice(0, at)}\n${text.slice(at + 1)}`, caret };
|
||
}
|
||
|
||
export interface CommitRunner {
|
||
busy: boolean;
|
||
/** Commit was asked for without a message: reported at the field. */
|
||
emptyError: boolean;
|
||
clearEmptyError: () => void;
|
||
/** Whether the Commit buttons are enabled. */
|
||
canCommit: boolean;
|
||
run: (push: boolean, rebaseFixup?: boolean) => Promise<void>;
|
||
}
|
||
|
||
/**
|
||
* Commit / Commit and Push… of the Commit tool window and the Commit dialog: the
|
||
* shared pipeline (`commitChanges`), then the push. `onCommitted` runs after a commit.
|
||
*/
|
||
export function useCommitRunner(included: number, onCommitted?: () => void): CommitRunner {
|
||
const amend = useCommit((s) => s.amend);
|
||
const merging = useVcs((s) => s.branch?.operation === "merge");
|
||
const [emptyError, setEmptyError] = useState(false);
|
||
const [busy, setBusy] = useState(false);
|
||
const run = async (push: boolean, rebaseFixup = false) => {
|
||
if (busy) return;
|
||
// IDEA keeps Commit enabled and reports an empty message at the field.
|
||
if (!useCommit.getState().message.trim() && !useCommit.getState().amend) {
|
||
setEmptyError(true);
|
||
useCommit.setState((s) => ({ focusTick: s.focusTick + 1 }));
|
||
return;
|
||
}
|
||
setBusy(true);
|
||
try {
|
||
const fixup = rebaseFixup ? pendingFixup(useCommit.getState().message, useCommit.getState().fixupTarget) : null;
|
||
const id = await commitChanges();
|
||
if (id === undefined) return;
|
||
onCommitted?.();
|
||
if (fixup) void rebaseAfterFixup(fixup);
|
||
else if (push) void pushAfterCommit();
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
};
|
||
return {
|
||
busy,
|
||
emptyError,
|
||
clearEmptyError: () => setEmptyError(false),
|
||
canCommit: (included > 0 || amend || merging) && !busy,
|
||
run,
|
||
};
|
||
}
|
||
|
||
/** Amend, message history and the message field with its inspections and failed checks. */
|
||
export function CommitMessageField({
|
||
included,
|
||
runner,
|
||
autoFocus = false,
|
||
}: {
|
||
included: number;
|
||
runner: CommitRunner;
|
||
/** The message takes the initial focus of the dialog it is in (the Commit dialog). */
|
||
autoFocus?: boolean;
|
||
}) {
|
||
const message = useCommit((s) => s.message);
|
||
const amend = useCommit((s) => s.amend);
|
||
const focusTick = useCommit((s) => s.focusTick);
|
||
const ref = useRef<HTMLTextAreaElement>(null);
|
||
const barRef = useRef<HTMLDivElement>(null);
|
||
const [historyOpen, setHistoryOpen] = useState(false);
|
||
const historyTick = useCommit((s) => s.historyTick);
|
||
const checkProblems = useCommit((s) => s.checkProblems);
|
||
const { emptyError } = runner;
|
||
|
||
// The failed checks concern what was about to be committed: editing it dismisses them.
|
||
useEffect(() => {
|
||
if (useCommit.getState().checkProblems.length) useCommit.setState({ checkProblems: [] });
|
||
}, [message, amend, included]);
|
||
|
||
useEffect(() => {
|
||
if (focusTick) ref.current?.focus();
|
||
}, [focusTick]);
|
||
// Show Commit Message History (⌃M) asks through the store.
|
||
const [seenHistoryTick, setSeenHistoryTick] = useState(historyTick);
|
||
if (historyTick !== seenHistoryTick) {
|
||
setSeenHistoryTick(historyTick);
|
||
if (messageHistory().length) setHistoryOpen(true);
|
||
}
|
||
|
||
const cfg = useWorkflowSettings((s) => s.vcs.commit);
|
||
const editor = useProject((s) => s.settings?.editor);
|
||
const { family, size } = resolveMonoFont(undefined, editor);
|
||
const first = message.split("\n")[0] ?? "";
|
||
const tooLong = cfg.limitSubject && first.length > cfg.subjectLimit;
|
||
const problems = messageProblems(message, cfg).filter((p) => !p.startsWith("Subject line"));
|
||
const margin = cfg.limitBody ? cfg.bodyLimit : cfg.subjectLimit;
|
||
const showMargin = cfg.showRightMargin && (cfg.limitBody || cfg.limitSubject);
|
||
const history = messageHistory();
|
||
|
||
return (
|
||
<>
|
||
<div ref={barRef} className={styles.commitBar}>
|
||
<Checkbox checked={amend} onChange={(v) => void setAmend(v)} label="Amend" />
|
||
<IconButton
|
||
icon="history"
|
||
title="Commit Message History"
|
||
size="small"
|
||
disabled={!history.length}
|
||
aria-haspopup="menu"
|
||
aria-expanded={historyOpen}
|
||
onClick={() => setHistoryOpen((o) => !o)}
|
||
/>
|
||
<Menu
|
||
open={historyOpen}
|
||
onClose={() => setHistoryOpen(false)}
|
||
anchor={barRef}
|
||
placement="top-start"
|
||
iconColumn={false}
|
||
aria-label="Recent commit messages"
|
||
items={history.map((m, i) => ({
|
||
id: `h${i}`,
|
||
label: m.split("\n")[0]!.slice(0, 80),
|
||
run: () => {
|
||
useCommit.setState({ message: m });
|
||
ref.current?.focus();
|
||
},
|
||
}))}
|
||
/>
|
||
<CommitMessageActions />
|
||
<span className={styles.grow} />
|
||
{tooLong && (
|
||
<span className={clsx(styles.lineLen, styles.lineLenWarn)} title={tx("Subject line length")}>
|
||
{first.length}/{cfg.subjectLimit}
|
||
</span>
|
||
)}
|
||
</div>
|
||
<textarea
|
||
ref={ref}
|
||
data-autofocus={autoFocus ? "" : undefined}
|
||
className={clsx(styles.message, !showMargin && styles.noMargin, emptyError && styles.messageError)}
|
||
aria-invalid={emptyError || undefined}
|
||
style={{
|
||
["--guide" as string]: `${margin}ch`,
|
||
fontFamily: family,
|
||
fontSize: size,
|
||
fontWeight: fontWeightOf(editor?.fontWeight, 400),
|
||
lineHeight: editor?.lineHeight ? `${editor.lineHeight}px` : undefined,
|
||
}}
|
||
value={message}
|
||
placeholder={tx("Commit Message")}
|
||
aria-label={tx("Commit message")}
|
||
spellCheck
|
||
onChange={(e) => {
|
||
const el = e.target;
|
||
const wrapped =
|
||
cfg.limitBody && cfg.wrapAtMargin ? wrapAtMargin(el.value, el.selectionStart, cfg.bodyLimit) : null;
|
||
useCommit.setState({ message: wrapped?.text ?? el.value });
|
||
runner.clearEmptyError();
|
||
if (wrapped) requestAnimationFrame(() => el.setSelectionRange(wrapped.caret, wrapped.caret));
|
||
}}
|
||
onKeyDown={(e) => {
|
||
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
|
||
e.preventDefault();
|
||
void runner.run(e.altKey);
|
||
} else if (e.key === "Escape") {
|
||
e.currentTarget.blur();
|
||
}
|
||
}}
|
||
/>
|
||
{emptyError && (
|
||
<div className={clsx(styles.inspections, styles.inspectionError)} role="alert">
|
||
<T>Specify commit message</T>
|
||
</div>
|
||
)}
|
||
{problems.length > 0 && (
|
||
<div className={styles.inspections} role="status">
|
||
{problems.map((p, i) => (
|
||
<span key={p}>
|
||
{i > 0 && " · "}
|
||
{txTemplate(p)}
|
||
{p === MISSING_BLANK_LINE && (
|
||
<button
|
||
type="button"
|
||
className={styles.inspectionFix}
|
||
onClick={() => {
|
||
const el = ref.current;
|
||
const caret = el ? el.selectionStart + (el.selectionStart > first.length ? 1 : 0) : 0;
|
||
useCommit.setState({ message: addBlankLineAfterSubject(message) });
|
||
requestAnimationFrame(() => {
|
||
el?.focus();
|
||
el?.setSelectionRange(caret, caret);
|
||
});
|
||
}}
|
||
>
|
||
<T>Add blank line</T>
|
||
</button>
|
||
)}
|
||
</span>
|
||
))}
|
||
</div>
|
||
)}
|
||
{checkProblems.length > 0 && (
|
||
<div className={styles.checkProblems} role="alert">
|
||
<Icon name="warning" size={14} className={styles.checkProblemsIcon} />
|
||
<div className={styles.checkProblemsList}>
|
||
{checkProblems.map((p, i) => (
|
||
<div key={i}>
|
||
<span className={styles.checkProblemTitle}>{tx(p.title)}</span>
|
||
{typeof p.text === "string" ? `: ${tx(p.text)}` : p.text}
|
||
{p.details && (
|
||
<>
|
||
{" "}
|
||
<button type="button" className={styles.checkProblemLink} onClick={p.details.run}>
|
||
{tx(p.details.label)}
|
||
</button>
|
||
</>
|
||
)}
|
||
</div>
|
||
))}
|
||
</div>
|
||
</div>
|
||
)}
|
||
</>
|
||
);
|
||
}
|
||
|
||
/** Actions contributed above the message (a plugin's `commit.message` menu items). */
|
||
function CommitMessageActions() {
|
||
const items = useMenuSlotItems((s) => s.slots.commitMessage);
|
||
useContextKeys();
|
||
return (
|
||
<>
|
||
{(items ?? [])
|
||
.filter((i) => evaluateWhen(i.when ?? undefined))
|
||
.map((i) => (
|
||
<IconButton key={i.action} icon={i.icon ?? "plugin"} action={i.action} actionArg={i.arg} size="small" />
|
||
))}
|
||
</>
|
||
);
|
||
}
|
||
|
||
/** Label of the Commit button. */
|
||
export function commitLabel(amend: boolean, problems: boolean, all = false): string {
|
||
return problems ? "Commit Anyway" : amend ? "Amend Commit" : all ? "Commit All" : "Commit";
|
||
}
|
||
|
||
/** Label of the Commit and Push button. */
|
||
export const commitAndPushLabel = (amend: boolean) => (amend ? "Amend and Push…" : "Commit and Push…");
|
||
|
||
/** The message and the Commit buttons of the Commit tool window. */
|
||
function CommitPanel({ included }: { included: number }) {
|
||
const amend = useCommit((s) => s.amend);
|
||
const remoteBusy = useVcs((s) => s.remoteBusy);
|
||
const checkProblems = useCommit((s) => s.checkProblems);
|
||
const optionsRef = useRef<HTMLButtonElement>(null);
|
||
const [optionsOpen, setOptionsOpen] = useState(false);
|
||
const runner = useCommitRunner(included);
|
||
const { canCommit } = runner;
|
||
// After Fixup… / Squash Into…, the second button commits and autosquashes the target.
|
||
const fixup = useCommit((s) => pendingFixup(s.message, s.fixupTarget));
|
||
const staging = useStagingArea();
|
||
const commitAll = useVcs((s) => staging && isCommitAll(s.status));
|
||
|
||
return (
|
||
<div className={styles.commit}>
|
||
<CommitMessageField included={included} runner={runner} />
|
||
<div className={styles.commitActions}>
|
||
<Button variant="primary" disabled={!canCommit} onClick={() => void runner.run(false)} title="⌘↩">
|
||
<T>{commitLabel(amend, checkProblems.length > 0, commitAll)}</T>
|
||
</Button>
|
||
{fixup ? (
|
||
<Button
|
||
className={styles.pushButton}
|
||
disabled={!canCommit}
|
||
onClick={() => void runner.run(false, true)}
|
||
title="Commit and Rebase"
|
||
>
|
||
<span>
|
||
<T>Commit and Rebase</T>
|
||
</span>
|
||
</Button>
|
||
) : (
|
||
<Button
|
||
className={styles.pushButton}
|
||
disabled={!canCommit || !!remoteBusy}
|
||
onClick={() => void runner.run(true)}
|
||
title={`${tx(commitAndPushLabel(amend))} (⌥⌘↩)`}
|
||
>
|
||
<span>
|
||
<T>{commitAndPushLabel(amend)}</T>
|
||
</span>
|
||
</Button>
|
||
)}
|
||
<span className={styles.grow} />
|
||
<IconButton
|
||
ref={optionsRef}
|
||
icon="settings"
|
||
title="Commit Options"
|
||
size="small"
|
||
aria-haspopup="dialog"
|
||
aria-expanded={optionsOpen}
|
||
onClick={() => setOptionsOpen((o) => !o)}
|
||
/>
|
||
{optionsOpen && <CommitOptionsPopup anchor={optionsRef} onClose={() => setOptionsOpen(false)} />}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
|
||
type StashEntry = Stash & { repo: string | null };
|
||
type StashNode = { key: string; stash: StashEntry; change?: LogChange };
|
||
|
||
const stashKey = (s: StashEntry) => `${s.repo}:${s.index}:${s.id}`;
|
||
const STASH_PREVIEW_DELAY = 120;
|
||
|
||
/** A stash's changes against the commit it was made on, as Git Log changes (diffs, line counts). */
|
||
async function stashFileChanges(stash: StashEntry): Promise<LogChange[]> {
|
||
const files = await call(commands.vcsCommitFiles(stash.repo, stash.id));
|
||
const commit = { id: stash.id, shortId: stash.id.slice(0, 8), parents: stash.base ? [stash.base] : [] };
|
||
return toLogChanges(files, commit as unknown as Commit, stash.repo, true);
|
||
}
|
||
|
||
export function StashesPanel() {
|
||
const repo = useVcs((s) => s.repo);
|
||
const repos = useVcs((s) => s.roots);
|
||
const version = useVcs((s) => s.version);
|
||
const [tick, setTick] = useState(0);
|
||
const [selectedKey, setSelectedKey] = useState<string | null>(null);
|
||
const request = useMemo(() => ({ repos, repo, version, tick }), [repos, repo, version, tick]);
|
||
const [result, setResult] = useState<{
|
||
request: typeof request;
|
||
stashes: StashEntry[];
|
||
error: string | null;
|
||
} | null>(null);
|
||
const [files, setFiles] = useState<ReadonlyMap<string, LogChange[]>>(new Map());
|
||
const previewTimer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
||
const loaded = result?.request === request;
|
||
const stashes = loaded ? result.stashes : [];
|
||
const error = loaded ? result.error : null;
|
||
useEffect(() => {
|
||
let live = true;
|
||
void Promise.all(
|
||
(request.repos.length ? request.repos : [request.repo]).map(async (root) =>
|
||
(await call(commands.vcsStashes(root))).map((s) => ({ ...s, repo: root })),
|
||
),
|
||
)
|
||
.then((lists) => {
|
||
if (live) setResult({ request, stashes: lists.flat(), error: null });
|
||
})
|
||
.catch((e) => {
|
||
if (live) setResult({ request, stashes: [], error: String(e) });
|
||
});
|
||
return () => {
|
||
live = false;
|
||
};
|
||
}, [request]);
|
||
useEffect(() => () => clearTimeout(previewTimer.current), []);
|
||
const roots = useMemo<StashNode[]>(() => stashes.map((stash) => ({ key: stashKey(stash), stash })), [stashes]);
|
||
const childrenOf = (node: StashNode): StashNode[] | undefined =>
|
||
node.change
|
||
? undefined
|
||
: files.get(node.key)?.map((change) => ({ key: `${node.key}\u0000${change.path}`, stash: node.stash, change }));
|
||
const loadFiles = async (node: StashNode) => {
|
||
if (files.has(node.key)) return;
|
||
const changes = await stashFileChanges(node.stash).catch(() => []);
|
||
setFiles((prev) => new Map(prev).set(node.key, changes));
|
||
};
|
||
const nodeOf = (key: string | null): StashNode | undefined => {
|
||
if (!key) return undefined;
|
||
const root = roots.find((r) => key === r.key || key.startsWith(`${r.key}\u0000`));
|
||
if (!root || key === root.key) return root;
|
||
return childrenOf(root)?.find((n) => n.key === key);
|
||
};
|
||
const selectedNode = nodeOf(selectedKey);
|
||
const current = selectedNode?.stash ?? stashes[0];
|
||
const showDiff = (node: StashNode, preview: boolean) => {
|
||
const chain = files.get(stashKey(node.stash)) ?? [];
|
||
if (node.change) void showChangesDiff([node.change], preview, chain).catch(() => undefined);
|
||
};
|
||
return (
|
||
<div className={styles.changes}>
|
||
<div className={styles.toolbar} role="toolbar" aria-label={tx("Stashes")}>
|
||
{repos.length > 1 && (
|
||
<Select
|
||
aria-label="Repository"
|
||
value={repo}
|
||
onChange={(root) => void selectRepo(root)}
|
||
options={repos.map((root) => ({ value: root, label: repoName(root) }))}
|
||
/>
|
||
)}
|
||
<IconButton
|
||
icon="refresh"
|
||
title="Refresh"
|
||
size="small"
|
||
onClick={() => {
|
||
setFiles(new Map());
|
||
setTick((n) => n + 1);
|
||
}}
|
||
/>
|
||
<IconButton icon="plus" title="Stash Changes…" size="small" onClick={() => void stashChanges()} />
|
||
<span className={styles.sep} />
|
||
<IconButton
|
||
icon="check"
|
||
title="Apply Stash"
|
||
size="small"
|
||
disabled={!current}
|
||
onClick={() => current && void applyStash(current.index, false, current.repo)}
|
||
/>
|
||
<IconButton
|
||
icon="undo"
|
||
title="Pop Stash (Apply and Drop)"
|
||
size="small"
|
||
disabled={!current}
|
||
onClick={() => current && void applyStash(current.index, true, current.repo)}
|
||
/>
|
||
<IconButton
|
||
icon="trash"
|
||
title="Drop Stash"
|
||
size="small"
|
||
disabled={!current}
|
||
onClick={() => current && void dropStash(current.index, current.message, current.repo)}
|
||
/>
|
||
</div>
|
||
<div className={styles.treeWrap}>
|
||
<Tree<StashNode>
|
||
roots={roots}
|
||
getKey={(n) => n.key}
|
||
getLabel={(n) => (n.change ? basename(n.change.path) : n.stash.message)}
|
||
getChildren={childrenOf}
|
||
isExpandable={(n) => !n.change}
|
||
loadChildren={loadFiles}
|
||
selected={useMemo(() => new Set(selectedKey ? [selectedKey] : []), [selectedKey])}
|
||
multiSelect={false}
|
||
rowHeight={22}
|
||
aria-label="Stashes"
|
||
className={styles.stashList}
|
||
empty={<span className={styles.empty}>{error ?? (loaded ? tx("No stashes") : tx("Loading…"))}</span>}
|
||
onSelectionChange={(sel) => {
|
||
const key = [...sel][0] ?? null;
|
||
setSelectedKey(key);
|
||
clearTimeout(previewTimer.current);
|
||
const node = nodeOf(key);
|
||
// With Show Diff Preview on, the selection walks a preview tab through the stash's files.
|
||
if (node?.change && useCommit.getState().diffPreview && !vcsDiffInFront())
|
||
previewTimer.current = setTimeout(() => showDiff(node, true), STASH_PREVIEW_DELAY);
|
||
}}
|
||
onActivate={(n) => showDiff(n, false)}
|
||
onContextMenu={(n, _sel, e) => {
|
||
if (!n) return;
|
||
e.preventDefault();
|
||
setSelectedKey(n.key);
|
||
const s = n.stash;
|
||
showContextMenu(e, [
|
||
...(n.change
|
||
? [
|
||
{ label: "Show Diff", icon: "diff", run: () => showDiff(n, false) } as MenuItem,
|
||
{ kind: "separator" } as MenuItem,
|
||
]
|
||
: []),
|
||
{ label: "Apply Stash", icon: "check", run: () => void applyStash(s.index, false, s.repo) },
|
||
{ label: "Pop Stash", icon: "undo", run: () => void applyStash(s.index, true, s.repo) },
|
||
{ label: "Unstash…", run: () => showUnstashDialog(s.index, s.repo) },
|
||
{ kind: "separator" },
|
||
{ label: "Drop Stash", icon: "trash", run: () => void dropStash(s.index, s.message, s.repo) },
|
||
]);
|
||
}}
|
||
renderNode={(n) => {
|
||
if (n.change) {
|
||
const kind = n.change.kind === "untracked" ? "added" : n.change.kind;
|
||
return (
|
||
<>
|
||
<Icon name={fileIconName(basename(n.change.path), n.change.path)} size={16} className={styles.icon} />
|
||
<span className={styles.label} style={{ color: KIND_COLOR[kind] }}>
|
||
{basename(n.change.path)}
|
||
</span>
|
||
<span className={styles.detail}>{dirname(n.change.path)}</span>
|
||
</>
|
||
);
|
||
}
|
||
const s = n.stash;
|
||
const { branch, text } = stashLabel(s.message);
|
||
return (
|
||
<span className={styles.stashRow} title={`stash@{${s.index}}: ${s.message}`}>
|
||
<span className={styles.stashMessage}>
|
||
{repos.length > 1 && s.repo ? `${repoName(s.repo)} · ` : ""}
|
||
{text}
|
||
</span>
|
||
{branch && <span className={clsx(styles.detail, styles.stashBranch)}>{branch}</span>}
|
||
<span className={clsx(styles.detail, styles.stashTime)}>{formatTime(s.timeMs)}</span>
|
||
</span>
|
||
);
|
||
}}
|
||
/>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|