@@ -4,6 +4,8 @@ export interface ComposerListEdit {
44 start : number ;
55 end : number ;
66 replacement : string ;
7+ /** Expanded caret offset after the edit is applied. */
8+ cursorAfter : number ;
79}
810
911type ListMarker =
@@ -64,21 +66,80 @@ function nextMarkerText(marker: ListMarker): string {
6466 return `${ marker . indent } ${ marker . bullet } ` ;
6567}
6668
67- function segmentSource (
68- segment : ReturnType < typeof splitPromptIntoComposerSegments > [ number ] ,
69- ) : string {
70- if ( segment . type === "text" ) return segment . text ;
71- return segment . source ;
69+ // Markdown tabs advance to the next four-column stop.
70+ function indentWidth ( line : string ) : number {
71+ let width = 0 ;
72+ for ( const char of line ) {
73+ if ( char === " " ) width += 1 ;
74+ else if ( char === "\t" ) width += 4 - ( width % 4 ) ;
75+ else break ;
76+ }
77+ return width ;
78+ }
79+
80+ function renumberFollowingItems (
81+ value : string ,
82+ afterLineEnd : number ,
83+ marker : Extract < ListMarker , { kind : "ordered" } > ,
84+ tokens : ReturnType < typeof inlineTokenRanges > ,
85+ ) : { end : number ; text : string } | null {
86+ const baseWidth = indentWidth ( marker . indent ) ;
87+ const parts : string [ ] = [ ] ;
88+ let expected = Number . parseInt ( marker . numberText , 10 ) + 2 ;
89+ let position = afterLineEnd ;
90+ let copiedUntil = afterLineEnd ;
91+ let tokenIndex = 0 ;
92+ while ( position < value . length && Number . isSafeInteger ( expected ) ) {
93+ const lineStart = position + 1 ;
94+ const nextBreak = value . indexOf ( "\n" , lineStart ) ;
95+ const lineEnd = nextBreak === - 1 ? value . length : nextBreak ;
96+ position = lineEnd ;
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+
105+ const line = value . slice ( lineStart , lineEnd ) ;
106+ const width = indentWidth ( line ) ;
107+ if ( line . trim ( ) === "" || width < baseWidth ) break ;
108+ if ( width > baseWidth ) continue ;
109+ const parsed = parseListMarker ( line ) ;
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 ;
121+ }
122+ expected += 1 ;
123+ }
124+ return parts . length === 0 ? null : { end : copiedUntil , text : parts . join ( "" ) } ;
72125}
73126
74- function isInsideInlineToken ( value : string , cursor : number ) : boolean {
127+ function inlineTokenRanges ( value : string ) : Array < { start : number ; end : number } > {
128+ const ranges : Array < { start : number ; end : number } > = [ ] ;
75129 let offset = 0 ;
76130 for ( const segment of splitPromptIntoComposerSegments ( value ) ) {
77- const end = offset + segmentSource ( segment ) . length ;
78- if ( segment . type !== "text" && cursor > offset && cursor < end ) return true ;
131+ const end = offset + ( segment . type === "text" ? segment . text : segment . source ) . length ;
132+ if ( segment . type !== "text" ) ranges . push ( { start : offset , end } ) ;
79133 offset = end ;
80134 }
81- return false ;
135+ return ranges ;
136+ }
137+
138+ function isInsideInlineToken (
139+ tokens : ReturnType < typeof inlineTokenRanges > ,
140+ cursor : number ,
141+ ) : boolean {
142+ return tokens . some ( ( range ) => cursor > range . start && cursor < range . end ) ;
82143}
83144
84145function currentLine ( value : string , cursor : number ) : { start : number ; end : number ; text : string } {
@@ -95,15 +156,31 @@ export function listContinuationForEnter(value: string, cursor: number): Compose
95156 if ( ! parsed ) return null ;
96157 const markerEnd = line . start + parsed . markerEnd ;
97158 if ( cursor < markerEnd ) return null ;
98- if ( isInsideInlineToken ( value , cursor ) ) return null ;
159+ const tokens = inlineTokenRanges ( value ) ;
160+ if ( isInsideInlineToken ( tokens , cursor ) ) return null ;
99161 if ( value . slice ( markerEnd , line . end ) . trim ( ) === "" ) {
100- return { start : line . start , end : Math . max ( cursor , markerEnd ) , replacement : "" } ;
162+ return {
163+ start : line . start ,
164+ end : Math . max ( cursor , markerEnd ) ,
165+ replacement : "" ,
166+ cursorAfter : line . start ,
167+ } ;
168+ }
169+ const insertion = `\n${ nextMarkerText ( parsed . marker ) } ` ;
170+ const cursorAfter = cursor + insertion . length ;
171+ const { marker } = parsed ;
172+ if ( marker . kind === "ordered" ) {
173+ const renumbered = renumberFollowingItems ( value , line . end , marker , tokens ) ;
174+ if ( renumbered ) {
175+ return {
176+ start : cursor ,
177+ end : renumbered . end ,
178+ replacement : insertion + value . slice ( cursor , line . end ) + renumbered . text ,
179+ cursorAfter,
180+ } ;
181+ }
101182 }
102- return {
103- start : cursor ,
104- end : cursor ,
105- replacement : `\n${ nextMarkerText ( parsed . marker ) } ` ,
106- } ;
183+ return { start : cursor , end : cursor , replacement : insertion , cursorAfter } ;
107184}
108185
109186export function listIndentForTab (
@@ -114,6 +191,6 @@ export function listIndentForTab(
114191 if ( ! Number . isInteger ( start ) || start !== end || start < 0 || start > value . length ) return null ;
115192 const line = currentLine ( value , start ) ;
116193 if ( ! parseListMarker ( line . text ) ) return null ;
117- if ( isInsideInlineToken ( value , start ) ) return null ;
118- return { start : line . start , end : line . start , replacement : " " } ;
194+ if ( isInsideInlineToken ( inlineTokenRanges ( value ) , start ) ) return null ;
195+ return { start : line . start , end : line . start , replacement : " " , cursorAfter : start + 2 } ;
119196}
0 commit comments