Files
LazyVim/lua/lazyvim/util/pick.lua
Arthur eca8692451 fix(pick): remove the unused commands attribute (#3673)
## What is this PR for?
When we register a picker, such as a Telescope picker or a fzf-lua
picker,

we use commands within their own picker. For example, we use
M.picker.commands instead of M.pick.commands.

<!-- Describe the big picture of your changes to communicate to the
maintainers
  why we should accept this pull request. -->

## Does this PR fix an existing issue?
no

<!--
  If this PR fixes any issues, please link to the issue here.
  Fixes #<issue_number>
-->

## Checklist

- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.
2024-06-15 18:54:39 +02:00

104 lines
2.4 KiB
Lua

---@class lazyvim.util.pick
---@overload fun(command:string, opts?:lazyvim.util.pick.Opts): fun()
local M = setmetatable({}, {
__call = function(m, ...)
return m.wrap(...)
end,
})
---@class lazyvim.util.pick.Opts: table<string, any>
---@field root? boolean
---@field cwd? string
---@field buf? number
---@field show_untracked? boolean
---@class LazyPicker
---@field name string
---@field open fun(command:string, opts?:lazyvim.util.pick.Opts)
---@field commands table<string, string>
---@type LazyPicker?
M.picker = nil
---@param picker LazyPicker
function M.register(picker)
-- this only happens when using :LazyExtras
-- so allow to get the full spec
if vim.v.vim_did_enter == 1 then
return true
end
if M.picker and M.picker.name ~= M.want() then
M.picker = nil
end
if M.picker and M.picker.name ~= picker.name then
LazyVim.warn(
"`LazyVim.pick`: picker already set to `" .. M.picker.name .. "`,\nignoring new picker `" .. picker.name .. "`"
)
return false
end
M.picker = picker
return true
end
function M.want()
vim.g.lazyvim_picker = vim.g.lazyvim_picker or "auto"
if vim.g.lazyvim_picker == "auto" then
return LazyVim.has_extra("editor.fzf") and "fzf" or "telescope"
end
return vim.g.lazyvim_picker
end
---@param command? string
---@param opts? lazyvim.util.pick.Opts
function M.open(command, opts)
if not M.picker then
return LazyVim.error("LazyVim.pick: picker not set")
end
command = command or "auto"
opts = opts or {}
opts = vim.deepcopy(opts)
if type(opts.cwd) == "boolean" then
LazyVim.warn("LazyVim.pick: opts.cwd should be a string or nil")
opts.cwd = nil
end
if not opts.cwd and opts.root ~= false then
opts.cwd = LazyVim.root({ buf = opts.buf })
end
local cwd = opts.cwd or vim.uv.cwd()
if command == "auto" then
command = "files"
if
vim.uv.fs_stat(cwd .. "/.git")
and not vim.uv.fs_stat(cwd .. "/.ignore")
and not vim.uv.fs_stat(cwd .. "/.rgignore")
then
command = "git_files"
opts.show_untracked = opts.show_untracked ~= false
end
end
command = M.picker.commands[command] or command
M.picker.open(command, opts)
end
---@param command? string
---@param opts? lazyvim.util.pick.Opts
function M.wrap(command, opts)
opts = opts or {}
return function()
LazyVim.pick.open(command, vim.deepcopy(opts))
end
end
function M.config_files()
return M.wrap("files", { cwd = vim.fn.stdpath("config") })
end
return M