snacks.nvim/lua/snacks/picker/source/buffers.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

45 lines
1.4 KiB
Lua

local M = {}
---@class snacks.picker
---@field buffers fun(opts?: snacks.picker.buffers.Config): snacks.Picker
---@param opts snacks.picker.buffers.Config
---@type snacks.picker.finder
function M.buffers(opts, filter)
local items = {} ---@type snacks.picker.finder.Item[]
local current_buf = vim.api.nvim_get_current_buf()
local alternate_buf = vim.fn.bufnr("#")
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
local keep = (opts.hidden or vim.bo[buf].buflisted)
and (opts.unloaded or vim.api.nvim_buf_is_loaded(buf))
and (opts.current or buf ~= current_buf)
and (opts.nofile or vim.bo[buf].buftype ~= "nofile")
if keep then
local name = vim.api.nvim_buf_get_name(buf)
name = name == "" and "[No Name]" or name
local info = vim.fn.getbufinfo(buf)[1]
local flags = {
buf == current_buf and "%" or (buf == alternate_buf and "#" or ""),
info.hidden == 1 and "h" or "a",
vim.bo[buf].readonly and "=" or "",
info.changed == 1 and "+" or "",
}
table.insert(items, {
flags = table.concat(flags),
buf = buf,
text = name,
file = name,
info = info,
pos = { info.lnum, 0 },
})
end
end
if opts.sort_lastused then
table.sort(items, function(a, b)
return a.info.lastused > b.info.lastused
end)
end
return filter:filter(items)
end
return M