robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
3.0 KiB104 linesraw
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()
9  return 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)
14  return n.api.nvim_replace_termcodes(s, true, true, true)
15end
16
17--- Starts collecting CmdAtom event-data.
18function m.atoms_start()
19  n.exec_lua([[
20    _G.atoms = {}
21    vim.api.nvim_create_autocmd('CmdAtom', {
22      callback = function(ev)
23        table.insert(_G.atoms, ev.data)
24      end,
25    })
26  ]])
27end
28
29--- Gets the collected CmdAtom event-data.
30function m.atoms()
31  n.poke_eventloop() -- CmdAtom is deferred, so drain the event loop first.
32  return n.exec_lua('return _G.atoms')
33end
34
35--- Gets the last collected CmdAtom event.
36function m.atom_last()
37  local evs = m.atoms()
38  return 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, ...)
44  local evs = m.atoms()
45  local fields = select('#', ...) > 0 and { ... } or nil
46  local tail = {}
47  for i = #evs - count + 1, #evs do
48    table.insert(tail, fields and t.pick(evs[i], unpack(fields)) or evs[i].keys)
49  end
50  return 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, ...)
56  local fields = select('#', ...) > 0 and { ... } or nil
57  local subs = {}
58  for _, c in ipairs(ev.atoms) do
59    table.insert(subs, fields and t.pick(c, unpack(fields)) or c.keys)
60  end
61  return 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 = [[
68  function! MiniSurroundSetup() abort
69    set operatorfunc=MiniSurround
70    return 'g@'
71  endfunction
72  function! MiniSurround(type) abort
73    let char = nr2char(getchar())
74    let save = getreg('"')
75    silent exe "norm! v`[o`]y"
76    call setreg('"', char .. getreg('"') .. char, 'v')
77    silent exe "norm! gvp`["
78    call setreg('"', save)
79  endfunction
80  nnoremap <expr> ys MiniSurroundSetup()
81]]
82
83--- Minimal vim-sneak: :omap whose ":call" reads a 2-char getchar() and moves the cursor.
84m.minisneak_vim = [[
85  function! MiniSneak() abort
86    let c1 = nr2char(getchar())
87    let c2 = nr2char(getchar())
88    call search('\V' . c1 . c2, 'W')
89  endfunction
90  onoremap <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 = [[
96  function! DelSurround() abort
97    call getchar()
98    " cursor is on the "("; delete it and its matching ")".
99    normal! mz%x`zx
100  endfunction
101  nnoremap <silent> ds :<C-U>call DelSurround()<CR>
102]]
103
104return m