Файли
openidea/src/features/vcs/FileRevisionChooser.tsx
T
Muuтаmuutot 83b9744a36 feat(vcs): annotate the file as of an earlier revision (#3265)
The annotation gutter's menu gains IDEA's annotate family:

- Annotate Previous Revision opens the file at the first parent of the annotated line's commit next to the commit's version, with the previous side annotated; on a file's first commit it says there is no previous revision.
- Annotate at Revision… opens the file's revision chooser and shows the picked revision annotated next to the working copy, following renames through historyPath.

One diff viewer provider annotates the left pane once per diff, so annotations the user closes stay closed when the panes rebuild. The diff opens at its first change, since the annotated line's number belongs to the working copy.

Co-authored-by: muutot <[email protected]>
2026-11-14 10:38:39 +08:00

174 рядки
5.2 KiB
TypeScript

import { useEffect, useState, type KeyboardEvent } from "react";
import type { Commit } from "@/bindings";
import { call, commands, nextRequestId } from "@/platform/ipc";
import { tx } from "@/platform/i18n";
import { Dialog, List, SearchField, Spinner, T, listNavigate } from "@/ui";
import { openDialog } from "./dialogs";
import { annotateAtRevision } from "./annotateRevision";
import { openVcsDiff } from "./diffInput";
import { hasFileHistory } from "./fileRevisions";
import { formatTime } from "./model";
import { repositoryPath } from "./repositoryPaths";
import { repoOf } from "./store";
import styles from "./FileRevisionChooser.module.css";
export function compareWithFileRevision(path: string | null | undefined) {
if (!path || !hasFileHistory(path)) return;
const repo = repoOf(path);
openDialog((close) => (
<FileRevisionChooser
path={path}
repo={repo}
onClose={close}
onChoose={(commit) => {
close();
if (repoOf(path) !== repo) return;
void openVcsDiff({
path,
left: {
type: "rev",
rev: commit.id,
label: commit.shortId,
path: commit.historyPath ? repositoryPath(repo, commit.historyPath) : path,
},
right: { type: "worktree" },
});
}}
/>
));
}
/** Annotate the file as of a revision the user picks: who last changed each line up to there. */
export function annotateFileRevision(path: string | null | undefined) {
// No `hasFileHistory` gate: the chooser lists the file's revisions itself, and a menu item
// that silently does nothing is worse than one that opens an empty list.
if (!path) return;
const repo = repoOf(path);
openDialog((close) => (
<FileRevisionChooser
path={path}
repo={repo}
onClose={close}
title={tx("Annotate at Revision…")}
onChoose={(commit) => {
close();
if (repoOf(path) !== repo) return;
void annotateAtRevision(path, commit);
}}
/>
));
}
function FileRevisionChooser({
path,
repo,
onClose,
onChoose,
title,
}: {
path: string;
repo: string | null;
onClose: () => void;
onChoose: (commit: Commit) => void;
/** "Compare with Revision…" unless the chooser asks for another sentence. */
title?: string;
}) {
const [commits, setCommits] = useState<Commit[] | null>(null);
const [error, setError] = useState<string | null>(null);
const [query, setQuery] = useState("");
const [active, setActive] = useState(0);
useEffect(() => {
let alive = true;
let pending = true;
const request = nextRequestId();
void call(
commands.vcsLog(repo, {
branch: null,
path,
author: null,
text: null,
skip: 0,
limit: 0,
follow: true,
summaryOnly: true,
request,
}),
)
.then((rows) => alive && setCommits(rows))
.catch((e) => alive && setError(String(e)))
.finally(() => {
pending = false;
});
return () => {
alive = false;
if (pending) void commands.cancelRequest(request).catch(() => undefined);
};
}, [path, repo]);
const q = query.trim().toLocaleLowerCase();
const items = (commits ?? []).filter((c) =>
`${c.id} ${formatTime(c.timeMs)} ${c.author} ${c.summary}`.toLocaleLowerCase().includes(q),
);
const idx = Math.min(active, items.length - 1);
const onKeyDown = (e: KeyboardEvent) => {
if (e.key === "Enter") {
e.preventDefault();
if (items[idx]) onChoose(items[idx]);
return;
}
const next = listNavigate(e.key, { items, active: idx, wrap: true });
if (next !== null) {
e.preventDefault();
setActive(next);
}
};
return (
<Dialog open title={title ?? "Compare with Revision…"} onClose={onClose} width={720} position="top">
<div className={styles.search}>
<SearchField
data-autofocus
value={query}
onChange={(value) => {
setQuery(value);
setActive(0);
}}
onKeyDown={onKeyDown}
placeholder="Search file revisions"
aria-label="Search file revisions"
variant="bare"
/>
</div>
<div className={styles.body}>
{error ? (
<div className={styles.error} role="alert">
{error}
</div>
) : commits === null ? (
<Spinner size={14} />
) : (
<List
items={items}
getKey={(c) => c.id}
activeIndex={idx}
onActiveChange={setActive}
onActivate={onChoose}
focusable={false}
hoverSelects
rowHeight={28}
aria-label="File revisions"
empty={<T>No commits</T>}
className={styles.list}
renderItem={(c) => (
<span className={styles.row} title={`${c.shortId} ${formatTime(c.timeMs)} ${c.author} ${c.summary}`}>
<span className={styles.hash}>{c.shortId}</span>
<span className={styles.date}>{formatTime(c.timeMs)}</span>
<span className={styles.author}>{c.author}</span>
<span className={styles.subject}>{c.summary}</span>
</span>
)}
/>
)}
</div>
</Dialog>
);
}