Align selection history preview and revision table (#4764)

The selection history dialog aligns its preview and revision table and keeps both usable in narrow panes.
This commit is contained in:
l0ng-ai
2026-11-20 19:40:33 +08:00
committed by GitHub
parent 844e290729
commit 834fac89b4
5 changed files with 215 additions and 62 deletions
+94
View File
@@ -0,0 +1,94 @@
import type { EditorView } from "@codemirror/view";
import { expect, stubDesktop, test } from "./fixtures";
test("selection history keeps a full-width diff above a keyboard-resizable revision table", async ({
workbench: page,
}) => {
await page.setViewportSize({ width: 1140, height: 740 });
const filename = "QuarterlyCrossBorderSettlementReconciliationProjection.ts";
const path = `/dev/acme-api/modules/${filename}`;
const lastMessage = "Introduce the reconciliation projection for pending cross-border merchant settlements";
await page.evaluate(async (path) => {
const backend = "/src/editor/core/backend.ts";
(await import(/* @vite-ignore */ backend))
.docBackend()
.files.set(path, "export const pending = 0;\nconst projection = uncommitted;\n");
const service = "/src/workbench/contributions.ts";
await (await import(/* @vite-ignore */ service)).editorService().openFile(path);
}, path);
const editor = page.locator(".cm-content");
await expect(editor).toContainText("uncommitted");
await editor.evaluate((el) => {
const view = (el as HTMLElement & { cmTile: { view: EditorView } }).cmTile.view;
const line = view.state.doc.line(2);
view.dispatch({ selection: { anchor: line.from, head: line.to } });
view.focus();
});
await stubDesktop(page);
await page.evaluate(
async ({ path, lastMessage }) => {
window.__ipc.handlers.cancel_request = () => null;
window.__ipc.handlers.vcs_block_history = () => ({
headLines: [1, 1],
revisions: Array.from({ length: 22 }, (_, i) => ({
id: (i + 16).toString(16).padStart(40, "0"),
shortId: (i + 16).toString(16).padStart(7, "0"),
author: "Alexandra Reconciliation Coordinator",
email: "coordinator@example.test",
timeMs: 1738329600000 - i * 86400000,
summary: i === 21 ? lastMessage : `Update reconciliation estimate ${22 - i}`,
path,
before: `const projection = estimate${21 - i};\n`,
after: `const projection = estimate${22 - i};\n`,
beforeLine: 1,
afterLine: 1,
})),
});
const actions = "/src/platform/actions.ts";
await (await import(/* @vite-ignore */ actions)).executeAction("vcs.showHistoryForSelection");
},
{ path, lastMessage },
);
const diff = page.locator(".cm-mergeView");
await expect(diff).toContainText("uncommitted");
const revisions = page.getByRole("grid", { name: "Revisions" });
const history = revisions.or(page.getByRole("listbox", { name: "Revisions" }));
await expect(history).toBeVisible();
const preview = (await diff.boundingBox())!;
const list = (await history.boundingBox())!;
expect(list.y).toBeGreaterThanOrEqual(preview.y + preview.height - 1);
expect(preview.width).toBeGreaterThanOrEqual(list.width - 2);
await expect(revisions.getByRole("columnheader")).toHaveText(["Revision", "Date", "Author", "Message"]);
const divider = page.getByRole("separator", { name: "Revisions", exact: true });
await expect(divider).toHaveAttribute("aria-orientation", "horizontal");
const initial = (await divider.boundingBox())!.y;
await divider.press("ArrowUp");
await expect.poll(async () => (await divider.boundingBox())!.y).toBeLessThan(initial);
await expect(revisions.getByRole("row", { selected: true })).toContainText("Local Changes");
await page.setViewportSize({ width: 490, height: 620 });
await expect(divider).toHaveAttribute("aria-orientation", "horizontal");
await expect(page.getByTitle(`${filename}:2-2`, { exact: true })).toBeVisible();
const body = revisions.getByRole("rowgroup");
await body.press("End");
const last = revisions.getByRole("row", { selected: true });
await expect(last.getByRole("gridcell", { name: lastMessage, exact: true })).toHaveAttribute("title", lastMessage);
await expect
.poll(async () => {
const listBounds = (await body.boundingBox())!;
const rowBounds = (await last.boundingBox())!;
return Math.max(listBounds.y - rowBounds.y, rowBounds.y + rowBounds.height - listBounds.y - listBounds.height);
})
.toBeLessThanOrEqual(1);
const headers = revisions.getByRole("columnheader");
const cells = last.getByRole("gridcell");
for (let i = 0; i < 4; i++) {
expect(
Math.abs((await headers.nth(i).boundingBox())!.x - (await cells.nth(i).boundingBox())!.x),
).toBeLessThanOrEqual(1);
}
expect(await body.evaluate((el) => el.scrollWidth - el.clientWidth)).toBeLessThanOrEqual(1);
await expect(diff).toContainText("estimate1");
});
-13
View File
@@ -8,13 +8,6 @@
.resizable {
min-width: 0;
}
.sidebar {
flex: none;
width: 260px;
display: flex;
flex-direction: column;
border-right: 1px solid var(--border);
}
.header {
flex: none;
height: var(--h-header);
@@ -77,12 +70,6 @@
.deletedPath {
text-decoration: line-through;
}
.diff {
flex: 1;
min-width: 0;
display: flex;
flex-direction: column;
}
.message {
padding: var(--space-4);
color: var(--fg-faint);
+46 -8
View File
@@ -1,18 +1,56 @@
.range {
flex: none;
min-width: 0;
max-width: min(60%, 52ch);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: var(--fg-subtle);
font-size: var(--fs-small);
}
.summary,
.meta {
.table {
--list-inset: var(--space-1);
--history-columns: minmax(56px, 9%) minmax(68px, 18%) minmax(56px, 18%) minmax(0, 1fr);
flex: 1;
min-height: 0;
min-width: 0;
display: flex;
flex-direction: column;
}
.columns {
display: grid;
grid-template-columns: var(--history-columns);
align-items: center;
gap: var(--space-2);
width: 100%;
min-width: 0;
}
.heading {
flex: none;
min-height: var(--h-row);
padding-inline: calc(var(--list-inset) + var(--space-2));
border-bottom: 1px solid var(--border);
color: var(--fg-muted);
font-size: var(--fs-small);
overflow: hidden;
scrollbar-gutter: stable;
}
.heading::-webkit-scrollbar {
width: 10px;
}
.list {
flex: 1;
scrollbar-gutter: stable;
font-size: var(--fs-ui);
}
.cell {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.summary {
color: var(--fg-emphasis);
}
.meta {
.cell[role="gridcell"]:not(:last-child) {
color: var(--fg-subtle);
font-size: var(--fs-small);
}
.cell[role="gridcell"]:nth-child(-n + 2) {
font-variant-numeric: tabular-nums;
}
+68 -39
View File
@@ -7,7 +7,7 @@
import { useEffect, useState } from "react";
import type { BlockHistory, BlockRevision } from "@/bindings";
import { call, commands, isCancelled, nextRequestId } from "@/platform/ipc";
import { List, Spinner, T } from "@/ui";
import { List, Spinner, SplitGroup, SplitHandle, SplitPanel, T } from "@/ui";
import type { EditorInput } from "@/workbench/contributions";
import { DiffEditor } from "./DiffEditor";
import { diffInput, type DiffSide } from "./diffInput";
@@ -23,6 +23,7 @@ export type HistoryRow =
| { kind: "revision"; revision: BlockRevision };
const HEAD = "HEAD";
const COLUMNS = ["Revision", "Date", "Author", "Message"] as const;
/** Lines `from..=to` of `text`, each ending with a line break (as Git prints a block). */
export function blockOf(text: string, [from, to]: readonly [number, number]): string {
@@ -78,6 +79,7 @@ export function SelectionHistoryEditor({ input }: { input: EditorInput }) {
const rows = history ? historyRows(history, blockOf(data.text, data.lines)) : [];
const current = rows[Math.min(active, rows.length - 1)];
const [from, to] = data.lines;
const range = `${baseName(data.path)}:${from + 1}-${to + 1}`;
let detail;
if (!current)
@@ -97,13 +99,23 @@ export function SelectionHistoryEditor({ input }: { input: EditorInput }) {
}
return (
<div className={layout.root}>
<div className={layout.sidebar}>
<SplitGroup
className={`${layout.root} ${layout.resizable}`}
orientation="vertical"
storageKey="openidea.selectionHistory.layout"
>
<SplitPanel id="selection-preview" defaultSize="50%" minSize="25%">
{detail}
</SplitPanel>
<SplitHandle aria-label={tx("Revisions")} />
<SplitPanel id="selection-revisions" defaultSize="50%" minSize="25%">
<div className={layout.header}>
<span className={layout.title}>
<T>History for Selection</T>
</span>
<span className={styles.range}>{`${baseName(data.path)}:${from + 1}-${to + 1}`}</span>
<span className={styles.range} title={range}>
{range}
</span>
</div>
{error ? (
<div className={layout.message}>{error}</div>
@@ -112,43 +124,60 @@ export function SelectionHistoryEditor({ input }: { input: EditorInput }) {
<Spinner size={14} />
</div>
) : (
<List
items={rows}
getKey={(row) => (row.kind === "local" ? "local" : row.revision.id)}
activeIndex={Math.min(active, rows.length - 1)}
onActiveChange={setActive}
onActivate={(_row, i) => setActive(i)}
rowHeight={38}
aria-label="Revisions"
className={layout.list}
empty={
<span className={layout.message}>
{history.headLines ? tx`No commits changed these lines` : tx`These lines are not committed yet`}
</span>
}
renderItem={(row) =>
row.kind === "local" ? (
<span className={layout.entry}>
<span className={styles.summary}>
<T>Local Changes</T>
</span>
<span className={styles.meta}>
<T>Not Committed Yet</T>
</span>
<div
className={styles.table}
role="grid"
aria-label={tx("Revisions")}
aria-colcount={COLUMNS.length}
aria-rowcount={rows.length + 1}
>
<div role="row" aria-rowindex={1} className={`${styles.columns} ${styles.heading}`}>
{COLUMNS.map((label) => (
<span key={label} role="columnheader" className={styles.cell}>
<T>{label}</T>
</span>
) : (
<span className={layout.entry}>
<span className={styles.summary}>{row.revision.summary}</span>
<span className={styles.meta}>
{`${row.revision.shortId} · ${row.revision.author} · ${formatTime(row.revision.timeMs)}`}
</span>
))}
</div>
<List
items={rows}
getKey={(row) => (row.kind === "local" ? "local" : row.revision.id)}
activeIndex={Math.min(active, rows.length - 1)}
onActiveChange={setActive}
onActivate={(_row, i) => setActive(i)}
tableBody={{ headerRows: 1 }}
className={styles.list}
empty={
<span className={layout.message}>
{history.headLines ? tx`No commits changed these lines` : tx`These lines are not committed yet`}
</span>
)
}
/>
}
renderItem={(row) => {
const revision = row.kind === "revision" ? row.revision : null;
const cells = [
revision?.shortId ?? "",
formatTime(revision?.timeMs),
revision?.author ?? "",
revision?.summary ?? tx("Local Changes"),
];
return (
<span role="none" className={styles.columns}>
{cells.map((value, i) => (
<span
key={COLUMNS[i]}
role="gridcell"
className={styles.cell}
title={value && (!revision ? tx("Not Committed Yet") : value)}
>
{value}
</span>
))}
</span>
);
}}
/>
</div>
)}
</div>
<div className={layout.diff}>{detail}</div>
</div>
</SplitPanel>
</SplitGroup>
);
}
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import { EditorState } from "@codemirror/state";
import type { BlockHistory, BlockRevision } from "@/bindings";
@@ -108,7 +108,12 @@ describe("History for Selection tab", () => {
await waitFor(() =>
expect(screen.getByTestId("diff").textContent).toBe("bbbbbbb^: two\nthree\n| bbbbbbb: TWO\nthree\n"),
);
expect(screen.getByText(/^bbbbbbb · Ada · /)).toBeTruthy();
const row = screen.getByRole("row", { name: /bbbbbbb.*Ada.*Shout two/ });
expect(
within(row)
.getAllByRole("gridcell")
.map((cell) => cell.textContent),
).toEqual(["bbbbbbb", expect.any(String), "Ada", "Shout two"]);
});
it("says why there is nothing to show", async () => {