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
2.9 KiB132 linesraw
1---@module 'artio.config'
2
3---@class artio.config
4---@field opts artio.config.opts
5---@field win artio.config.win
6---@field mappings table<string, 'up'|'down'|'accept'|'cancel'|'togglepreview'|string>
7
8---@class artio.config.opts
9---@field preselect boolean
10---@field bottom boolean
11---@field shrink boolean
12---@field promptprefix string
13---@field prompt_title boolean
14---@field pointer string
15---@field marker string
16---@field infolist ('index'|'list')[]
17---@field use_icons boolean
18
19---@class artio.config.win
20---@field height? integer|number
21---@field hidestatusline? boolean
22---@field preview_opts? fun(view: artio.View): vim.api.keyset.win_config
23
24local M = {}
25
26---@type artio.config
27---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
28---@text # Default ~
29M.default = {
30  opts = {
31    preselect = true,
32    bottom = true,
33    shrink = true,
34    promptprefix = "",
35    prompt_title = true,
36    pointer = "",
37    marker = "│",
38    infolist = { "list" }, -- index: [1] list: (4/5)
39    use_icons = _G["MiniIcons"] and true or false,
40  },
41  win = {
42    height = 0.4,
43    hidestatusline = false, -- works best with laststatus=3
44    ---@diagnostic disable-next-line: assign-type-mismatch
45    preview_opts = vim.NIL,
46  },
47  mappings = {
48    ["<down>"] = "down",
49    ["<up>"] = "up",
50    ["<cr>"] = "accept",
51    ["<esc>"] = "cancel",
52    ["<tab>"] = "mark",
53    ["<c-g>"] = "togglelive",
54    ["<c-l>"] = "togglepreview",
55    ["<c-q>"] = "setqflist",
56    ["<c-s>"] = "split",
57    ["<c-v>"] = "vsplit",
58    ["<c-t>"] = "tabnew",
59  },
60}
61
62---@type artio.config
63---@diagnostic disable-next-line: missing-fields
64M.config = {}
65
66---@private
67---@generic T: table|any[]
68---@param tdefault T
69---@param toverride T
70---@return T
71local function tmerge(tdefault, toverride)
72  if toverride == nil then
73    return tdefault
74  end
75
76  if vim.islist(tdefault) then
77    return toverride
78  end
79  if vim.tbl_isempty(tdefault) then
80    return toverride
81  end
82
83  return vim.iter(pairs(tdefault)):fold({}, function(tnew, k, v)
84    if v == vim.NIL and toverride[k] ~= nil then
85      tnew[k] = toverride[k]
86      return tnew
87    end
88
89    if toverride[k] == nil or type(v) ~= type(toverride[k]) then
90      tnew[k] = v
91      return tnew
92    end
93    if type(v) == "table" then
94      tnew[k] = tmerge(v, toverride[k])
95      return tnew
96    end
97
98    tnew[k] = toverride[k]
99    return tnew
100  end)
101end
102
103---@param tdefault artio.config
104---@param toverride artio.config
105---@return artio.config
106function M.merge(tdefault, toverride)
107  local defaults = vim.deepcopy(tdefault, true)
108  local mappings = tdefault.mappings
109  defaults.mappings = vim.NIL
110  local t = tmerge(defaults, toverride)
111  t.mappings = toverride.mappings or mappings
112
113  return t
114end
115
116---@return artio.config
117function M.get()
118  return M.merge(M.default, M.config)
119end
120
121---@param cfg artio.config
122---@return artio.config
123function M.override(cfg)
124  return M.merge(M.default, cfg)
125end
126
127---@param cfg artio.config
128function M.set(cfg)
129  M.config = cfg
130end
131
132return M