minimal extui fuzzy finder for neovim
1---@class artio.Actions.proto
2---@field actions table<string, function>
3
4---@class artio.Actions : artio.Actions.proto
5---@field picker artio.Picker
6---@field co thread
7---@field key_ns integer
8local Actions = {}
9Actions.__index = Actions
10
11---@param props artio.Actions.proto
12function Actions:new(props)
13 return setmetatable(
14 vim.tbl_extend("force", {
15 actions = {},
16 }, props),
17 Actions
18 )
19end
20
21function Actions:init(picker)
22 self.picker = picker
23
24 local co, ismain = coroutine.running()
25 assert(not ismain, "must be called from a coroutine")
26 self.co = co
27
28 self.key_ns = vim.on_key(function(key, typed)
29 return self:on_key(key, typed)
30 end)
31
32 local result = coroutine.yield()
33
34 vim.on_key(nil, self.key_ns)
35
36 return result
37end
38
39function Actions:on_key(_, typed)
40 if self.picker.view.closed then
41 coroutine.resume(self.co)
42 return
43 end
44
45 typed = string.lower(vim.fn.keytrans(typed))
46
47 local _, actionname = vim.iter(pairs(self.picker.mappings)):find(function(key, _)
48 return key == typed
49 end)
50
51 local action = self.actions[actionname]
52 if action and vim.is_callable(action) then
53 action(self.picker, self.co)
54 return ""
55 end
56end
57
58return Actions