robin/artio.nvim

minimal ui2 fuzzy finder for Neovim

git clone https://git.robinwobin.dev/robin/artio.nvim

robinfeat!: collapse qflist actions into one8fb9b8a

main
5.0 KiB216 linesraw
1local uv = vim.uv
2
3local utils = {}
4
5---@param path string
6---@param ctx? vim.context.mods
7function utils.edit(path, ctx)
8  local f = function()
9    vim.api.nvim_cmd({
10      cmd = "edit",
11      args = { path },
12      magic = { file = false, bar = false },
13    }, {})
14  end
15
16  if ctx then
17    return vim._with(ctx, f)
18  end
19
20  return f()
21end
22
23local function parsechunks(chunks)
24  local lines = {}
25
26  for _, chunk in ipairs(chunks) do
27    for _, line in ipairs(vim.split(chunk, "\n", { trimempty = true })) do
28      table.insert(lines, line)
29    end
30  end
31
32  return lines
33end
34
35---@param prg? string
36---@param opts? table
37---@return fun(arg?: string): string[]
38function utils.make_cmd(prg, opts)
39  ---@async
40  ---@param arg? string
41  ---@return string[]
42  return function(arg)
43    if not prg then
44      return {}
45    end
46    local cmd, n = prg, nil
47    if arg and #arg > 0 then
48      arg = string.format("'%s'", arg)
49      cmd, n = prg:gsub("%$%*", arg)
50      if n == 0 then
51        cmd = ("%s %s"):format(prg, arg)
52      end
53    end
54
55    local chunks = {}
56    local err_chunks = {}
57
58    local stdout = uv.new_pipe()
59    local stderr = uv.new_pipe()
60
61    local co = coroutine.running()
62    assert(co, "utils.make_cmd needs to be run inside a coroutine")
63
64    uv.spawn(
65      vim.o.shell,
66      vim.tbl_extend("keep", {
67        stdio = { nil, stdout, stderr },
68        args = { vim.o.shellcmdflag, cmd },
69      }, opts or {}),
70      function(code, signal)
71        if code == 0 then
72          local lines = parsechunks(chunks)
73          coroutine.resume(co, lines)
74          return
75        end
76        local err = ("error while running shell cmd %s (%d)"):format(signal, code)
77        table.insert(err_chunks, 1, err)
78        local lines = parsechunks(err_chunks)
79        coroutine.resume(co, lines, true)
80      end
81    )
82
83    ---@diagnostic disable-next-line: param-type-mismatch
84    uv.read_start(stdout, function(err, data)
85      assert(not err, err)
86      if data then
87        table.insert(chunks, data)
88      end
89    end)
90
91    ---@diagnostic disable-next-line: param-type-mismatch
92    uv.read_start(stderr, function(err, data)
93      assert(not err, err)
94      if data then
95        table.insert(err_chunks, data)
96      end
97    end)
98
99    return coroutine.yield()
100  end
101end
102
103---@param fn fun(item: artio.Picker.item): vim.quickfix.entry
104---@return artio.Picker.action
105function utils.make_setqflist(fn)
106  return require("artio").wrap(function(self)
107    if #(vim.tbl_keys(self.marked)) > 0 then
108      vim.fn.setqflist(vim
109        .iter(ipairs(self:getmarked()))
110        :map(function(_, id)
111          local item = self.items[id]
112          local qfitem = fn(item)
113          return qfitem
114        end)
115        :totable())
116      self:cancel()
117      return
118    end
119
120    vim.fn.setqflist(vim
121      .iter(ipairs(self.matches))
122      :map(function(_, match)
123        local item = self.items[match[1]]
124        local qfitem = fn(item)
125        return qfitem
126      end)
127      :totable())
128    self:cancel()
129  end, function(_)
130    vim.cmd.copen()
131  end)
132end
133
134---@param fn fun(item: artio.Picker.item): vim.quickfix.entry
135---@return table<string, artio.Picker.action>
136function utils.make_setqflistaction(fn)
137  return {
138    setqflist = utils.make_setqflist(fn),
139  }
140end
141
142---@param fn fun(item: artio.Picker.item): integer
143---@return table<string, artio.Picker.action>
144function utils.make_fileactions(fn)
145  return {
146    split = require("artio").wrap(function(self)
147      self:cancel()
148    end, function(self)
149      local item = self:getcurrent()
150      if not item then
151        return
152      end
153      local buf = fn(item)
154      vim.api.nvim_open_win(buf, true, { win = -1, vertical = false })
155    end),
156    vsplit = require("artio").wrap(function(self)
157      self:cancel()
158    end, function(self)
159      local item = self:getcurrent()
160      if not item then
161        return
162      end
163      local buf = fn(item)
164      vim.api.nvim_open_win(buf, true, { win = -1, vertical = true })
165    end),
166    tabnew = require("artio").wrap(function(self)
167      self:cancel()
168    end, function(self)
169      local item = self:getcurrent()
170      if not item then
171        return
172      end
173      local buf = fn(item)
174      vim.api.nvim_cmd({
175        cmd = "split",
176        args = { ("+%dbuf"):format(buf) },
177        ---@diagnostic disable-next-line: missing-fields
178        mods = {
179          tab = 1,
180          silent = true,
181        },
182      }, {
183        output = false,
184      })
185    end),
186  }
187end
188
189function utils.hl_qfitem(item)
190  local name_end = string.find(item.text, ":")
191  if not name_end then
192    return
193  end
194  name_end = name_end - 1
195  local lnum_end = string.find(item.text, ":", name_end + 2)
196  if not lnum_end then
197    return
198  end
199  lnum_end = lnum_end - 1
200  local col_end = string.find(item.text, ":", lnum_end + 2)
201  if not col_end then
202    return
203  end
204  col_end = col_end - 1
205
206  return {
207    { { 0, name_end }, "Title" },
208    { { name_end, name_end + 1 }, "NonText" },
209    { { name_end + 1, lnum_end }, "Number" },
210    { { lnum_end, lnum_end + 1 }, "NonText" },
211    { { lnum_end + 1, col_end }, "Number" },
212    { { col_end, col_end + 1 }, "NonText" },
213  }
214end
215
216return utils