robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Helpers shared by the atom-capture specs (mcursor_spec.lua, cmdatom_spec.lua). 2 3local t =require ( 'test.testutil' ) 4local n =require ( 'test.functional.testnvim' )() 5 6local m ={} 7 8function m . get_lines () 9return n . buf_lines ( 0 ) 10end 11 12--- vim.keycode(): |key-notation| => the raw bytes of the CmdAtom event's keys/lhs. 13function m . k ( s ) 14return n . api . nvim_replace_termcodes ( s , true , true , true ) 15end 16 17--- Starts collecting CmdAtom event-data. 18function m . atoms_start () 19n . exec_lua ( [[ 20_G.atoms = {} 21vim.api.nvim_create_autocmd('CmdAtom', { 22callback = function(ev) 23table.insert(_G.atoms, ev.data) 24end, 25}) 26]] ) 27end 28 29--- Gets the collected CmdAtom event-data. 30function m . atoms () 31n . poke_eventloop () -- CmdAtom is deferred, so drain the event loop first. 32return n . exec_lua ( 'return _G.atoms' ) 33end 34 35--- Gets the last collected CmdAtom event. 36function m . atom_last () 37local evs =m . atoms () 38return evs [ # evs ] 39end 40 41--- Gets the last `count` collected CmdAtom events: bare `keys` strings by default, or 42--- projections of the named `fields`. 43function m . atoms_tail ( count , ... ) 44local evs =m . atoms () 45local fields =select ( '#' , ... ) > 0 and { ... } or nil 46local tail ={} 47for i =# evs - count + 1 , # evs do 48table . insert ( tail , fields and t . pick ( evs [ i ], unpack ( fields )) or evs [ i ]. keys ) 49end 50return tail 51end 52 53--- Gets `ev`'s subatoms (`CmdAtom.atoms`): bare `keys` strings by default, or projections of the 54--- named `fields`. 55function m . subatoms ( ev , ... ) 56local fields =select ( '#' , ... ) > 0 and { ... } or nil 57local subs ={} 58for _ , c in ipairs ( ev . atoms ) do 59table . insert ( subs , fields and t . pick ( c , unpack ( fields )) or c . keys ) 60end 61return subs 62end 63 64--- Minimal vim-surround "ys": an <expr> mapping sets 'operatorfunc' and returns "g@"; the opfunc 65--- reads the wrap char with getchar() and wraps the motion region (yank, modify register, paste 66--- back). 67m . minisurround_vim =[[ 68function! MiniSurroundSetup() abort 69set operatorfunc=MiniSurround 70return 'g@' 71endfunction 72function! MiniSurround(type) abort 73let char = nr2char(getchar()) 74let save = getreg('"') 75silent exe "norm! v`[o`]y" 76call setreg('"', char .. getreg('"') .. char, 'v') 77silent exe "norm! gvp`[" 78call setreg('"', save) 79endfunction 80nnoremap <expr> ys MiniSurroundSetup() 81]] 82 83--- Minimal vim-sneak: :omap whose ":call" reads a 2-char getchar() and moves the cursor. 84m . minisneak_vim =[[ 85function! MiniSneak() abort 86let c1 = nr2char(getchar()) 87let c2 = nr2char(getchar()) 88call search('\V' . c1 . c2, 'W') 89endfunction 90onoremap <silent> z :<C-U>call MiniSneak()<CR> 91]] 92 93--- Minimal vim-surround "ds": a ":call" mapping whose edit runs through :normal inside a 94--- function, with a getchar() payload naming the surround to delete. 95m . delsurround_vim =[[ 96function! DelSurround() abort 97call getchar() 98" cursor is on the "("; delete it and its matching ")". 99normal! mz%x`zx 100endfunction 101nnoremap <silent> ds :<C-U>call DelSurround()<CR> 102]] 103 104return m