From 0d4ab8e6befc38e49456ab950fe5ad9d9924208b Mon Sep 17 00:00:00 2001 From: James Long <17031+jlongster@users.noreply.github.com> Date: Thu, 30 Jul 2026 16:28:45 +0000 Subject: [PATCH] fix(tui): synchronize diff gutters --- packages/tui/src/component/patch-diff.tsx | 29 +++++++++++++++++-- .../tui/test/component/patch-diff.test.tsx | 12 +++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/packages/tui/src/component/patch-diff.tsx b/packages/tui/src/component/patch-diff.tsx index aa314354e9..e1bc371702 100644 --- a/packages/tui/src/component/patch-diff.tsx +++ b/packages/tui/src/component/patch-diff.tsx @@ -1,5 +1,5 @@ /** @jsxImportSource @opentui/solid */ -import type { ColorInput } from "@opentui/core" +import { DiffRenderable, LineNumberRenderable, type ColorInput } from "@opentui/core" import type { JSX } from "@opentui/solid" import { createMemo, For, Show, splitProps } from "solid-js" import { splitPatchHunks } from "../util/diff" @@ -13,6 +13,25 @@ type Props = Omit & { export function PatchDiff(props: Props) { const [local, diffProps] = splitProps(props, ["diff", "hunkFg", "lineNumberBg"]) const hunks = createMemo(() => splitPatchHunks(local.diff)) + const nodes = new Set() + const syncGutters = (attempt = 0) => { + requestAnimationFrame(() => { + const sides = [...nodes] + .filter((item) => !item.isDestroyed) + .flatMap((item) => item.getChildren().filter((side) => side instanceof LineNumberRenderable)) + const max = Math.max(...sides.flatMap((side) => [...side.getLineNumbers().values()])) + if (!max && attempt < 2) return syncGutters(attempt + 1) + if (!max) return + sides.forEach((side) => { + side.setLineNumbers(new Map([...side.getLineNumbers(), [-1, max]])) + side.setLineSigns(new Map([...side.getLineSigns(), [-1, { after: " " }]])) + }) + }) + } + const register = (node: DiffRenderable) => { + nodes.add(node) + syncGutters() + } return ( @@ -25,7 +44,13 @@ export function PatchDiff(props: Props) { - + )} diff --git a/packages/tui/test/component/patch-diff.test.tsx b/packages/tui/test/component/patch-diff.test.tsx index f3e20ca8f7..9c0c85d41c 100644 --- a/packages/tui/test/component/patch-diff.test.tsx +++ b/packages/tui/test/component/patch-diff.test.tsx @@ -41,9 +41,9 @@ test("renders separate diff nodes with a full-width hunk row", async () => { ), { width: 120, height: 30 }, ) - const frame = await app.waitForFrame((value) => - value.includes("@@ -20,3 +20,3 @@"), - ) + await app.waitForFrame((value) => value.includes("@@ -20,3 +20,3 @@")) + await app.renderOnce() + const frame = app.captureCharFrame() const headerRow = frame.split("\n").findIndex((line) => line.includes("@@ -20,3 +20,3 @@")) const header = frame.split("\n")[headerRow] const background = parseColor("#222222") @@ -61,7 +61,11 @@ test("renders separate diff nodes with a full-width hunk row", async () => { span.bg.a === background.a, ), ).toBe(true) - expect(findDiffs(app.renderer.root)).toHaveLength(2) + const diffs = findDiffs(app.renderer.root) + const gutters = diffs.flatMap((diff) => diff.getChildren().flatMap((side) => side.getChildren().slice(0, 1))) + expect(diffs).toHaveLength(2) + expect(gutters[0].width).toBeGreaterThan(0) + expect(new Set(gutters.map((gutter) => gutter.width)).size).toBe(1) }) function findDiffs(root: Renderable): DiffRenderable[] {