robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local api =n . api 6local clear =n . clear 7local command =n . command 8local feed =n . feed 9local poke_eventloop =n . poke_eventloop 10local eval =n . eval 11 12local eq =t . eq 13local neq =t . neq 14 15describe ( 'MarkSet' , function () 16-- TODO(justinmk): support other marks?: [, ] <, > . ^ " ' 17 18before_each ( function () 19clear () 20end ) 21 22it ( 'emits when lowercase/uppercase/[/] marks are set' , function () 23command ( [[ 24let g:mark_names = '' 25let g:mark_events = [] 26autocmd MarkSet * call add(g:mark_events, {'event': deepcopy(v:event)}) | let g:mark_names ..= expand('<amatch>') 27" TODO: there is a bug lurking here. 28" autocmd MarkSet * let g:mark_names ..= expand('<amatch>') 29]] ) 30 31api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 32'foo\0bar' , 33'baz text' , 34'line 3' , 35} ) 36 37feed ( 'ma' ) 38feed ( 'j' ) 39command ( 'mark b' ) 40 41poke_eventloop () 42eq ( 'ab' , eval ( 'g:mark_names' )) 43 44-- event-data is copied to `v:event`. 45eq ( { 46{ 47event ={ 48col =0 , 49line =1 , 50name ='a' , 51} , 52} , 53{ 54event ={ 55col =0 , 56line =2 , 57name ='b' , 58} , 59} , 60} , eval ( 'g:mark_events' )) 61 62feed ( 'mA' ) 63feed ( 'l' ) 64feed ( 'mB' ) 65feed ( 'j' ) 66feed ( 'mC' ) 67 68feed ( 'x' ) -- TODO(justinmk): Sets [,] marks but does not emit MarkSet event (yet). 69feed ( '0vll<esc>' ) -- TODO(justinmk): Sets <,> marks but does not emit MarkSet event (yet). 70-- XXX: set these marks manually to exercise these cases. 71api . nvim_buf_set_mark ( 0 , '[' , 2 , 0 , {} ) 72api . nvim_buf_set_mark ( 0 , ']' , 2 , 0 , {} ) 73api . nvim_buf_set_mark ( 0 , '<' , 2 , 0 , {} ) 74api . nvim_buf_set_mark ( 0 , '>' , 2 , 0 , {} ) 75api . nvim_buf_set_mark ( 0 , '"' , 2 , 0 , {} ) 76 77poke_eventloop () 78eq ( 'abABC[]<>"' , eval ( 'g:mark_names' )) 79end ) 80 81it ( 'can subscribe to specific marks by pattern' , function () 82command ( [[ 83let g:mark_names = '' 84autocmd MarkSet [ab] let g:mark_names ..= expand('<amatch>') 85]] ) 86 87api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 88'foo\0bar' , 89'baz text' , 90} ) 91 92feed ( 'md' ) 93feed ( 'mc' ) 94feed ( 'l' ) 95feed ( 'mb' ) 96feed ( 'j' ) 97feed ( 'ma' ) 98 99poke_eventloop () 100eq ( 'ba' , eval ( 'g:mark_names' )) 101end ) 102 103it ( 'handles marks across multiple windows/buffers' , function () 104local orig_bufnr =api . nvim_get_current_buf () 105 106command ( 'enew' ) 107local second_bufnr =api . nvim_get_current_buf () 108api . nvim_buf_set_lines ( second_bufnr , 0 , - 1 , true , { 109'second buffer line 1' , 110'second buffer line 2' , 111} ) 112 113command ( 'enew' ) 114local third_bufnr =api . nvim_get_current_buf () 115api . nvim_buf_set_lines ( third_bufnr , 0 , - 1 , true , { 116'third buffer line 1' , 117'third buffer line 2' , 118} ) 119 120command ( 'split' ) 121command ( 'vsplit' ) 122 123command ( 'tabnew' ) 124command ( 'split' ) 125 126command ( [[ 127let g:markset_events = [] 128autocmd MarkSet * call add(g:markset_events, { 'buf': 0 + expand('<abuf>'), 'event': deepcopy(v:event) }) 129]] ) 130 131command ( 'buffer ' .. orig_bufnr ) 132feed ( 'gg' ) 133feed ( 'mA' ) 134 135command ( 'wincmd w' ) 136command ( 'tabnext' ) 137 138feed ( 'mB' ) 139 140command ( 'wincmd w' ) 141command ( 'enew' ) 142 143local final_bufnr =api . nvim_get_current_buf () 144api . nvim_buf_set_lines ( final_bufnr , 0 , - 1 , true , { 145'final buffer after chaos' , 146'line 2 of final buffer' , 147} ) 148 149feed ( 'j' ) 150feed ( 'mC' ) 151 152command ( 'tabclose' ) 153 154feed ( 'mD' ) 155-- delmarks should signal on the buf the mark is set on, not the current buf 156command ( 'delmarks A' ) 157 158poke_eventloop () 159eq ( { 160{ 161buf =1 , 162event ={ 163col =0 , 164line =1 , 165name ='A' , 166} , 167} , 168{ 169buf =2 , 170event ={ 171col =0 , 172line =1 , 173name ='B' , 174} , 175} , 176{ 177buf =4 , 178event ={ 179col =0 , 180line =2 , 181name ='C' , 182} , 183} , 184{ 185buf =3 , 186event ={ 187col =0 , 188line =1 , 189name ='D' , 190} , 191} , 192{ 193buf =1 , 194event ={ 195col =0 , 196line =0 , 197name ='A' , 198} , 199} , 200} , eval ( 'g:markset_events' )) 201end ) 202 203it ( 'handles an autocommand that calls bwipeout!' , function () 204api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 205'line 1' , 206'line 2' , 207'line 3' , 208} ) 209 210local test_bufnr =api . nvim_get_current_buf () 211 212command ( "autocmd MarkSet * let g:autocmd ..= expand('<amatch>') | bwipeout!" ) 213command ( [[let g:autocmd = '']] ) 214 215feed ( 'ma' ) 216poke_eventloop () 217 218eq ( 'a' , eval ( 'g:autocmd' )) 219 220eq ( false , api . nvim_buf_is_valid ( test_bufnr )) 221 222local current_bufnr =api . nvim_get_current_buf () 223neq ( current_bufnr , test_bufnr ) 224end ) 225 226it ( 'when autocommand switches windows and tabs' , function () 227api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 228'first buffer line 1' , 229'first buffer line 2' , 230'first buffer line 3' , 231} ) 232local first_bufnr =api . nvim_get_current_buf () 233 234command ( 'split' ) 235command ( 'enew' ) 236api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 237'second buffer line 1' , 238'second buffer line 2' , 239} ) 240local second_bufnr =api . nvim_get_current_buf () 241 242command ( 'tabnew' ) 243api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 244'third buffer line 1' , 245'third buffer line 2' , 246'third buffer line 3' , 247} ) 248local third_bufnr =api . nvim_get_current_buf () 249 250command ( [[ 251let g:markset_events = [] 252autocmd MarkSet * call add(g:markset_events, {'buf': 0 + expand('<abuf>'), 'event': deepcopy(v:event)}) | wincmd w | tabnext 253]] ) 254 255command ( 'buffer ' .. second_bufnr ) 256feed ( 'j' ) 257feed ( 'mA' ) 258command ( 'buffer ' .. third_bufnr ) 259feed ( 'l' ) 260feed ( 'mB' ) 261command ( 'buffer ' .. first_bufnr ) 262feed ( 'jj' ) 263feed ( 'mC' ) 264poke_eventloop () 265 266eq ( { 267{ 268buf =2 , 269event ={ 270col =0 , 271line =2 , 272name ='A' , 273} , 274} , 275{ 276buf =3 , 277event ={ 278col =1 , 279line =1 , 280name ='B' , 281} , 282} , 283{ 284buf =1 , 285event ={ 286col =0 , 287line =3 , 288name ='C' , 289} , 290} , 291} , eval ( 'g:markset_events' )) 292 293eq ( { 2 , 0 } , api . nvim_buf_get_mark ( second_bufnr , 'A' )) 294eq ( { 1 , 1 } , api . nvim_buf_get_mark ( third_bufnr , 'B' )) 295eq ( { 3 , 0 } , api . nvim_buf_get_mark ( first_bufnr , 'C' )) 296end ) 297 298it ( 'emits when marks are deleted' , function () 299command ( [[ 300let g:mark_events = [] 301autocmd MarkSet * call add(g:mark_events, {'event': deepcopy(v:event)}) 302]] ) 303 304api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 305'line 1' , 306'line 2' , 307} ) 308 309feed ( 'ma' ) 310feed ( 'mb' ) 311command ( 'delmarks a' ) 312-- deleting a second time should not trigger an event 313command ( 'delmarks a' ) 314api . nvim_buf_del_mark ( 0 , 'b' ) 315api . nvim_buf_del_mark ( 0 , 'b' ) 316 317poke_eventloop () 318 319eq ( { 320{ 321event ={ 322col =0 , 323line =1 , 324name ='a' , 325} , 326} , 327{ 328event ={ 329col =0 , 330line =1 , 331name ='b' , 332} , 333} , 334{ 335event ={ 336col =0 , 337line =0 , 338name ='a' , 339} , 340} , 341{ 342event ={ 343col =0 , 344line =0 , 345name ='b' , 346} , 347} , 348} , eval ( 'g:mark_events' )) 349end ) 350end )