mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
refactor(tui): use OpenTUI diff gutter options (#57399)
This commit is contained in:
committed by
GitHub
Unverified
parent
f9f463be78
commit
09e6ddf630
@@ -4,8 +4,8 @@ import {
|
||||
CodeRenderable,
|
||||
DiffRenderable,
|
||||
getTreeSitterClient,
|
||||
LineNumberRenderable,
|
||||
type ColorInput,
|
||||
type DiffGutterOptions,
|
||||
type OnHighlightCallback,
|
||||
type Renderable,
|
||||
type ScrollBoxRenderable,
|
||||
@@ -13,9 +13,8 @@ import {
|
||||
} from "@opentui/core"
|
||||
import type { JSX } from "@opentui/solid"
|
||||
import { useRenderer } from "@opentui/solid"
|
||||
import { batch, createEffect, createMemo, createSignal, For, on, onCleanup, Show, splitProps } from "solid-js"
|
||||
import { batch, createMemo, createSignal, For, onCleanup, Show, splitProps } from "solid-js"
|
||||
import { splitAddedPatch, splitPatchHunks, type AddedPatchChunk } from "../util/diff"
|
||||
import { stringWidth } from "../util/string-width"
|
||||
|
||||
export interface PatchDiffRef {
|
||||
readonly hunks: () => readonly (DiffRenderable | BoxRenderable)[]
|
||||
@@ -36,7 +35,6 @@ type Props = Omit<JSX.IntrinsicElements["diff"], "diff" | "lineNumberBg" | "ref"
|
||||
}
|
||||
|
||||
export function PatchDiff(props: Props) {
|
||||
const renderer = useRenderer()
|
||||
const [local, diffProps] = splitProps(props, ["diff", "hunkFg", "lineNumberBg", "ref", "scroll", "gutterPadding"])
|
||||
const hunks = createMemo(() => splitPatchHunks(local.diff))
|
||||
const chunks = createMemo(() => {
|
||||
@@ -44,10 +42,16 @@ export function PatchDiff(props: Props) {
|
||||
const result = splitAddedPatch(local.diff, VIRTUAL_CHUNK_LINES)
|
||||
return result && lineCount(result) > VIRTUAL_MIN_LINES ? result : undefined
|
||||
})
|
||||
// Virtual chunks mount independently, so size the gutter for the whole file rather than the mounted chunks.
|
||||
const minDigits = createMemo(() => {
|
||||
// Hunks, virtual chunks and split sides each have their own gutter; give them all one width so the code lines up.
|
||||
const gutter = createMemo((): DiffGutterOptions => {
|
||||
const items = chunks()
|
||||
return items ? String(lineCount(items)).length : 0
|
||||
const paddingLeft = 1 + (local.gutterPadding ?? 0)
|
||||
if (items) return { paddingLeft, minDigits: String(lineCount(items)).length }
|
||||
const last = [...local.diff.matchAll(/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/gm)].flatMap((match) => [
|
||||
Number(match[1]) + Number(match[2] ?? 1) - 1,
|
||||
Number(match[3]) + Number(match[4] ?? 1) - 1,
|
||||
])
|
||||
return { paddingLeft, minDigits: String(Math.max(1, ...last)).length }
|
||||
})
|
||||
const nodes = new Map<number, DiffRenderable>()
|
||||
let virtualRoot: BoxRenderable | undefined
|
||||
@@ -60,49 +64,9 @@ export function PatchDiff(props: Props) {
|
||||
.filter((node) => !node.isDestroyed)
|
||||
},
|
||||
})
|
||||
const syncGutters = (attempt = 0) => {
|
||||
renderer.requestAnimationFrame(() => {
|
||||
const sides = [...nodes.values()]
|
||||
.filter((item) => !item.isDestroyed)
|
||||
.flatMap((item) => item.getChildren().filter((side) => side instanceof LineNumberRenderable))
|
||||
const lineNumbers = sides.map((side) => new Map([...side.getLineNumbers()].filter(([line]) => line >= 0)))
|
||||
const digits = lineNumbers.map((numbers) => Math.max(0, ...numbers.values()).toString().length)
|
||||
const after = sides.map((side) =>
|
||||
Math.max(
|
||||
0,
|
||||
...[...side.getLineSigns()].filter(([line]) => line >= 0).map(([, sign]) => stringWidth(sign.after ?? "")),
|
||||
),
|
||||
)
|
||||
// A diff creates its gutters after it mounts, so wait a few frames for them to appear.
|
||||
if (sides.length === 0 && attempt < 5) return syncGutters(attempt + 1)
|
||||
if (sides.length === 0) return
|
||||
const maxDigits = Math.max(...digits)
|
||||
const maxAfter = Math.max(...after)
|
||||
const width = Math.max(maxDigits, minDigits())
|
||||
sides.forEach((side) => {
|
||||
const index = sides.indexOf(side)
|
||||
const signs = new Map([...side.getLineSigns()].filter(([line]) => line >= 0))
|
||||
// Line -1 never renders; its sign only sets the gutter's padding columns.
|
||||
signs.set(-1, {
|
||||
before: " ".repeat(local.gutterPadding ?? 0) || undefined,
|
||||
after: " ".repeat(maxAfter + width - digits[index]),
|
||||
})
|
||||
side.setLineNumbers(lineNumbers[index])
|
||||
side.setLineSigns(signs)
|
||||
})
|
||||
})
|
||||
}
|
||||
createEffect(
|
||||
on(
|
||||
() => local.gutterPadding,
|
||||
() => syncGutters(),
|
||||
{ defer: true },
|
||||
),
|
||||
)
|
||||
const register = (index: number, node: DiffRenderable) => {
|
||||
nodes.set(index, node)
|
||||
onCleanup(() => nodes.delete(index))
|
||||
syncGutters()
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -125,6 +89,7 @@ export function PatchDiff(props: Props) {
|
||||
diff={hunk.patch}
|
||||
minHeight={hunk.rows}
|
||||
lineNumberBg={local.lineNumberBg}
|
||||
gutter={gutter()}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
@@ -137,6 +102,7 @@ export function PatchDiff(props: Props) {
|
||||
scroll={local.scroll!}
|
||||
diffProps={diffProps}
|
||||
lineNumberBg={local.lineNumberBg}
|
||||
gutter={gutter()}
|
||||
register={register}
|
||||
registerRoot={(root) => (virtualRoot = root)}
|
||||
/>
|
||||
@@ -152,6 +118,7 @@ function VirtualAddedPatch(props: {
|
||||
scroll: () => ScrollBoxRenderable | undefined
|
||||
diffProps: Omit<JSX.IntrinsicElements["diff"], "diff" | "lineNumberBg" | "ref">
|
||||
lineNumberBg: ColorInput
|
||||
gutter: DiffGutterOptions
|
||||
register: (index: number, node: DiffRenderable) => void
|
||||
registerRoot: (root: BoxRenderable) => void
|
||||
}) {
|
||||
@@ -225,6 +192,7 @@ function VirtualAddedPatch(props: {
|
||||
wrapMode="none"
|
||||
height={chunk.rows}
|
||||
lineNumberBg={props.lineNumberBg}
|
||||
gutter={props.gutter}
|
||||
/>
|
||||
</Show>
|
||||
)}
|
||||
|
||||
@@ -2486,6 +2486,8 @@ test.each([100, 160])("the sidebar source picker switches VCS sources at %i colu
|
||||
(frame) => !frame.includes("Diff source") && frame.includes("Uncommitted") && frame.includes("const first"),
|
||||
)
|
||||
expect(viewer.vcsDiffInput()).toEqual({ location: { directory: "/repo/session" }, mode: "working", context: "12" })
|
||||
// The header resized with the new label; render once more so the click hits its new position.
|
||||
await viewer.app.flush()
|
||||
await viewer.app.mockMouse.click(source().x, source().y)
|
||||
await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
|
||||
viewer.app.mockInput.pressKey("HOME")
|
||||
|
||||
Reference in New Issue
Block a user