mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
feat(vcs): edit the compared folders in Compare Directories (#4975)
The left and right paths of Compare Directories are editable fields with a folder chooser beside each, as in IDEA. Enter, leaving the field or picking a folder compares the new pair, opened as its own tab in place of the current one; a path that isn't a folder, or the same folder on both sides, is refused with a message. The layout e2e test follows the editable fields and per-row synchronisation. Closes #3925 Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
This commit is contained in:
co-authored by
wildcatDownstairs
parent
0a7a292d82
commit
db8f9a72b0
@@ -21,13 +21,12 @@
|
||||
.path {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--space-2);
|
||||
gap: var(--space-1);
|
||||
min-width: 0;
|
||||
}
|
||||
.pathInput {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
min-height: var(--h-field);
|
||||
padding-inline: var(--space-2);
|
||||
border-radius: var(--radius-control);
|
||||
background: var(--bg-field);
|
||||
box-shadow: inset 0 0 0 1px var(--border);
|
||||
}
|
||||
.path:last-child {
|
||||
grid-column: 3;
|
||||
|
||||
@@ -29,12 +29,13 @@ import {
|
||||
lazyComponent,
|
||||
type IconName,
|
||||
} from "@/ui";
|
||||
import type { EditorInput } from "@/workbench/contributions";
|
||||
import { editorService, type EditorInput } from "@/workbench/contributions";
|
||||
import { choosePath } from "@/platform/choosePath";
|
||||
import { upsertTask } from "@/workbench/tasks";
|
||||
import { confirmDialog } from "./dialogs";
|
||||
import { diffInput, openVcsDiff, type DiffData, type DiffSide } from "./diffInput";
|
||||
import { compareFilesData, displayPath } from "./compareData";
|
||||
import type { DirectoryCompareData } from "./directoryCompareKind";
|
||||
import { compareFilesData } from "./compareData";
|
||||
import { directoryCompareInput, type DirectoryCompareData } from "./directoryCompareKind";
|
||||
import { formatSize, formatTime } from "./model";
|
||||
import styles from "./DirectoryCompare.module.css";
|
||||
import { useRowHeight } from "@/ui/density";
|
||||
@@ -190,7 +191,18 @@ export function directoryRows(
|
||||
|
||||
export function DirectoryCompareEditor({ input }: { input: EditorInput }) {
|
||||
const { left, right } = input.data as DirectoryCompareData;
|
||||
return <DirectoryCompare left={left} right={right} />;
|
||||
return (
|
||||
<DirectoryCompare
|
||||
left={left}
|
||||
right={right}
|
||||
onPathsChange={(l, r) => {
|
||||
// ponytail: reopens as a new tab and closes this one; an in-place input update would keep the tab's position.
|
||||
void editorService()
|
||||
.openInput(directoryCompareInput(l, r), { focus: true })
|
||||
.then(() => editorService().closeInput?.(input.key));
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const COLUMNS = {
|
||||
@@ -274,7 +286,69 @@ function SyncCell({
|
||||
);
|
||||
}
|
||||
|
||||
export function DirectoryCompare({ left, right }: { left: string; right: string }) {
|
||||
/** An editable folder path with a chooser; Enter or leaving the field compares the typed folder. */
|
||||
function PathField({
|
||||
side,
|
||||
path,
|
||||
disabled,
|
||||
onCommit,
|
||||
}: {
|
||||
side: "left" | "right";
|
||||
path: string;
|
||||
disabled: boolean;
|
||||
onCommit: (path: string) => void;
|
||||
}) {
|
||||
const [draft, setDraft] = useState(path);
|
||||
const label = side === "left" ? "Left folder" : "Right folder";
|
||||
const commit = (next: string) => {
|
||||
if (next.trim() && next.trim() !== path) onCommit(next.trim());
|
||||
};
|
||||
return (
|
||||
<span className={styles.path}>
|
||||
<Input
|
||||
className={styles.pathInput}
|
||||
aria-label={label}
|
||||
title={path}
|
||||
value={draft}
|
||||
disabled={disabled}
|
||||
onChange={(e) => setDraft(e.target.value)}
|
||||
onBlur={() => commit(draft)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter") commit(draft);
|
||||
else if (e.key === "Escape" && draft !== path) {
|
||||
e.stopPropagation();
|
||||
setDraft(path);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<IconButton
|
||||
icon="folder"
|
||||
title="Browse…"
|
||||
aria-label={side === "left" ? "Choose left folder" : "Choose right folder"}
|
||||
size="small"
|
||||
disabled={disabled}
|
||||
onClick={async () => {
|
||||
const picked = await choosePath({ directory: true, title: label, defaultPath: path });
|
||||
if (picked) {
|
||||
setDraft(picked);
|
||||
commit(picked);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
export function DirectoryCompare({
|
||||
left,
|
||||
right,
|
||||
onPathsChange,
|
||||
}: {
|
||||
left: string;
|
||||
right: string;
|
||||
/** Compares other folders (the tab is replaced); without it the paths are read-only. */
|
||||
onPathsChange?: (left: string, right: string) => void;
|
||||
}) {
|
||||
const rowHeight = useRowHeight(22);
|
||||
const t = useTx();
|
||||
const [entries, setEntries] = useState<DirectoryCompareEntry[]>([]);
|
||||
@@ -288,6 +362,7 @@ export function DirectoryCompare({ left, right }: { left: string; right: string
|
||||
const [operating, setOperating] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [operationError, setOperationError] = useState<string | null>(null);
|
||||
const [pathError, setPathError] = useState<string | null>(null);
|
||||
const [progress, setProgress] = useState<DirectoryCompareProgress | null>(null);
|
||||
const [cancelled, setCancelled] = useState(false);
|
||||
const cancel = useRef<() => void>(() => {});
|
||||
@@ -369,6 +444,18 @@ export function DirectoryCompare({ left, right }: { left: string; right: string
|
||||
if (data) void openVcsDiff(data);
|
||||
};
|
||||
|
||||
const changePath = async (side: "left" | "right", path: string) => {
|
||||
setPathError(null);
|
||||
const next = side === "left" ? { left: path, right } : { left, right: path };
|
||||
try {
|
||||
if ((await call(commands.fsEntryKind(path))) !== "dir") throw new Error(tx`${path} is not a folder`);
|
||||
if (next.left === next.right) throw new Error(tx("Choose two different folders"));
|
||||
onPathsChange?.(next.left, next.right);
|
||||
} catch (e) {
|
||||
if (mounted.current) setPathError(e instanceof Error ? e.message : String(e));
|
||||
}
|
||||
};
|
||||
|
||||
const actionOf = (e: DirectoryCompareEntry) => actions.get(e.path) ?? syncChoices(e)[0]!;
|
||||
const cycle = (e: DirectoryCompareEntry) => {
|
||||
if (busy || operating) return;
|
||||
@@ -576,15 +663,21 @@ export function DirectoryCompare({ left, right }: { left: string; right: string
|
||||
)}
|
||||
</div>
|
||||
<div className={styles.paths}>
|
||||
<span className={styles.path} title={left}>
|
||||
<Icon name="folder" size={14} />
|
||||
<span className={styles.text}>{tx`Left: ${displayPath(left)}`}</span>
|
||||
</span>
|
||||
<span className={styles.path} title={right}>
|
||||
<Icon name="folder" size={14} />
|
||||
<span className={styles.text}>{tx`Right: ${displayPath(right)}`}</span>
|
||||
</span>
|
||||
{(["left", "right"] as const).map((side) => (
|
||||
<PathField
|
||||
key={`${side}:${side === "left" ? left : right}`}
|
||||
side={side}
|
||||
path={side === "left" ? left : right}
|
||||
disabled={!onPathsChange || operating}
|
||||
onCommit={(path) => void changePath(side, path)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
{pathError && (
|
||||
<div className={styles.error} role="alert">
|
||||
{tx(pathError)}
|
||||
</div>
|
||||
)}
|
||||
{error && (
|
||||
<div className={styles.error} role="alert">
|
||||
{tx(error)}
|
||||
|
||||
@@ -6,6 +6,8 @@ const mocks = vi.hoisted(() => ({
|
||||
compare: vi.fn(),
|
||||
cancel: vi.fn(),
|
||||
sync: vi.fn(),
|
||||
kind: vi.fn(),
|
||||
choose: vi.fn(),
|
||||
confirm: vi.fn(),
|
||||
open: vi.fn(),
|
||||
}));
|
||||
@@ -17,6 +19,7 @@ vi.mock("@/platform/ipc", async (original) => {
|
||||
...real.commands,
|
||||
fsCompareDirectories: mocks.compare,
|
||||
fsSynchronizeDirectories: mocks.sync,
|
||||
fsEntryKind: mocks.kind,
|
||||
cancelRequest: mocks.cancel,
|
||||
},
|
||||
};
|
||||
@@ -27,6 +30,7 @@ vi.mock("@tauri-apps/api/core", async (original) => ({
|
||||
onmessage = () => {};
|
||||
},
|
||||
}));
|
||||
vi.mock("@/platform/choosePath", () => ({ choosePath: mocks.choose }));
|
||||
vi.mock("../DiffEditor", () => ({
|
||||
DiffEditor: ({ input }: { input: unknown }) => <div data-testid="diff">{JSON.stringify(input)}</div>,
|
||||
}));
|
||||
@@ -67,6 +71,7 @@ beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.compare.mockResolvedValue(ok(rows));
|
||||
mocks.sync.mockResolvedValue(ok([]));
|
||||
mocks.kind.mockResolvedValue(ok("dir"));
|
||||
mocks.confirm.mockResolvedValue(true);
|
||||
});
|
||||
afterEach(cleanup);
|
||||
@@ -234,6 +239,33 @@ describe("disk directory comparison", () => {
|
||||
localStorage.removeItem("openidea.directoryCompare.mode");
|
||||
});
|
||||
|
||||
it("compares other folders typed into the path fields or chosen with the folder chooser", async () => {
|
||||
const change = vi.fn();
|
||||
render(<DirectoryCompare left="/p/left" right="/p/right" onPathsChange={change} />);
|
||||
const leftField = screen.getByRole("textbox", { name: "Left folder" });
|
||||
expect(leftField).toHaveValue("/p/left");
|
||||
fireEvent.change(leftField, { target: { value: "/p/other" } });
|
||||
fireEvent.keyDown(leftField, { key: "Enter" });
|
||||
await waitFor(() => expect(change).toHaveBeenCalledWith("/p/other", "/p/right"));
|
||||
expect(mocks.kind).toHaveBeenCalledWith("/p/other");
|
||||
// A file, or the folder already on the other side, is refused with a message.
|
||||
mocks.kind.mockResolvedValueOnce(ok("file"));
|
||||
const rightField = screen.getByRole("textbox", { name: "Right folder" });
|
||||
fireEvent.change(rightField, { target: { value: "/p/file.txt" } });
|
||||
fireEvent.blur(rightField);
|
||||
expect(await screen.findByRole("alert")).toHaveTextContent("/p/file.txt is not a folder");
|
||||
fireEvent.change(rightField, { target: { value: "/p/left" } });
|
||||
fireEvent.keyDown(rightField, { key: "Enter" });
|
||||
expect(await screen.findByText("Choose two different folders")).toBeInTheDocument();
|
||||
fireEvent.keyDown(rightField, { key: "Escape" });
|
||||
expect(rightField).toHaveValue("/p/right");
|
||||
mocks.choose.mockResolvedValueOnce("/p/chosen");
|
||||
fireEvent.click(screen.getByRole("button", { name: "Choose right folder" }));
|
||||
await waitFor(() => expect(change).toHaveBeenCalledWith("/p/left", "/p/chosen"));
|
||||
expect(mocks.choose).toHaveBeenCalledWith(expect.objectContaining({ directory: true, defaultPath: "/p/right" }));
|
||||
expect(change).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("streams progress, cancels on demand and on close, and ignores stale results after refresh", async () => {
|
||||
let resolve!: (value: unknown) => void;
|
||||
mocks.compare.mockImplementationOnce((_l, _r, _mode, _request, channel) => {
|
||||
|
||||
Reference in New Issue
Block a user