snacks.nvim/lua/snacks/picker/init.lua
Folke Lemaitre 559d6c6bf2
feat(snacks): added snacks.picker (#445)
## Description

More info coming tomorrow.

In short:
- very fast. pretty much realtime filtering/sorting in huge repos (like
1.7 million files)
- extensible
- easy to customize the layout (and lots of presets) with
`snacks.layout`
- simple to create custom pickers
- `vim.ui.select`
- lots of builtin pickers
- uses treesitter highlighting wherever it makes sense
- fast lua fuzzy matcher which supports the [fzf
syntax](https://junegunn.github.io/fzf/search-syntax/) and additionally
supports field filters, like `file:lua$ 'function`

There's no snacks picker command, just use lua.

```lua
-- all pickers
Snacks.picker()

-- run files picker
Snacks.picker.files(opts)
Snacks.picker.pick("files", opts)
Snacks.picker.pick({source = "files", ...})
```

<!-- Describe the big picture of your changes to communicate to the
maintainers
  why we should accept this pull request. -->

## Todo
- [x] issue with preview loc not always correct when scrolling fast in
list (probably due to `snacks.scroll`)
- [x] `grep` (`live_grep`) is sometimes too fast in large repos and can
impact ui rendering. Not very noticeable, but something I want to look
at.
- [x] docs
- [x] treesitter highlights are broken. Messed something up somewhere

## Related Issue(s)

<!--
  If this PR fixes any issues, please link to the issue here.
  - Fixes #<issue_number>
-->

## Screenshots

<!-- Add screenshots of the changes if applicable. -->
2025-01-14 22:53:59 +01:00

91 lines
2.4 KiB
Lua

---@class snacks.picker
---@field actions snacks.picker.actions
---@field config snacks.picker.config
---@field format snacks.picker.formatters
---@field preview snacks.picker.previewers
---@field sort snacks.picker.sorters
---@field util snacks.picker.util
---@field current? snacks.Picker
---@field highlight snacks.picker.highlight
---@field resume fun(opts?: snacks.picker.Config):snacks.Picker
---@field sources snacks.picker.sources.Config
---@overload fun(opts: snacks.picker.Config): snacks.Picker
---@overload fun(source: string, opts: snacks.picker.Config): snacks.Picker
local M = setmetatable({}, {
__call = function(M, ...)
return M.pick(...)
end,
---@param M snacks.picker
__index = function(M, k)
if type(k) ~= "string" then
return
end
local mods = {
"actions",
"config",
"format",
"preview",
"util",
"sort",
highlight = "util.highlight",
sources = "config.sources",
}
for m, mod in pairs(mods) do
mod = mod == k and k or m == k and mod or nil
if mod then
---@diagnostic disable-next-line: no-unknown
M[k] = require("snacks.picker." .. mod)
return rawget(M, k)
end
end
return M.config.wrap(k, { check = true })
end,
})
---@type snacks.meta.Meta
M.meta = {
desc = "Picker for selecting items",
needs_setup = true,
merge = { config = "config.defaults", picker = "core.picker", "actions" },
}
-- create actual picker functions for autocomplete
vim.schedule(M.config.setup)
--- Create a new picker
---@param source? string
---@param opts? snacks.picker.Config
---@overload fun(opts: snacks.picker.Config): snacks.Picker
function M.pick(source, opts)
if not opts and type(source) == "table" then
opts, source = source, nil
end
opts = opts or {}
opts.source = source or opts.source
-- Show pickers if no source, items or finder is provided
if not (opts.source or opts.items or opts.finder) then
opts.source = "pickers"
return M.pick(opts)
end
return require("snacks.picker.core.picker").new(opts)
end
--- Implementation for `vim.ui.select`
---@type snacks.picker.ui_select
function M.select(...)
return require("snacks.picker.select").select(...)
end
---@private
function M.setup()
if M.config.get().ui_select then
vim.ui.select = M.select
end
end
---@private
function M.health()
require("snacks.picker.core._health").health()
end
return M