Compare commits

...
Author SHA1 Message Date
l0ng-ai 9448c86609 fix(diff): keep revision filenames readable in narrow panes 2026-11-22 13:25:26 +08:00
3 changed files with 106 additions and 9 deletions

No files matched your search

+65 -1
View File
@@ -1,6 +1,6 @@
import type { EditorView } from "@codemirror/view";
import type { Page } from "@playwright/test";
import { expect, test } from "./fixtures";
import { expect, stubDesktop, test } from "./fixtures";
async function openChain(page: Page) {
await page.evaluate(async () => {
@@ -46,6 +46,70 @@ test("a narrow diff keeps navigation, settings and the difference count visible"
await expect(toolbar).toContainText("1 of 1 differences");
});
test("revision titles keep both filenames in view when the diff divider moves", async ({ workbench: page }) => {
const leftName = "OrderRoutes.ts";
const rightName = "OrdersRoutes.ts";
const directory = "services/customer-orders/fulfillment/warehouse-routing/primary";
await page.setViewportSize({ width: 800, height: 650 });
await stubDesktop(page);
await page.evaluate(
async ({ directory, leftName, rightName }) => {
const inputPath = "/src/features/vcs/diffInput.ts";
const layoutPath = "/src/workbench/toolwindows/store.ts";
const { openDiff } = await import(inputPath);
const { hideAllToolWindows } = await import(layoutPath);
const leftPath = `/dev/acme-api/${directory}/${leftName}`;
const rightPath = `/dev/acme-api/${directory}/${rightName}`;
window.__ipc.handlers.vcs_file_at = (args) =>
args.rev === "7b70e02c" ? "export const region = 'east';\r\n" : "export const region = 'west';\n";
window.__ipc.handlers.vcs_file_mode = () => "100644";
hideAllToolWindows();
localStorage.setItem("openidea.vcs.diffOptions", JSON.stringify({ mode: "split", alignChanges: false }));
await openDiff({
path: rightPath,
left: { type: "rev", rev: "7b70e02c", path: leftPath },
right: { type: "rev", rev: "a8c5f31d", path: rightPath },
});
},
{ directory, leftName, rightName },
);
const left = page.getByTitle(/^7b70e02c .*OrderRoutes\.ts$/);
const right = page.getByTitle(/^a8c5f31d .*OrdersRoutes\.ts$/);
const filenamesFit = async () => {
for (const [title, filename] of [
[left, leftName],
[right, rightName],
] as const) {
await expect(title).toBeVisible();
expect(await title.getAttribute("title")).toContain(`${directory}/${filename}`);
const text = title.locator("span").filter({ hasText: filename }).last();
expect(
await text.evaluate((el, filename) => {
const node = el.firstChild!;
const range = document.createRange();
range.setStart(node, (node.textContent ?? "").length - filename.length);
range.setEnd(node, (node.textContent ?? "").length);
const bounds = range.getBoundingClientRect();
const clip = el.getBoundingClientRect();
return bounds.left >= clip.left - 1 && bounds.right <= clip.right + 1;
}, filename),
`${filename} is visible within its revision label`,
).toBe(true);
}
};
await filenamesFit();
const divider = page.getByRole("separator", { name: "Resize diff panes" });
const { x, y, width, height } = (await divider.boundingBox())!;
const leftWidth = (await left.boundingBox())!.width;
await page.mouse.move(x + width / 2, y + height / 2);
await page.mouse.down();
await page.mouse.move(x + width / 2 - 96, y + height / 2, { steps: 4 });
await page.mouse.up();
await expect.poll(async () => (await left.boundingBox())!.width).toBeLessThan(leftWidth - 60);
await filenamesFit();
});
test("a medium-width diff keeps its full status beside the viewer tools", async ({ workbench: page }) => {
await page.setViewportSize({ width: 490, height: 650 });
await page.evaluate(async () => {
+16
View File
@@ -118,6 +118,22 @@
.sourceLabels .labelFile {
font-size: var(--fs-small);
}
.labelFile.revisionPath {
display: inline-flex;
color: var(--fg-muted);
font-size: var(--fs-ui);
}
.revisionDirectory,
.revisionFilename {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
}
.revisionFilename {
/* Let the directory shrink first, then elide the filename if it still exceeds the path's width. */
flex: none;
max-width: 100%;
}
.badge {
padding: 0 5px;
border-radius: var(--radius-small);
+25 -8
View File
@@ -111,6 +111,23 @@ import styles from "./DiffEditor.module.css";
/** Commands of the focused diff viewer (diff.* actions, vcs.nextChange / prevChange). */
export { focusedDiffNavigator, type DiffNavigator };
interface PaneTitle {
label: string;
detail?: string;
revisionPath?: string;
}
function PaneTitleDetail({ detail, revisionPath }: PaneTitle) {
if (!revisionPath) return detail ? <span className={styles.labelFile}>{detail}</span> : null;
const filenameStart = revisionPath.lastIndexOf("/") + 1;
return (
<span className={clsx(styles.labelFile, styles.revisionPath)}>
{filenameStart > 0 && <span className={styles.revisionDirectory}>{revisionPath.slice(0, filenameStart)}</span>}
<span className={styles.revisionFilename}>{revisionPath.slice(filenameStart)}</span>
</span>
);
}
interface Views {
/** The right / unified editor (chunks live here). */
b: EditorView;
@@ -1371,7 +1388,7 @@ export function DiffEditor({
const canStage = editableB && isWorktree(right) && left.type === "rev" && !!useVcs.getState().root;
const hasSource = !!source;
const paneTitle = (s: DiffSide): { label: string; detail?: string } => {
const paneTitle = (s: DiffSide): PaneTitle => {
if (s.type === "worktree" || s.type === "text") return { label: sideLabel(s), detail: s.labelDetail };
if (s.type !== "rev") return { label: sideLabel(s) };
const file = s.path ?? path;
@@ -1379,13 +1396,13 @@ export function DiffEditor({
const commit = !s.label && s.rev === "HEAD" && root ? heads[root]?.headCommit : null;
return {
label: commit ? commit.slice(0, 8) : sideLabel(s),
detail: root && file.startsWith(`${root}/`) ? file.slice(root.length + 1) : file,
revisionPath: root && file.startsWith(`${root}/`) ? file.slice(root.length + 1) : file,
};
};
const titleA = paneTitle(left);
const titleB = paneTitle(right);
const captionA = [titleA.label, titleA.detail].filter(Boolean).join(" ");
const captionB = [titleB.label, titleB.detail].filter(Boolean).join(" ");
const captionA = [titleA.label, titleA.revisionPath ?? titleA.detail].filter(Boolean).join(" ");
const captionB = [titleB.label, titleB.revisionPath ?? titleB.detail].filter(Boolean).join(" ");
const sourceLabels = "labelDetail" in left || "labelDetail" in right;
const labels = clsx(styles.labels, sourceLabels && styles.sourceLabels);
@@ -1591,7 +1608,7 @@ export function DiffEditor({
<div className={labels}>
<span className={styles.label} title={captionA}>
<span className={styles.labelText}>{titleA.label}</span>
{titleA.detail && <span className={styles.labelFile}>{titleA.detail}</span>}
<PaneTitleDetail {...titleA} />
{eolDiffers && eolA && <span className={styles.badge}>{badgeA}</span>}
{editableA && (
<span className={styles.badge}>
@@ -1601,7 +1618,7 @@ export function DiffEditor({
</span>
<span className={styles.label} title={captionB}>
<span className={styles.labelText}>{titleB.label}</span>
{titleB.detail && <span className={styles.labelFile}>{titleB.detail}</span>}
<PaneTitleDetail {...titleB} />
{eolDiffers && eolB && <span className={styles.badge}>{badgeB}</span>}
{editableB && (
<span className={styles.badge}>{left.type === "rev" ? tx("working copy") : tx("editable")}</span>
@@ -1612,10 +1629,10 @@ export function DiffEditor({
<div className={labels}>
<span className={styles.label} title={sourceLabels ? `${captionA} → ${captionB}` : undefined}>
<span className={styles.labelText}>{sideLabel(left)}</span>
{sourceLabels && titleA.detail && <span className={styles.labelFile}>{titleA.detail}</span>}
{sourceLabels && <PaneTitleDetail {...titleA} />}
{eolDiffers && eolA && <span className={styles.badge}>{badgeA}</span>} →
<span className={styles.labelText}>{sideLabel(right)}</span>
{sourceLabels && titleB.detail && <span className={styles.labelFile}>{titleB.detail}</span>}
{sourceLabels && <PaneTitleDetail {...titleB} />}
{eolDiffers && eolB && <span className={styles.badge}>{badgeB}</span>}
</span>
</div>