minimal extui fuzzy finder for neovim

feat(builtins): allow passing props to builtins

+132 -98
+132 -98
lua/artio/builtins.lua
··· 9 9 local artio = lzrq("artio") 10 10 local config = lzrq("artio.config") 11 11 12 + local function extend(t1, t2) 13 + return vim.tbl_deep_extend("force", t1, t2) 14 + end 15 + 12 16 local function cmd_callback(o) 13 17 local src = o.stderr 14 18 if o.code == 0 then ··· 43 47 44 48 local findprg = "fd -H -p -t f --color=never" 45 49 46 - builtins.files = function() 47 - local lst = make_cmd(findprg)() 50 + builtins.files = function(props) 51 + props = props or {} 52 + props.findprg = props.findprg or findprg 53 + 54 + local lst = make_cmd(props.findprg)() 48 55 49 - return artio.generic(lst, { 50 - prompt = "files", 51 - on_close = function(text, _) 52 - vim.schedule(function() 53 - vim.cmd.edit(text) 54 - end) 55 - end, 56 - get_icon = config.get().opts.use_icons and function(item) 57 - return require("mini.icons").get("file", item.v) 58 - end or nil, 59 - preview_item = function(item) 60 - return vim.fn.bufadd(item) 61 - end, 62 - }) 56 + return artio.generic( 57 + lst, 58 + extend({ 59 + prompt = "files", 60 + on_close = function(text, _) 61 + vim.schedule(function() 62 + vim.cmd.edit(text) 63 + end) 64 + end, 65 + get_icon = config.get().opts.use_icons and function(item) 66 + return require("mini.icons").get("file", item.v) 67 + end or nil, 68 + preview_item = function(item) 69 + return vim.fn.bufadd(item) 70 + end, 71 + }, props) 72 + ) 63 73 end 64 74 65 - builtins.grep = function() 75 + builtins.grep = function(props) 76 + props = props or {} 66 77 local ext = require("vim._extui.shared") 67 78 local grepcmd = make_cmd(vim.o.grepprg) 68 79 69 - return artio.pick({ 80 + return artio.pick(extend({ 70 81 items = {}, 71 82 prompt = "grep", 72 83 get_items = function(input) ··· 107 118 get_icon = config.get().opts.use_icons and function(item) 108 119 return require("mini.icons").get("file", item.v[1]) 109 120 end or nil, 110 - }) 121 + }, props)) 111 122 end 112 123 113 124 local function find_oldfiles() ··· 119 130 :totable() 120 131 end 121 132 122 - builtins.oldfiles = function() 133 + builtins.oldfiles = function(props) 134 + props = props or {} 123 135 local lst = find_oldfiles() 124 136 125 - return artio.generic(lst, { 126 - prompt = "oldfiles", 127 - on_close = function(text, _) 128 - vim.schedule(function() 129 - vim.cmd.edit(text) 130 - end) 131 - end, 132 - get_icon = config.get().opts.use_icons and function(item) 133 - return require("mini.icons").get("file", item.v) 134 - end or nil, 135 - preview_item = function(item) 136 - return vim.fn.bufadd(item) 137 - end, 138 - }) 137 + return artio.generic( 138 + lst, 139 + extend({ 140 + prompt = "oldfiles", 141 + on_close = function(text, _) 142 + vim.schedule(function() 143 + vim.cmd.edit(text) 144 + end) 145 + end, 146 + get_icon = config.get().opts.use_icons and function(item) 147 + return require("mini.icons").get("file", item.v) 148 + end or nil, 149 + preview_item = function(item) 150 + return vim.fn.bufadd(item) 151 + end, 152 + }, props) 153 + ) 139 154 end 140 155 141 - builtins.buffergrep = function() 156 + builtins.buffergrep = function(props) 157 + props = props or {} 142 158 local win = vim.api.nvim_get_current_win() 143 159 local buf = vim.api.nvim_win_get_buf(win) 144 160 local n = vim.api.nvim_buf_line_count(buf) ··· 149 165 150 166 local pad = #tostring(lst[#lst]) 151 167 152 - return artio.generic(lst, { 153 - prompt = "buffergrep", 154 - on_close = function(row, _) 155 - vim.schedule(function() 156 - vim.api.nvim_win_set_cursor(win, { row, 0 }) 157 - end) 158 - end, 159 - format_item = function(row) 160 - return vim.api.nvim_buf_get_lines(buf, row - 1, row, true)[1] 161 - end, 162 - preview_item = function(row) 163 - return buf, 164 - function(w) 165 - vim.api.nvim_set_option_value("cursorline", true, { scope = "local", win = w }) 166 - vim.api.nvim_win_set_cursor(w, { row, 0 }) 167 - end 168 - end, 169 - get_icon = function(row) 170 - local v = tostring(row.v) 171 - return ("%s%s"):format((" "):rep(pad - #v), v) 172 - end, 173 - }) 168 + return artio.generic( 169 + lst, 170 + extend({ 171 + prompt = "buffergrep", 172 + on_close = function(row, _) 173 + vim.schedule(function() 174 + vim.api.nvim_win_set_cursor(win, { row, 0 }) 175 + end) 176 + end, 177 + format_item = function(row) 178 + return vim.api.nvim_buf_get_lines(buf, row - 1, row, true)[1] 179 + end, 180 + preview_item = function(row) 181 + return buf, 182 + function(w) 183 + vim.api.nvim_set_option_value("cursorline", true, { scope = "local", win = w }) 184 + vim.api.nvim_win_set_cursor(w, { row, 0 }) 185 + end 186 + end, 187 + get_icon = function(row) 188 + local v = tostring(row.v) 189 + return ("%s%s"):format((" "):rep(pad - #v), v) 190 + end, 191 + }, props) 192 + ) 174 193 end 175 194 176 195 local function find_helptags() ··· 185 204 end, tags) 186 205 end 187 206 188 - builtins.helptags = function() 207 + builtins.helptags = function(props) 208 + props = props or {} 189 209 local lst = find_helptags() 190 210 191 - return artio.generic(lst, { 192 - prompt = "helptags", 193 - on_close = function(text, _) 194 - vim.schedule(function() 195 - vim.cmd.help(text) 196 - end) 197 - end, 198 - }) 211 + return artio.generic( 212 + lst, 213 + extend({ 214 + prompt = "helptags", 215 + on_close = function(text, _) 216 + vim.schedule(function() 217 + vim.cmd.help(text) 218 + end) 219 + end, 220 + }, props) 221 + ) 199 222 end 200 223 201 224 local function find_buffers() ··· 207 230 :totable() 208 231 end 209 232 210 - builtins.buffers = function() 233 + builtins.buffers = function(props) 234 + props = props or {} 211 235 local lst = find_buffers() 212 236 213 237 return artio.select(lst, { ··· 226 250 preview_item = function(item) 227 251 return item 228 252 end, 229 - }) 253 + }, props) 230 254 end 231 255 232 256 ---@param currentfile string ··· 246 270 --- uses the regular files picker as a base 247 271 --- - boosts items in the bufferlist 248 272 --- - proportionally boosts items that match closely to the current file in proximity within the filesystem 249 - builtins.smart = function() 273 + builtins.smart = function(props) 274 + props = props or {} 250 275 local currentfile = vim.api.nvim_buf_get_name(0) 251 276 currentfile = vim.fs.abspath(currentfile) 252 277 253 - local lst = find_files() 278 + props.findprg = props.findprg or findprg 279 + local lst = make_cmd(props.findprg)() 254 280 255 281 local pwd = vim.fn.getcwd() 256 282 local recentlst = vim ··· 261 287 end) 262 288 :totable() 263 289 264 - return artio.pick({ 290 + return artio.pick(extend({ 265 291 prompt = "smart", 266 292 items = vim.tbl_keys(vim.iter({ lst, recentlst }):fold({}, function(items, l) 267 293 for i = 1, #l do ··· 298 324 preview_item = function(item) 299 325 return vim.fn.bufadd(item) 300 326 end, 301 - }) 327 + }, props)) 302 328 end 303 329 304 - builtins.colorschemes = function() 330 + builtins.colorschemes = function(props) 331 + props = props or {} 305 332 local files = vim.api.nvim_get_runtime_file("colors/*.{vim,lua}", true) 306 333 local lst = vim.tbl_map(function(f) 307 334 return vim.fs.basename(f):gsub("%.[^.]+$", "") 308 335 end, files) 309 336 310 - return artio.generic(lst, { 311 - prompt = "colorschemes", 312 - on_close = function(text, _) 313 - vim.schedule(function() 314 - vim.cmd.colorscheme(text) 315 - end) 316 - end, 317 - }) 337 + return artio.generic( 338 + lst, 339 + extend({ 340 + prompt = "colorschemes", 341 + on_close = function(text, _) 342 + vim.schedule(function() 343 + vim.cmd.colorscheme(text) 344 + end) 345 + end, 346 + }, props) 347 + ) 318 348 end 319 349 320 - builtins.highlights = function() 350 + builtins.highlights = function(props) 351 + props = props or {} 321 352 local hlout = vim.split(vim.api.nvim_exec2([[ highlight ]], { output = true }).output, "\n", { trimempty = true }) 322 353 323 354 local maxw = 0 ··· 335 366 return t 336 367 end) 337 368 338 - return artio.generic(vim.tbl_keys(hls), { 339 - prompt = "highlights", 340 - on_close = function(line, _) 341 - vim.schedule(function() 342 - vim.print(line) 343 - end) 344 - end, 345 - format_item = function(hlname) 346 - return hls[hlname] 347 - end, 348 - hl_item = function(hlname) 349 - return { 350 - { { 0, #hlname.v }, hlname.v }, 351 - } 352 - end, 353 - }) 369 + return artio.generic( 370 + vim.tbl_keys(hls), 371 + extend({ 372 + prompt = "highlights", 373 + on_close = function(line, _) 374 + vim.schedule(function() 375 + vim.print(line) 376 + end) 377 + end, 378 + format_item = function(hlname) 379 + return hls[hlname] 380 + end, 381 + hl_item = function(hlname) 382 + return { 383 + { { 0, #hlname.v }, hlname.v }, 384 + } 385 + end, 386 + }, props) 387 + ) 354 388 end 355 389 356 390 return builtins