feat(pack): allow skip install confirmation in add()

Problem: No way to skip install confirmation in `add()`. Having install
  confirmation by default is a more secure design. However, users are
  usually aware of the fact that plugin will be installed and there is
  currently no way to skip confirmation.

  Plus it can introduce inconvenience on the clean config initialization
  if it is modularized with many `vim.pack.add()` calls (leads to
  confirming installation many times in a row).

Solution: Add `opts.confirm` option that can skip install confirmation.
This commit is contained in:
Evgeni Chasnovski
2025-08-09 17:28:14 +03:00
parent dd828690c7
commit acff86601e
3 changed files with 24 additions and 4 deletions

View File

@@ -341,6 +341,8 @@ add({specs}, {opts}) *vim.pack.add()*
works like `:packadd!`. If function, called with plugin
data and is fully responsible for loading plugin. Default
`false` during startup and `true` afterwards.
• {confirm}? (`boolean`) Whether to ask user to confirm
initial install. Default `true`.
del({names}) *vim.pack.del()*
Remove plugins from disk

View File

@@ -562,9 +562,9 @@ local function checkout(p, timestamp, skip_same_sha)
end
--- @param plug_list vim.pack.Plug[]
local function install_list(plug_list)
local function install_list(plug_list, confirm)
-- Get user confirmation to install plugins
if not confirm_install(plug_list) then
if confirm and not confirm_install(plug_list) then
for _, p in ipairs(plug_list) do
p.info.err = 'Installation was not confirmed'
end
@@ -681,6 +681,8 @@ end
--- If function, called with plugin data and is fully responsible for loading plugin.
--- Default `false` during startup and `true` afterwards.
--- @field load? boolean|fun(plug_data: {spec: vim.pack.Spec, path: string})
---
--- @field confirm? boolean Whether to ask user to confirm initial install. Default `true`.
--- Add plugin to current session
---
@@ -705,7 +707,7 @@ end
--- @param opts? vim.pack.keyset.add
function M.add(specs, opts)
vim.validate('specs', specs, vim.islist, false, 'list')
opts = vim.tbl_extend('force', { load = vim.v.vim_did_enter == 1 }, opts or {})
opts = vim.tbl_extend('force', { load = vim.v.vim_did_enter == 1, confirm = true }, opts or {})
vim.validate('opts', opts, 'table')
--- @type vim.pack.Plug[]
@@ -720,7 +722,7 @@ function M.add(specs, opts)
if #plugs_to_install > 0 then
git_ensure_exec()
install_list(plugs_to_install)
install_list(plugs_to_install, opts.confirm)
end
-- Register and load those actually on disk while collecting errors

View File

@@ -332,6 +332,22 @@ describe('vim.pack', function()
eq({ confirm_msg, 'Proceed? &Yes\n&No', 1, 'Question' }, exec_lua('return _G.confirm_args'))
end)
it('respects `opts.confirm`', function()
exec_lua(function()
_G.confirm_used = false
---@diagnostic disable-next-line: duplicate-set-field
vim.fn.confirm = function()
_G.confirm_used = true
return 1
end
vim.pack.add({ repos_src.basic }, { confirm = false })
end)
eq(false, exec_lua('return _G.confirm_used'))
eq('basic main', exec_lua('return require("basic")'))
end)
it('installs at proper version', function()
local out = exec_lua(function()
vim.pack.add({