fix(tui): synchronize diff gutters

This commit is contained in:
James Long 2026-07-30 16:28:45 +00:00
parent ffa9a597dc
commit 0d4ab8e6be
2 changed files with 35 additions and 6 deletions

View file

@ -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<JSX.IntrinsicElements["diff"], "diff" | "lineNumberBg"> & {
export function PatchDiff(props: Props) {
const [local, diffProps] = splitProps(props, ["diff", "hunkFg", "lineNumberBg"])
const hunks = createMemo(() => splitPatchHunks(local.diff))
const nodes = new Set<DiffRenderable>()
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 (
<For each={hunks()}>
@ -25,7 +44,13 @@ export function PatchDiff(props: Props) {
</text>
</box>
</Show>
<diff {...diffProps} diff={hunk.patch} minHeight={hunk.rows} lineNumberBg={local.lineNumberBg} />
<diff
{...diffProps}
ref={register}
diff={hunk.patch}
minHeight={hunk.rows}
lineNumberBg={local.lineNumberBg}
/>
</>
)}
</For>

View file

@ -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[] {