@@ -5,16 +5,18 @@ import { splitPromptIntoComposerSegments } from "./composer-editor-mentions";
55 *
66 * Implemented once at the ChatComposer level (store replacement), so both
77 * composer modes behave identically and serialize identically:
8- * Shift+Enter on a list item continues it, Enter on an empty item exits the
9- * list, and Tab indents the item. Plain Markdown markers only — no real list
10- * nodes anywhere.
8+ * Shift+Enter on a list item continues it, Enter on an empty item leaves one
9+ * level of nesting (or the list) , and Tab nests the item under the one above.
10+ * Plain Markdown markers only — no real list nodes anywhere.
1111 */
1212
1313export interface ComposerListEdit {
1414 /** Expanded (plain string) offsets into the prompt. */
1515 start : number ;
1616 end : number ;
1717 replacement : string ;
18+ /** Expanded caret after the edit; defaults to the end of the replacement. */
19+ cursor ?: number ;
1820}
1921
2022type ListMarker =
@@ -98,17 +100,63 @@ function isInsideInlineToken(value: string, cursor: number): boolean {
98100 return false ;
99101}
100102
101- function currentLine ( value : string , cursor : number ) : { start : number ; end : number ; text : string } {
103+ interface Line {
104+ start : number ;
105+ end : number ;
106+ text : string ;
107+ }
108+
109+ function currentLine ( value : string , cursor : number ) : Line {
102110 const start = value . lastIndexOf ( "\n" , cursor - 1 ) + 1 ;
103111 const endIndex = value . indexOf ( "\n" , cursor ) ;
104112 const end = endIndex === - 1 ? value . length : endIndex ;
105113 return { start, end, text : value . slice ( start , end ) } ;
106114}
107115
116+ /** The list lines directly above `line`, nearest first, up to the first non-list line. */
117+ function * listLinesAbove ( value : string , line : Line ) : Generator < ListMarker > {
118+ let start = line . start ;
119+ while ( start > 0 ) {
120+ const previous = currentLine ( value , start - 1 ) ;
121+ const parsed = parseListMarker ( previous . text ) ;
122+ if ( ! parsed ) return ;
123+ yield parsed . marker ;
124+ start = previous . start ;
125+ }
126+ }
127+
128+ function isDeeper ( indent : string , than : string ) : boolean {
129+ return indent . length > than . length && indent . startsWith ( than ) ;
130+ }
131+
132+ /**
133+ * How far a child must be indented to nest under this item: its content
134+ * column, as CommonMark counts it. Two spaces under `1. ` would read as a
135+ * sibling, so the agent would get a flat list.
136+ */
137+ function childIndentWidth ( marker : ListMarker ) : number {
138+ // A task's box is item content, so its children nest under the dash.
139+ if ( marker . kind === "task" ) return 2 ;
140+ const markerText =
141+ marker . kind === "ordered" ? `${ marker . numberText } ${ marker . delimiter } ` : marker . bullet ;
142+ const space = marker . space . length ;
143+ return markerText . length + ( space >= 1 && space <= 4 ? space : 1 ) ;
144+ }
145+
146+ /** The nearest item above at a shallower indent: the one `indent` nests under. */
147+ function parentMarker ( value : string , line : Line , indent : string ) : ListMarker | null {
148+ if ( indent === "" ) return null ;
149+ for ( const marker of listLinesAbove ( value , line ) ) {
150+ if ( isDeeper ( indent , marker . indent ) ) return marker ;
151+ }
152+ return null ;
153+ }
154+
108155/**
109- * Enter on a list item line: continue the list, or exit it when the item is
110- * empty. Returns null for non-list lines, carets inside the marker, and
111- * carets inside an inline chip — all fall through to a plain newline.
156+ * Enter on a list item line: continue the list, or, on an empty item, leave
157+ * one level of nesting or the list itself. Returns null for non-list lines,
158+ * carets inside the marker, and carets inside an inline chip — all fall
159+ * through to a plain newline.
112160 */
113161export function listContinuationForEnter ( value : string , cursor : number ) : ComposerListEdit | null {
114162 if ( ! Number . isInteger ( cursor ) || cursor < 0 || cursor > value . length ) return null ;
@@ -119,8 +167,14 @@ export function listContinuationForEnter(value: string, cursor: number): Compose
119167 if ( cursor < markerEnd ) return null ;
120168 if ( isInsideInlineToken ( value , cursor ) ) return null ;
121169 if ( value . slice ( markerEnd , line . end ) . trim ( ) === "" ) {
122- // Empty item: Enter exits the list by removing the marker.
123- return { start : line . start , end : Math . max ( cursor , markerEnd ) , replacement : "" } ;
170+ // Empty item: a nested one moves up a level and continues its parent's
171+ // list; a top-level one exits the list by removing the marker.
172+ const parent = parentMarker ( value , line , parsed . marker . indent ) ;
173+ return {
174+ start : line . start ,
175+ end : Math . max ( cursor , markerEnd ) ,
176+ replacement : parent ? nextMarkerText ( parent ) : "" ,
177+ } ;
124178 }
125179 return {
126180 start : cursor ,
@@ -129,19 +183,85 @@ export function listContinuationForEnter(value: string, cursor: number): Compose
129183 } ;
130184}
131185
186+ interface ListPrefix {
187+ indent : string ;
188+ numberText ?: string ;
189+ }
190+
191+ /** The list item line at a collapsed caret, unless the caret splits an inline chip. */
192+ function listLineAt (
193+ value : string ,
194+ start : number ,
195+ end : number ,
196+ ) : { line : Line ; marker : ListMarker } | null {
197+ if ( ! Number . isInteger ( start ) || start !== end || start < 0 || start > value . length ) return null ;
198+ const line = currentLine ( value , start ) ;
199+ const parsed = parseListMarker ( line . text ) ;
200+ if ( ! parsed || isInsideInlineToken ( value , start ) ) return null ;
201+ return { line, marker : parsed . marker } ;
202+ }
203+
204+ /** Rewrites the item's indent and, for ordered items, its number, keeping the caret on the text. */
205+ function rewritePrefix (
206+ line : Line ,
207+ marker : ListMarker ,
208+ prefix : ListPrefix ,
209+ cursor : number ,
210+ ) : ComposerListEdit {
211+ const editEnd =
212+ line . start + marker . indent . length + ( marker . kind === "ordered" ? marker . numberText . length : 0 ) ;
213+ const replacement = `${ prefix . indent } ${ marker . kind === "ordered" ? ( prefix . numberText ?? "1" ) : "" } ` ;
214+ const shift = replacement . length - ( editEnd - line . start ) ;
215+ return {
216+ start : line . start ,
217+ end : editEnd ,
218+ replacement,
219+ cursor : cursor >= editEnd ? cursor + shift : line . start + replacement . length ,
220+ } ;
221+ }
222+
132223/**
133- * Tab on a list item line: indent by two spaces. Ranged selections, non-list
134- * lines, and carets inside an inline chip fall through (Shift+Tab stays the
135- * plan-mode toggle and is handled before this is consulted).
224+ * Tab on a list item line: nest it under the item above at the same indent,
225+ * written at that item's content column. A nested ordered item continues the
226+ * numbering of the sublist it joins, or starts it at 1. With no item to nest
227+ * under it indents by two spaces. Ranged selections, non-list lines, and
228+ * carets inside an inline chip fall through (Shift+Tab stays the plan-mode
229+ * toggle and is handled before this is consulted).
136230 */
137231export function listIndentForTab (
138232 value : string ,
139233 start : number ,
140234 end : number ,
141235) : ComposerListEdit | null {
142- if ( ! Number . isInteger ( start ) || start !== end || start < 0 || start > value . length ) return null ;
143- const line = currentLine ( value , start ) ;
144- if ( ! parseListMarker ( line . text ) ) return null ;
145- if ( isInsideInlineToken ( value , start ) ) return null ;
146- return { start : line . start , end : line . start , replacement : " " } ;
236+ const at = listLineAt ( value , start , end ) ;
237+ if ( ! at ) return null ;
238+ const nested = nestedPrefix ( value , at . line , at . marker ) ;
239+ if ( ! nested ) {
240+ return { start : at . line . start , end : at . line . start , replacement : " " , cursor : start + 2 } ;
241+ }
242+ return rewritePrefix ( at . line , at . marker , nested , start ) ;
243+ }
244+
245+ function nestedPrefix ( value : string , line : Line , marker : ListMarker ) : ListPrefix | null {
246+ // Items already nested under the new parent, nearest first.
247+ const children : ListMarker [ ] = [ ] ;
248+ for ( const above of listLinesAbove ( value , line ) ) {
249+ if ( isDeeper ( above . indent , marker . indent ) ) {
250+ children . push ( above ) ;
251+ continue ;
252+ }
253+ if ( above . indent !== marker . indent ) return null ;
254+ // Join the sublist the item above already has, or start one at its content column.
255+ const sublistDepth = Math . min ( ...children . map ( ( child ) => child . indent . length ) ) ;
256+ const lastChild = children . find ( ( child ) => child . indent . length === sublistDepth ) ;
257+ const indent = lastChild ?. indent ?? `${ marker . indent } ${ " " . repeat ( childIndentWidth ( above ) ) } ` ;
258+ if ( marker . kind !== "ordered" ) return { indent } ;
259+ const sibling =
260+ lastChild ?. kind === "ordered" && lastChild . delimiter === marker . delimiter ? lastChild : null ;
261+ const numberText = sibling
262+ ? nextOrderedMarkerText ( `${ sibling . numberText } ${ sibling . delimiter } ` ) . slice ( 0 , - 1 )
263+ : "1" ;
264+ return { indent, numberText } ;
265+ }
266+ return null ;
147267}
0 commit comments