@@ -66,95 +66,80 @@ function nextMarkerText(marker: ListMarker): string {
6666 return `${ marker . indent } ${ marker . bullet } ` ;
6767}
6868
69- function formatOrderedNumber ( value : number , numberText : string ) : string {
70- if ( ! Number . isSafeInteger ( value ) ) return numberText ;
71- // Keep the width only for explicitly zero-padded markers ("01.").
72- return numberText . startsWith ( "0" )
73- ? String ( value ) . padStart ( numberText . length , "0" )
74- : String ( value ) ;
75- }
76-
77- /** Visual indent width with tabs expanded to four-column stops, as in CommonMark. */
78- function indentWidth ( indent : string ) : number {
69+ // Markdown tabs advance to the next four-column stop.
70+ function indentWidth ( line : string ) : number {
7971 let width = 0 ;
80- for ( const char of indent ) width = char === "\t" ? width + 4 - ( width % 4 ) : width + 1 ;
72+ for ( const char of line ) {
73+ if ( char === " " ) width += 1 ;
74+ else if ( char === "\t" ) width += 4 - ( width % 4 ) ;
75+ else break ;
76+ }
8177 return width ;
8278}
8379
84- /**
85- * Renumber the ordered items that follow `afterLineEnd` so a newly inserted
86- * item does not leave duplicates below it. Walks the contiguous run of
87- * same-indent, same-delimiter items (skipping deeper-indented children) and
88- * stops at the first line that is not part of that run. Returns the end
89- * offset of the last renumbered marker and the replacement for the text
90- * between `afterLineEnd` and that offset.
91- */
9280function renumberFollowingItems (
9381 value : string ,
9482 afterLineEnd : number ,
9583 marker : Extract < ListMarker , { kind : "ordered" } > ,
96- firstNumber : number ,
84+ tokens : ReturnType < typeof inlineTokenRanges > ,
9785) : { end : number ; text : string } | null {
98- const tokens = inlineTokenRanges ( value ) ;
9986 const baseWidth = indentWidth ( marker . indent ) ;
100- let expected = firstNumber ;
87+ const parts : string [ ] = [ ] ;
88+ let expected = Number . parseInt ( marker . numberText , 10 ) + 2 ;
10189 let position = afterLineEnd ;
102- let end = - 1 ;
103- let text = "" ;
10490 let copiedUntil = afterLineEnd ;
105- while ( position < value . length ) {
91+ let tokenIndex = 0 ;
92+ while ( position < value . length && Number . isSafeInteger ( expected ) ) {
10693 const lineStart = position + 1 ;
10794 const nextBreak = value . indexOf ( "\n" , lineStart ) ;
10895 const lineEnd = nextBreak === - 1 ? value . length : nextBreak ;
10996 position = lineEnd ;
110- // A line that starts inside a multiline chip is part of that chip's text.
111- if ( tokens . some ( ( token ) => lineStart > token . start && lineStart < token . end ) ) continue ;
97+ let token = tokens [ tokenIndex ] ;
98+ while ( token && token . end <= lineStart ) {
99+ tokenIndex += 1 ;
100+ token = tokens [ tokenIndex ] ;
101+ }
102+ // A newline inside a chip does not start a list item.
103+ if ( token && token . start < lineStart ) continue ;
104+
112105 const line = value . slice ( lineStart , lineEnd ) ;
106+ const width = indentWidth ( line ) ;
107+ if ( line . trim ( ) === "" || width < baseWidth ) break ;
108+ if ( width > baseWidth ) continue ;
113109 const parsed = parseListMarker ( line ) ;
114- const width = indentWidth ( line . match ( / ^ [ \t ] * / ) ?. [ 0 ] ?? "" ) ;
115- if (
116- parsed ?. marker . kind === "ordered" &&
117- width === baseWidth &&
118- parsed . marker . delimiter === marker . delimiter
119- ) {
120- const current = parsed . marker ;
121- const numberStart = lineStart + current . indent . length ;
122- const numberEnd = numberStart + current . numberText . length ;
123- const replacement = formatOrderedNumber ( expected , current . numberText ) ;
124- if ( replacement !== current . numberText ) {
125- text += value . slice ( copiedUntil , numberStart ) + replacement ;
126- copiedUntil = numberEnd ;
127- end = numberEnd ;
128- }
129- expected += 1 ;
130- } else if ( line . trim ( ) === "" || width <= baseWidth ) {
131- break ;
110+ if ( parsed ?. marker . kind !== "ordered" || parsed . marker . delimiter !== marker . delimiter ) break ;
111+
112+ const current = parsed . marker ;
113+ const numberStart = lineStart + current . indent . length ;
114+ const numberEnd = numberStart + current . numberText . length ;
115+ const replacement = current . numberText . startsWith ( "0" )
116+ ? String ( expected ) . padStart ( current . numberText . length , "0" )
117+ : String ( expected ) ;
118+ if ( replacement !== current . numberText ) {
119+ parts . push ( value . slice ( copiedUntil , numberStart ) , replacement ) ;
120+ copiedUntil = numberEnd ;
132121 }
122+ expected += 1 ;
133123 }
134- return end === - 1 ? null : { end, text } ;
135- }
136-
137- function segmentSource (
138- segment : ReturnType < typeof splitPromptIntoComposerSegments > [ number ] ,
139- ) : string {
140- if ( segment . type === "text" ) return segment . text ;
141- return segment . source ;
124+ return parts . length === 0 ? null : { end : copiedUntil , text : parts . join ( "" ) } ;
142125}
143126
144127function inlineTokenRanges ( value : string ) : Array < { start : number ; end : number } > {
145128 const ranges : Array < { start : number ; end : number } > = [ ] ;
146129 let offset = 0 ;
147130 for ( const segment of splitPromptIntoComposerSegments ( value ) ) {
148- const end = offset + segmentSource ( segment ) . length ;
131+ const end = offset + ( segment . type === "text" ? segment . text : segment . source ) . length ;
149132 if ( segment . type !== "text" ) ranges . push ( { start : offset , end } ) ;
150133 offset = end ;
151134 }
152135 return ranges ;
153136}
154137
155- /** True when splitting at the caret would cut an inline chip in two. */
156- function isInsideInlineToken ( value : string , cursor : number ) : boolean {
157- return inlineTokenRanges ( value ) . some ( ( range ) => cursor > range . start && cursor < range . end ) ;
138+ function isInsideInlineToken (
139+ tokens : ReturnType < typeof inlineTokenRanges > ,
140+ cursor : number ,
141+ ) : boolean {
142+ return tokens . some ( ( range ) => cursor > range . start && cursor < range . end ) ;
158143}
159144
160145function currentLine ( value : string , cursor : number ) : { start : number ; end : number ; text : string } {
@@ -171,7 +156,8 @@ export function listContinuationForEnter(value: string, cursor: number): Compose
171156 if ( ! parsed ) return null ;
172157 const markerEnd = line . start + parsed . markerEnd ;
173158 if ( cursor < markerEnd ) return null ;
174- if ( isInsideInlineToken ( value , cursor ) ) return null ;
159+ const tokens = inlineTokenRanges ( value ) ;
160+ if ( isInsideInlineToken ( tokens , cursor ) ) return null ;
175161 if ( value . slice ( markerEnd , line . end ) . trim ( ) === "" ) {
176162 return {
177163 start : line . start ,
@@ -184,10 +170,7 @@ export function listContinuationForEnter(value: string, cursor: number): Compose
184170 const cursorAfter = cursor + insertion . length ;
185171 const { marker } = parsed ;
186172 if ( marker . kind === "ordered" ) {
187- const number = Number . parseInt ( marker . numberText , 10 ) ;
188- const renumbered = Number . isSafeInteger ( number )
189- ? renumberFollowingItems ( value , line . end , marker , number + 2 )
190- : null ;
173+ const renumbered = renumberFollowingItems ( value , line . end , marker , tokens ) ;
191174 if ( renumbered ) {
192175 return {
193176 start : cursor ,
@@ -208,6 +191,6 @@ export function listIndentForTab(
208191 if ( ! Number . isInteger ( start ) || start !== end || start < 0 || start > value . length ) return null ;
209192 const line = currentLine ( value , start ) ;
210193 if ( ! parseListMarker ( line . text ) ) return null ;
211- if ( isInsideInlineToken ( value , start ) ) return null ;
194+ if ( isInsideInlineToken ( inlineTokenRanges ( value ) , start ) ) return null ;
212195 return { start : line . start , end : line . start , replacement : " " , cursorAfter : start + 2 } ;
213196}
0 commit comments