@@ -13,39 +13,60 @@ type ListMarker =
1313 | { kind : "task" ; indent : string ; space : string }
1414 | { kind : "bullet" ; indent : string ; bullet : string ; space : string } ;
1515
16- function parseListMarker ( line : string ) : { marker : ListMarker ; markerEnd : number } | null {
16+ interface ParsedListMarker {
17+ marker : ListMarker ;
18+ markerEnd : number ;
19+ /** Column a nested item must reach to belong to this item. */
20+ contentColumn : number ;
21+ }
22+
23+ function parseListMarker ( line : string ) : ParsedListMarker | null {
1724 const indent = line . match ( / ^ [ \t ] * / ) ?. [ 0 ] ?? "" ;
1825 const rest = line . slice ( indent . length ) ;
1926 const ordered = rest . match ( / ^ ( \d + ) ( [ . ) ] ) ( (?: [ \t ] + ) | \s * $ ) / ) ;
2027 if ( ordered && ordered [ 1 ] !== undefined && ordered [ 2 ] !== undefined ) {
28+ const space = ordered [ 3 ] ?? "" ;
2129 return {
2230 marker : {
2331 kind : "ordered" ,
2432 indent,
2533 numberText : ordered [ 1 ] ,
2634 delimiter : ordered [ 2 ] === ")" ? ")" : "." ,
27- space : ordered [ 3 ] ?? "" ,
35+ space,
2836 } ,
2937 markerEnd : indent . length + ordered [ 0 ] . length ,
38+ contentColumn : contentColumn ( indent , ordered [ 1 ] . length + 1 , space ) ,
3039 } ;
3140 }
32- const task = rest . match ( / ^ - \s \[ [ x X ] \] ( (?: [ \t ] + ) | \s * $ ) / ) ;
33- if ( task ) {
41+ // The checkbox is item content, so a task's content starts after "- ".
42+ const task = rest . match ( / ^ - ( \s ) \[ [ x X ] \] ( (?: [ \t ] + ) | \s * $ ) / ) ;
43+ if ( task && task [ 1 ] !== undefined ) {
3444 return {
35- marker : { kind : "task" , indent, space : task [ 1 ] ?? "" } ,
45+ marker : { kind : "task" , indent, space : task [ 2 ] ?? "" } ,
3646 markerEnd : indent . length + task [ 0 ] . length ,
47+ contentColumn : contentColumn ( indent , 1 , task [ 1 ] ) ,
3748 } ;
3849 }
3950 const bullet = rest . match ( / ^ ( [ - * + ] ) ( (?: [ \t ] + ) | \s * $ ) / ) ;
4051 if ( bullet && bullet [ 1 ] !== undefined ) {
52+ const space = bullet [ 2 ] ?? "" ;
4153 return {
42- marker : { kind : "bullet" , indent, bullet : bullet [ 1 ] , space : bullet [ 2 ] ?? "" } ,
54+ marker : { kind : "bullet" , indent, bullet : bullet [ 1 ] , space } ,
4355 markerEnd : indent . length + bullet [ 0 ] . length ,
56+ contentColumn : contentColumn ( indent , 1 , space ) ,
4457 } ;
4558 }
4659 return null ;
4760}
4861
62+ // CommonMark content follows 1-4 columns of padding; wider padding is code, so the content
63+ // column sits one column past the marker.
64+ function contentColumn ( indent : string , markerWidth : number , padding : string ) : number {
65+ const markerEnd = indentWidth ( indent ) + markerWidth ;
66+ const space = indentWidth ( padding , markerEnd ) ;
67+ return markerEnd + ( space >= 1 && space <= 4 ? space : 1 ) ;
68+ }
69+
4970export function nextOrderedMarkerText ( marker : string ) : string {
5071 const numberText = marker . slice ( 0 , - 1 ) ;
5172 const delimiter = marker . slice ( - 1 ) ;
@@ -67,14 +88,14 @@ function nextMarkerText(marker: ListMarker): string {
6788}
6889
6990// Markdown tabs advance to the next four-column stop.
70- function indentWidth ( line : string ) : number {
71- let width = 0 ;
91+ function indentWidth ( line : string , fromColumn = 0 ) : number {
92+ let column = fromColumn ;
7293 for ( const char of line ) {
73- if ( char === " " ) width += 1 ;
74- else if ( char === "\t" ) width += 4 - ( width % 4 ) ;
94+ if ( char === " " ) column += 1 ;
95+ else if ( char === "\t" ) column += 4 - ( column % 4 ) ;
7596 else break ;
7697 }
77- return width ;
98+ return column - fromColumn ;
7899}
79100
80101function renumberFollowingItems (
@@ -149,6 +170,55 @@ function currentLine(value: string, cursor: number): { start: number; end: numbe
149170 return { start, end, text : value . slice ( start , end ) } ;
150171}
151172
173+ function * listMarkersAbove (
174+ value : string ,
175+ lineStart : number ,
176+ tokens : ReturnType < typeof inlineTokenRanges > ,
177+ ) : Generator < ParsedListMarker > {
178+ let tokenIndex = tokens . length - 1 ;
179+ while ( lineStart > 0 ) {
180+ const lineEnd = lineStart - 1 ;
181+ lineStart = value . lastIndexOf ( "\n" , lineEnd - 1 ) + 1 ;
182+ // A newline inside a chip does not start a list item.
183+ for ( let token = tokens [ tokenIndex ] ; token ; token = tokens [ tokenIndex ] ) {
184+ if ( token . start >= lineStart ) tokenIndex -= 1 ;
185+ else if ( token . end > lineStart ) lineStart = value . lastIndexOf ( "\n" , token . start - 1 ) + 1 ;
186+ else break ;
187+ }
188+ const parsed = parseListMarker ( value . slice ( lineStart , lineEnd ) ) ;
189+ if ( ! parsed ) return ;
190+ yield parsed ;
191+ }
192+ }
193+
194+ function parentMarker (
195+ value : string ,
196+ lineStart : number ,
197+ marker : ListMarker ,
198+ tokens : ReturnType < typeof inlineTokenRanges > ,
199+ ) : ListMarker | null {
200+ const width = indentWidth ( marker . indent ) ;
201+ for ( const above of listMarkersAbove ( value , lineStart , tokens ) ) {
202+ if ( indentWidth ( above . marker . indent ) < width ) return above . marker ;
203+ }
204+ return null ;
205+ }
206+
207+ function nestedNumber (
208+ marker : ListMarker ,
209+ previousChild : ListMarker | undefined ,
210+ ) : { replacedLength : number ; text : string } {
211+ if ( marker . kind !== "ordered" ) return { replacedLength : 0 , text : "" } ;
212+ const continues =
213+ previousChild ?. kind === "ordered" && previousChild . delimiter === marker . delimiter ;
214+ return {
215+ replacedLength : marker . numberText . length ,
216+ text : continues
217+ ? nextOrderedMarkerText ( `${ previousChild . numberText } ${ marker . delimiter } ` ) . slice ( 0 , - 1 )
218+ : "1" ,
219+ } ;
220+ }
221+
152222export function listContinuationForEnter ( value : string , cursor : number ) : ComposerListEdit | null {
153223 if ( ! Number . isInteger ( cursor ) || cursor < 0 || cursor > value . length ) return null ;
154224 const line = currentLine ( value , cursor ) ;
@@ -159,12 +229,21 @@ export function listContinuationForEnter(value: string, cursor: number): Compose
159229 const tokens = inlineTokenRanges ( value ) ;
160230 if ( isInsideInlineToken ( tokens , cursor ) ) return null ;
161231 if ( value . slice ( markerEnd , line . end ) . trim ( ) === "" ) {
162- return {
163- start : line . start ,
164- end : Math . max ( cursor , markerEnd ) ,
165- replacement : "" ,
166- cursorAfter : line . start ,
167- } ;
232+ const parent = parentMarker ( value , line . start , parsed . marker , tokens ) ;
233+ const replacement = parent ? nextMarkerText ( parent ) : "" ;
234+ const cursorAfter = line . start + replacement . length ;
235+ const end = Math . max ( cursor , markerEnd ) ;
236+ const renumbered =
237+ parent ?. kind === "ordered" ? renumberFollowingItems ( value , line . end , parent , tokens ) : null ;
238+ if ( renumbered ) {
239+ return {
240+ start : line . start ,
241+ end : renumbered . end ,
242+ replacement : replacement + value . slice ( end , line . end ) + renumbered . text ,
243+ cursorAfter,
244+ } ;
245+ }
246+ return { start : line . start , end, replacement, cursorAfter } ;
168247 }
169248 const insertion = `\n${ nextMarkerText ( parsed . marker ) } ` ;
170249 const cursorAfter = cursor + insertion . length ;
@@ -190,7 +269,36 @@ export function listIndentForTab(
190269) : ComposerListEdit | null {
191270 if ( ! Number . isInteger ( start ) || start !== end || start < 0 || start > value . length ) return null ;
192271 const line = currentLine ( value , start ) ;
193- if ( ! parseListMarker ( line . text ) ) return null ;
194- if ( isInsideInlineToken ( inlineTokenRanges ( value ) , start ) ) return null ;
272+ const parsed = parseListMarker ( line . text ) ;
273+ if ( ! parsed ) return null ;
274+ const tokens = inlineTokenRanges ( value ) ;
275+ if ( isInsideInlineToken ( tokens , start ) ) return null ;
276+ const { marker } = parsed ;
277+ const width = indentWidth ( marker . indent ) ;
278+ let previousChild : ListMarker | undefined ;
279+ for ( const above of listMarkersAbove ( value , line . start , tokens ) ) {
280+ const aboveWidth = indentWidth ( above . marker . indent ) ;
281+ if ( aboveWidth < width ) break ;
282+ if ( aboveWidth > width ) {
283+ if ( ! previousChild || aboveWidth < indentWidth ( previousChild . indent ) ) {
284+ previousChild = above . marker ;
285+ }
286+ continue ;
287+ }
288+ const indent =
289+ previousChild ?. indent ?? above . marker . indent + " " . repeat ( above . contentColumn - aboveWidth ) ;
290+ const number = nestedNumber ( marker , previousChild ) ;
291+ const replacedEnd = line . start + marker . indent . length + number . replacedLength ;
292+ const replacement = indent + number . text ;
293+ return {
294+ start : line . start ,
295+ end : replacedEnd ,
296+ replacement,
297+ cursorAfter :
298+ start < replacedEnd
299+ ? line . start + indent . length
300+ : start - replacedEnd + line . start + replacement . length ,
301+ } ;
302+ }
195303 return { start : line . start , end : line . start , replacement : " " , cursorAfter : start + 2 } ;
196304}
0 commit comments