snacks.nvim/lua/snacks/picker/core/main.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

50 lines
1.6 KiB
Lua

---@class snacks.picker.main
local M = {}
---@class snacks.picker.main.Config
---@field float? boolean main window can be a floating window (defaults to false)
---@field file? boolean main window should be a file (defaults to true)
---@field current? boolean main window should be the current window (defaults to false)
---@param opts? snacks.picker.main.Config
function M.get(opts)
opts = vim.tbl_extend("force", {
float = false,
file = true,
current = false,
}, opts or {})
local current = vim.api.nvim_get_current_win()
if opts.current then
return current
end
local prev = vim.fn.winnr("#")
local wins = { current, prev }
local all = vim.api.nvim_list_wins()
-- sort all by lastused of the win buffer
table.sort(all, function(a, b)
local ba = vim.api.nvim_win_get_buf(a)
local bb = vim.api.nvim_win_get_buf(b)
return vim.fn.getbufinfo(ba)[1].lastused > vim.fn.getbufinfo(bb)[1].lastused
end)
vim.list_extend(wins, all)
wins = vim.tbl_filter(function(win)
-- exclude invalid windows
if win == 0 or not vim.api.nvim_win_is_valid(win) then
return false
end
-- exclude non-file buffers
if opts.file and vim.bo[vim.api.nvim_win_get_buf(win)].buftype ~= "" then
return false
end
local win_config = vim.api.nvim_win_get_config(win)
local is_float = win_config.relative ~= ""
-- exclude floating windows and non-focusable windows
if is_float and (not opts.float or not win_config.focusable) then
return false
end
return true
end, wins)
return wins[1] or current
end
return M