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

64 lines
1.9 KiB
Lua

local M = {}
---@class snacks.picker
---@field help fun(opts?: snacks.picker.help.Config): snacks.Picker
---@param opts snacks.picker.help.Config
---@type snacks.picker.finder
function M.help(opts)
local langs = opts.lang or vim.opt.helplang:get() ---@type string[]
local rtp = vim.o.runtimepath
if package.loaded.lazy then
rtp = rtp .. "," .. table.concat(require("lazy.core.util").get_unloaded_rtp(""), ",")
end
local files = vim.fn.globpath(rtp, "doc/*", true, true) ---@type string[]
---@async
---@param cb async fun(item: snacks.picker.finder.Item)
return function(cb)
if not vim.tbl_contains(langs, "en") then
table.insert(langs, "en")
end
local tag_files = {} ---@type table<string, string[]>
local help_files = {} ---@type table<string, string>
for _, file in ipairs(files) do
local name = vim.fn.fnamemodify(file, ":t")
local lang = "en"
if name == "tags" or name:sub(1, 5) == "tags-" then
lang = name:match("^tags%-(..)$") or lang
if vim.tbl_contains(langs, lang) then
tag_files[lang] = tag_files[lang] or {}
table.insert(tag_files[lang], file)
end
else
help_files[name] = file
end
end
local done = {} ---@type table<string, boolean>
for _, lang in ipairs(langs) do
for _, file in ipairs(tag_files[lang] or {}) do
for line in io.lines(file) do
local fields = vim.split(line, string.char(9), { plain = true })
if not line:match("^!_TAG_") and #fields == 3 and not done[fields[1]] then
done[fields[1]] = true
---@type snacks.picker.finder.Item
local item = {
text = fields[1],
tag = fields[1],
file = help_files[fields[2]],
search = "/\\V" .. fields[3]:sub(2),
}
if item.file then
cb(item)
end
end
end
end
end
end
end
return M