@@ -2,32 +2,37 @@ import type { Extension } from "@codemirror/state";
22import { EditorView } from "@codemirror/view" ;
33
44export const taskAutoComplete : Extension = EditorView . inputHandler . of ( ( view , from , to , text ) => {
5- const isValidInput = text === " " && from === to && from >= 2 ;
6- if ( ! isValidInput ) return false ;
5+ const isWhitespaceInput = from === to && text . length > 0 && / ^ [ \t ] + $ / . test ( text ) ;
6+ if ( ! isWhitespaceInput ) return false ;
77
88 const doc = view . state . doc ;
99 const line = doc . lineAt ( from ) ;
10- const linePrefix = doc . sliceString ( line . from , from ) ;
10+ const before = doc . sliceString ( line . from , from ) ;
11+ const after = doc . sliceString ( from , line . to ) ;
1112
12- const patterns = [
13- { suffix : "- [" , offset : 3 } ,
14- { suffix : "-[" , offset : 2 } ,
15- ] ;
13+ const bulletMatch = before . match ( / [ - * ] \s * \[ $ / ) ;
14+ if ( ! bulletMatch ) return false ;
1615
17- const matchedPattern = patterns . find ( ( { suffix } ) => linePrefix . endsWith ( suffix ) ) ;
18- if ( ! matchedPattern ) return false ;
16+ const bulletStart = before . length - bulletMatch [ 0 ] . length ;
17+ const indent = before . slice ( 0 , bulletStart ) ;
18+ if ( ! / ^ [ \t ] * $ / . test ( indent ) ) return false ;
1919
20- const insertFrom = from - matchedPattern . offset ;
21- if ( insertFrom < line . from ) return false ;
20+ // Avoid re-triggering on existing task list items like "- [ ]" or "- [x]"
21+ if ( / ^ [ \t ] * (?: [ x X ] \] | \] ) / . test ( after ) ) return false ;
2222
23- const replacement = "- [ ] " ;
23+ const insertFrom = line . from ;
24+ const bullet = bulletMatch [ 0 ] [ 0 ] ;
25+ const replacement = `${ indent } ${ bullet } [ ] ` ;
26+ // don't remove waste `]`
27+ const replaceTo = after . startsWith ( "]" ) && from + 1 <= line . to ? from + 1 : from ;
2428
2529 view . dispatch ( {
2630 changes : {
2731 from : insertFrom ,
28- to : from ,
32+ to : replaceTo ,
2933 insert : replacement ,
3034 } ,
35+ userEvent : "input.taskAutoComplete" ,
3136 selection : { anchor : insertFrom + replacement . length } ,
3237 } ) ;
3338
0 commit comments