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

102 lines
2.9 KiB
Lua

local M = {}
local uv = vim.uv or vim.loop
---@class snacks.picker
---@field git_files fun(opts?: snacks.picker.git.files.Config): snacks.Picker
---@field git_log fun(opts?: snacks.picker.git.log.Config): snacks.Picker
---@field git_log_file fun(opts?: snacks.picker.git.log.Config): snacks.Picker
---@field git_log_line fun(opts?: snacks.picker.git.log.Config): snacks.Picker
---@field git_status fun(opts?: snacks.picker.Config): snacks.Picker
---@param opts snacks.picker.git.files.Config
---@type snacks.picker.finder
function M.files(opts)
local args = { "-c", "core.quotepath=false", "ls-files", "--exclude-standard", "--cached" }
if opts.untracked then
table.insert(args, "--others")
elseif opts.submodules then
table.insert(args, "--recurse-submodules")
end
local cwd = vim.fs.normalize(opts and opts.cwd or uv.cwd() or ".") or nil
return require("snacks.picker.source.proc").proc(vim.tbl_deep_extend("force", {
cmd = "git",
args = args,
---@param item snacks.picker.finder.Item
transform = function(item)
item.cwd = cwd
item.file = item.text
end,
}, opts or {}))
end
---@param opts snacks.picker.git.log.Config
---@type snacks.picker.finder
function M.log(opts)
local args = {
"log",
"--pretty=format:%h %s (%ch)",
"--abbrev-commit",
"--decorate",
"--date=short",
"--color=never",
"--no-show-signature",
"--no-patch",
}
if opts.follow and not opts.current_line then
args[#args + 1] = "--follow"
end
if opts.current_line then
local cursor = vim.api.nvim_win_get_cursor(0)
local line = cursor[1]
args[#args + 1] = "-L"
args[#args + 1] = line .. ",+1:" .. vim.api.nvim_buf_get_name(0)
elseif opts.current_file then
args[#args + 1] = "--"
args[#args + 1] = vim.api.nvim_buf_get_name(0)
end
local cwd = vim.fs.normalize(opts and opts.cwd or uv.cwd() or ".") or nil
return require("snacks.picker.source.proc").proc(vim.tbl_deep_extend("force", {
cmd = "git",
args = args,
---@param item snacks.picker.finder.Item
transform = function(item)
local commit, msg, date = item.text:match("^(%S+) (.*) %((.*)%)$")
if not commit then
error(item.text)
end
item.cwd = cwd
item.commit = commit
item.msg = msg
item.date = date
item.file = item.text
end,
}, opts or {}))
end
---@param opts snacks.picker.Config
---@type snacks.picker.finder
function M.status(opts)
local args = {
"status",
"--porcelain=v1",
}
local cwd = vim.fs.normalize(opts and opts.cwd or uv.cwd() or ".") or nil
return require("snacks.picker.source.proc").proc(vim.tbl_deep_extend("force", {
cmd = "git",
args = args,
---@param item snacks.picker.finder.Item
transform = function(item)
local status, file = item.text:sub(1, 2), item.text:sub(4)
item.cwd = cwd
item.status = status
item.file = file
end,
}, opts or {}))
end
return M