Files
openidea/src/features/vcs/patches.tsx
T
Jaw 70f10e741c fix(i18n): Translate the remaining UI text (Git, hierarchy, pull requests, dialogs, aria labels) (#114)
Hard-coded UI text, titles and aria-labels in about 110 components (Git panels, hierarchy, pull requests, dialogs, settings, welcome) now go through tx; spliced fragments became whole sentences (fileCount, noneFoundLabel, hierarchyTitle) and pull request states use keyed messages. 225 zh entries added, stale fragment entries removed.

Also: tx used as a tag without interpolations (tx`No checks`) now looks the text up exactly instead of as a template, so it is translated like tx("No checks"); the text-catalog test checks such tags as plain literals. Check and test counts reuse the existing "{count} passed" / "{failed} failing" entries.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
Co-authored-by: jaw52 <53564781+jaw52@users.noreply.github.com>
2026-10-28 12:18:46 +08:00

284 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** Create Patch… (from local changes or commits) and Apply Patch… (file or clipboard, with a preview). */
import { useEffect, useState } from "react";
import type { Commit, FileStatus, PatchFile, PatchSource } from "@/bindings";
import { call, commands } from "@/platform/ipc";
import { Button, Checkbox, Dialog, Icon, Input, List, Spinner, fileIconName, T } from "@/ui";
import { notifications } from "@/workbench/contributions";
import { openDialog } from "./dialogs";
import { KIND_COLOR, baseName, dirName, stripSlash } from "./model";
import { errorMessage, logConsole, runOp, saveAll } from "./ops";
import { FileChecklist, defaultChangeFiles, reportApply } from "./shelf";
import { activeList, useChangelists } from "./changelists";
import { useVcs } from "./store";
import styles from "./common.module.css";
import { tx } from "@/platform/i18n";
export type PatchOrigin = { kind: "changes"; files: FileStatus[] } | { kind: "commits"; commits: Commit[] };
/** File name IDEA suggests: the change list or commit subject, sanitised. */
export function suggestedPatchName(origin: PatchOrigin, listName: string): string {
const base =
origin.kind === "commits"
? origin.commits.length === 1
? origin.commits[0]!.summary
: `${origin.commits.length} commits`
: listName;
const clean = base
.replace(/[^\p{L}\p{N}._-]+/gu, "_")
.replace(/^_+|_+$/g, "")
.slice(0, 60);
return `${clean || "changes"}.patch`;
}
export function showCreatePatchForChanges(paths?: readonly string[]) {
const files = defaultChangeFiles(paths);
if (!files.length) {
notifications().notify({ group: "vcs", level: "info", title: "No changes to create a patch from" });
return;
}
openDialog((close) => <CreatePatchDialog origin={{ kind: "changes", files }} onClose={close} />);
}
export function showCreatePatchForCommits(commits: Commit[]) {
if (!commits.length) return;
openDialog((close) => <CreatePatchDialog origin={{ kind: "commits", commits }} onClose={close} />);
}
async function browseSave(current: string): Promise<string | null> {
try {
const { save } = await import("@tauri-apps/plugin-dialog");
return (await save({ defaultPath: current, filters: [{ name: "Patch", extensions: ["patch", "diff"] }] })) ?? null;
} catch {
return null;
}
}
function CreatePatchDialog({ origin, onClose }: { origin: PatchOrigin; onClose: () => void }) {
const root = useVcs((s) => s.root) ?? "";
const listName = useChangelists((s) => activeList(s.state).name);
const [selected, setSelected] = useState(
() => new Set(origin.kind === "changes" ? origin.files.map((f) => f.path) : []),
);
const [toFile, setToFile] = useState(true);
const [file, setFile] = useState(() => `${root}/${suggestedPatchName(origin, listName)}`);
const [reverse, setReverse] = useState(false);
const empty = origin.kind === "changes" && !selected.size;
const run = async () => {
if (empty || (toFile && !file.trim())) return;
onClose();
await saveAll();
const source: PatchSource =
origin.kind === "changes"
? {
from: "changes",
paths: origin.files.filter((f) => selected.has(f.path)).map((f) => stripSlash(f.path)),
reverse,
}
: {
from: "commits",
commits: [...origin.commits].sort((a, b) => (a.timeMs ?? 0) - (b.timeMs ?? 0)).map((c) => c.id),
reverse,
};
await runOp(
"Create patch",
async () => {
const patch = await call(commands.vcsCreatePatch(source));
if (toFile) await call(commands.vcsWritePatch(file.trim(), patch));
else await navigator.clipboard.writeText(patch);
},
{ success: toFile ? `Patch saved to ${baseName(file.trim())}` : "Patch copied to the clipboard", refresh: false },
);
};
return (
<Dialog
open
title="Create Patch"
onClose={onClose}
width={560}
buttons={[
{ label: "Create", role: "default", disabled: empty || (toFile && !file.trim()), onClick: () => void run() },
{ label: "Cancel", role: "cancel" },
]}
>
{origin.kind === "changes" ? (
<>
<div className={styles.section}>
<T>Changes to include:</T>
</div>
<FileChecklist files={origin.files} selected={selected} onChange={setSelected} label="Files to include" />
</>
) : (
<div className={styles.section}>
{origin.commits.length === 1
? `${origin.commits[0]!.shortId} ${origin.commits[0]!.summary}`
: `${origin.commits.length} commits`}
</div>
)}
<div className={styles.form} role="radiogroup" aria-label={tx("Patch destination")}>
<label className={styles.radioRow}>
<input type="radio" name="dest" checked={toFile} onChange={() => setToFile(true)} />
<span className={styles.radioText}>
<T>Save to file:</T>
</span>
</label>
<div className={styles.cloneDirRow}>
<Input
className={styles.cloneDirInput}
mono
aria-label="Patch file"
disabled={!toFile}
value={file}
onChange={(e) => setFile(e.target.value)}
/>
<Button disabled={!toFile} onClick={() => void browseSave(file).then((p) => p && setFile(p))}>
<T>Browse…</T>
</Button>
</div>
<label className={styles.radioRow}>
<input type="radio" name="dest" checked={!toFile} onChange={() => setToFile(false)} />
<span className={styles.radioText}>
<T>Copy to clipboard</T>
</span>
</label>
<Checkbox checked={reverse} onChange={setReverse} label="Reverse patch" />
</div>
</Dialog>
);
}
// ---------------------------------------------------------------------------
/** Apply Patch…: from a file (chooser), or the clipboard. */
export async function showApplyPatch(from: "file" | "clipboard", path?: string) {
let text: string | null;
let source = "Clipboard";
try {
if (from === "clipboard") {
text = await navigator.clipboard.readText();
} else {
let file = path ?? null;
if (!file) {
const { open } = await import("@tauri-apps/plugin-dialog");
const picked = await open({
multiple: false,
directory: false,
defaultPath: useVcs.getState().root ?? undefined,
filters: [{ name: "Patch", extensions: ["patch", "diff"] }],
});
file = typeof picked === "string" ? picked : null;
}
if (!file) return;
source = baseName(file);
text = await call(commands.fsReadText(file));
}
} catch (e) {
logConsole("err", errorMessage(e));
notifications().notify({ group: "vcs", level: "error", title: "Cannot read the patch", body: errorMessage(e) });
return;
}
if (!text?.trim()) {
notifications().notify({
group: "vcs",
level: "info",
title: from === "clipboard" ? "The clipboard does not contain a patch" : "The patch file is empty",
});
return;
}
const patch = text;
openDialog((close) => <ApplyPatchDialog patch={patch} source={source} onClose={close} />);
}
function ApplyPatchDialog({ patch, source, onClose }: { patch: string; source: string; onClose: () => void }) {
const [files, setFiles] = useState<PatchFile[] | null>(null);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
let alive = true;
void saveAll().then(() =>
call(commands.vcsPatchPreview(patch))
.then((f) => alive && setFiles(f))
.catch((e) => alive && setError(errorMessage(e))),
);
return () => {
alive = false;
};
}, [patch]);
const failing = files?.filter((f) => !f.applies).length ?? 0;
const run = async () => {
onClose();
await saveAll();
const outcome = await runOp(`Apply patch (${source})`, () => call(commands.vcsApplyPatch(patch)), {
refresh: false,
});
if (outcome) await reportApply(outcome, "Patch applied");
};
return (
<Dialog
open
title={tx`Apply Patch: ${source}`}
onClose={onClose}
width={600}
footerStart={
files && failing > 0 ? (
<span className={styles.hint}>
{failing === 1
? tx`${failing} file will be merged; conflicts open in the merge tool`
: tx`${failing} files will be merged; conflicts open in the merge tool`}
</span>
) : undefined
}
buttons={[
{ label: "OK", role: "default", autoFocus: true, disabled: !files?.length, onClick: () => void run() },
{ label: "Cancel", role: "cancel" },
]}
>
<div className={styles.section}>
{files ? `${files.length} file${files.length === 1 ? "" : "s"} in the patch` : ""}
</div>
<div style={{ height: 280, display: "flex", flexDirection: "column", padding: "0 14px 14px" }}>
{error ? (
<div className={styles.error}>{error}</div>
) : !files ? (
<div className={styles.empty}>
<Spinner size={14} />
</div>
) : (
<List
items={files}
getKey={(f) => f.path}
rowHeight={22}
aria-label="Patch files"
className={styles.boxList}
empty={
<span>
<T>No changes</T>
</span>
}
renderItem={(f) => (
<span
className={styles.fileRow}
style={{ flex: 1, padding: 0 }}
title={f.applies ? "Applies cleanly" : "Does not apply cleanly: a 3-way merge will be attempted"}
>
<Icon name={fileIconName(baseName(f.path))} size={14} />
<span
className={styles.fileName}
style={{
color:
KIND_COLOR[f.kind === "untracked" ? "added" : f.kind === "conflicted" ? "conflicted" : f.kind],
}}
>
{baseName(f.path)}
</span>
<span className={styles.fileDir}>{f.oldPath ? `from ${f.oldPath}` : dirName(f.path)}</span>
<span className={styles.grow} />
<span className={styles.commitMeta}>{f.binary ? "binary" : `+${f.added} −${f.removed}`}</span>
{!f.applies && <Icon name="warning" size={14} label="Does not apply cleanly" />}
</span>
)}
/>
)}
</div>
</Dialog>
);
}