feat(picker.commands): do not autorun commands that require arguments (#879)

## Description

Currently when you use picker.commands to select a command that requires
an argument, it will cause an error. `:BlickCmd` is one example. To
avoid the error we can feed the keys to enter the command mode and
populate the command and leave it to the user what they want to do.

This is also what telescope
[does](415af52339/lua/telescope/builtin/__internal.lua (L399))
in its builtin command picker.

nargs values `*` or `?` mean that the command can be executed both with
arguments and without. I think it is safer to leave it to the user to
decide if they want them to either trigger a run or provide the
arguments. But happy to change it if you think running the commands by
default makes sense.

---------

Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
This commit is contained in:
Anton Kastritskii 2025-02-02 21:16:41 +00:00 committed by GitHub
parent b6a84db919
commit 62d99ed2a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 3 deletions

View file

@ -386,7 +386,11 @@ function M.cmd(picker, item)
picker:close()
if item and item.cmd then
vim.schedule(function()
vim.cmd(item.cmd)
if item.command and (item.command.nargs ~= "0") then
vim.api.nvim_input(":" .. item.cmd .. " ")
else
vim.cmd(item.cmd)
end
end)
end
end

View file

@ -15,8 +15,6 @@ local M = {}
---@class snacks.picker.history.Config: snacks.picker.Config
---@field name string
local uv = vim.uv or vim.loop
function M.commands()
local commands = vim.api.nvim_get_commands({})
for k, v in pairs(vim.api.nvim_buf_get_commands(0, {})) do