mirror of
https://github.com/folke/snacks.nvim
synced 2025-07-07 21:25:11 +00:00

## 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. -->
50 lines
1.5 KiB
Lua
50 lines
1.5 KiB
Lua
local M = {}
|
|
|
|
---@alias snacks.picker.ui_select fun(items: any[], opts?: {prompt?: string, format_item?: (fun(item: any): string), kind?: string}, on_choice: fun(item?: any, idx?: number))
|
|
|
|
---@generic T
|
|
---@param items T[] Arbitrary items
|
|
---@param opts? {prompt?: string, format_item?: (fun(item: T): string), kind?: string}
|
|
---@param on_choice fun(item?: T, idx?: number)
|
|
function M.select(items, opts, on_choice)
|
|
assert(type(on_choice) == "function", "on_choice must be a function")
|
|
opts = opts or {}
|
|
|
|
---@type snacks.picker.finder.Item[]
|
|
local finder_items = {}
|
|
for idx, item in ipairs(items) do
|
|
local text = (opts.format_item or tostring)(item)
|
|
table.insert(finder_items, {
|
|
formatted = text,
|
|
text = idx .. " " .. text,
|
|
item = item,
|
|
idx = idx,
|
|
})
|
|
end
|
|
|
|
local title = opts.prompt or "Select"
|
|
title = title:gsub("^%s*", ""):gsub("[%s:]*$", "")
|
|
local layout = Snacks.picker.config.layout("select")
|
|
layout.preview = false
|
|
layout.layout.height = math.floor(math.min(vim.o.lines * 0.8 - 10, #items + 2) + 0.5) + 10
|
|
layout.layout.title = " " .. title .. " "
|
|
layout.layout.title_pos = "center"
|
|
|
|
---@type snacks.picker.finder.Item[]
|
|
return Snacks.picker.pick({
|
|
source = "select",
|
|
items = finder_items,
|
|
format = Snacks.picker.format.ui_select(opts.kind, #items),
|
|
actions = {
|
|
confirm = function(picker, item)
|
|
picker:close()
|
|
vim.schedule(function()
|
|
on_choice(item and item.item, item and item.idx)
|
|
end)
|
|
end,
|
|
},
|
|
layout = layout,
|
|
})
|
|
end
|
|
|
|
return M
|