@@ -4,18 +4,17 @@ import {
44 CodeRenderable ,
55 DiffRenderable ,
66 getTreeSitterClient ,
7- LineNumberRenderable ,
87 type ColorInput ,
8+ type DiffGutterOptions ,
99 type OnHighlightCallback ,
1010 type Renderable ,
1111 type ScrollBoxRenderable ,
1212 type SimpleHighlight ,
1313} from "@opentui/core"
1414import type { JSX } from "@opentui/solid"
1515import { useRenderer } from "@opentui/solid"
16- import { batch , createEffect , createMemo , createSignal , For , on , onCleanup , Show , splitProps } from "solid-js"
16+ import { batch , createMemo , createSignal , For , onCleanup , Show , splitProps } from "solid-js"
1717import { splitAddedPatch , splitPatchHunks , type AddedPatchChunk } from "../util/diff"
18- import { stringWidth } from "../util/string-width"
1918
2019export interface PatchDiffRef {
2120 readonly hunks : ( ) => readonly ( DiffRenderable | BoxRenderable ) [ ]
@@ -36,18 +35,23 @@ type Props = Omit<JSX.IntrinsicElements["diff"], "diff" | "lineNumberBg" | "ref"
3635}
3736
3837export function PatchDiff ( props : Props ) {
39- const renderer = useRenderer ( )
4038 const [ local , diffProps ] = splitProps ( props , [ "diff" , "hunkFg" , "lineNumberBg" , "ref" , "scroll" , "gutterPadding" ] )
4139 const hunks = createMemo ( ( ) => splitPatchHunks ( local . diff ) )
4240 const chunks = createMemo ( ( ) => {
4341 if ( ! local . scroll ) return
4442 const result = splitAddedPatch ( local . diff , VIRTUAL_CHUNK_LINES )
4543 return result && lineCount ( result ) > VIRTUAL_MIN_LINES ? result : undefined
4644 } )
47- // Virtual chunks mount independently, so size the gutter for the whole file rather than the mounted chunks .
48- const minDigits = createMemo ( ( ) => {
45+ // Hunks, virtual chunks and split sides each have their own gutter; give them all one width so the code lines up .
46+ const gutter = createMemo ( ( ) : DiffGutterOptions => {
4947 const items = chunks ( )
50- return items ? String ( lineCount ( items ) ) . length : 0
48+ const paddingLeft = 1 + ( local . gutterPadding ?? 0 )
49+ if ( items ) return { paddingLeft, minDigits : String ( lineCount ( items ) ) . length }
50+ const last = [ ...local . diff . matchAll ( / ^ @ @ - ( \d + ) (?: , ( \d + ) ) ? \+ ( \d + ) (?: , ( \d + ) ) ? @ @ / gm) ] . flatMap ( ( match ) => [
51+ Number ( match [ 1 ] ) + Number ( match [ 2 ] ?? 1 ) - 1 ,
52+ Number ( match [ 3 ] ) + Number ( match [ 4 ] ?? 1 ) - 1 ,
53+ ] )
54+ return { paddingLeft, minDigits : String ( Math . max ( 1 , ...last ) ) . length }
5155 } )
5256 const nodes = new Map < number , DiffRenderable > ( )
5357 let virtualRoot : BoxRenderable | undefined
@@ -60,49 +64,9 @@ export function PatchDiff(props: Props) {
6064 . filter ( ( node ) => ! node . isDestroyed )
6165 } ,
6266 } )
63- const syncGutters = ( attempt = 0 ) => {
64- renderer . requestAnimationFrame ( ( ) => {
65- const sides = [ ...nodes . values ( ) ]
66- . filter ( ( item ) => ! item . isDestroyed )
67- . flatMap ( ( item ) => item . getChildren ( ) . filter ( ( side ) => side instanceof LineNumberRenderable ) )
68- const lineNumbers = sides . map ( ( side ) => new Map ( [ ...side . getLineNumbers ( ) ] . filter ( ( [ line ] ) => line >= 0 ) ) )
69- const digits = lineNumbers . map ( ( numbers ) => Math . max ( 0 , ...numbers . values ( ) ) . toString ( ) . length )
70- const after = sides . map ( ( side ) =>
71- Math . max (
72- 0 ,
73- ...[ ...side . getLineSigns ( ) ] . filter ( ( [ line ] ) => line >= 0 ) . map ( ( [ , sign ] ) => stringWidth ( sign . after ?? "" ) ) ,
74- ) ,
75- )
76- // A diff creates its gutters after it mounts, so wait a few frames for them to appear.
77- if ( sides . length === 0 && attempt < 5 ) return syncGutters ( attempt + 1 )
78- if ( sides . length === 0 ) return
79- const maxDigits = Math . max ( ...digits )
80- const maxAfter = Math . max ( ...after )
81- const width = Math . max ( maxDigits , minDigits ( ) )
82- sides . forEach ( ( side ) => {
83- const index = sides . indexOf ( side )
84- const signs = new Map ( [ ...side . getLineSigns ( ) ] . filter ( ( [ line ] ) => line >= 0 ) )
85- // Line -1 never renders; its sign only sets the gutter's padding columns.
86- signs . set ( - 1 , {
87- before : " " . repeat ( local . gutterPadding ?? 0 ) || undefined ,
88- after : " " . repeat ( maxAfter + width - digits [ index ] ) ,
89- } )
90- side . setLineNumbers ( lineNumbers [ index ] )
91- side . setLineSigns ( signs )
92- } )
93- } )
94- }
95- createEffect (
96- on (
97- ( ) => local . gutterPadding ,
98- ( ) => syncGutters ( ) ,
99- { defer : true } ,
100- ) ,
101- )
10267 const register = ( index : number , node : DiffRenderable ) => {
10368 nodes . set ( index , node )
10469 onCleanup ( ( ) => nodes . delete ( index ) )
105- syncGutters ( )
10670 }
10771
10872 return (
@@ -125,6 +89,7 @@ export function PatchDiff(props: Props) {
12589 diff = { hunk . patch }
12690 minHeight = { hunk . rows }
12791 lineNumberBg = { local . lineNumberBg }
92+ gutter = { gutter ( ) }
12893 />
12994 </ >
13095 ) }
@@ -137,6 +102,7 @@ export function PatchDiff(props: Props) {
137102 scroll = { local . scroll ! }
138103 diffProps = { diffProps }
139104 lineNumberBg = { local . lineNumberBg }
105+ gutter = { gutter ( ) }
140106 register = { register }
141107 registerRoot = { ( root ) => ( virtualRoot = root ) }
142108 />
@@ -152,6 +118,7 @@ function VirtualAddedPatch(props: {
152118 scroll : ( ) => ScrollBoxRenderable | undefined
153119 diffProps : Omit < JSX . IntrinsicElements [ "diff" ] , "diff" | "lineNumberBg" | "ref" >
154120 lineNumberBg : ColorInput
121+ gutter : DiffGutterOptions
155122 register : ( index : number , node : DiffRenderable ) => void
156123 registerRoot : ( root : BoxRenderable ) => void
157124} ) {
@@ -225,6 +192,7 @@ function VirtualAddedPatch(props: {
225192 wrapMode = "none"
226193 height = { chunk . rows }
227194 lineNumberBg = { props . lineNumberBg }
195+ gutter = { props . gutter }
228196 />
229197 </ Show >
230198 ) }
0 commit comments