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 works like `:packadd!`. If function, called with plugin
data and is fully responsible for loading plugin. Default data and is fully responsible for loading plugin. Default
`false` during startup and `true` afterwards. `false` during startup and `true` afterwards.
• {confirm}? (`boolean`) Whether to ask user to confirm
initial install. Default `true`.
del({names}) *vim.pack.del()* del({names}) *vim.pack.del()*
Remove plugins from disk Remove plugins from disk

View File

@@ -562,9 +562,9 @@ local function checkout(p, timestamp, skip_same_sha)
end end
--- @param plug_list vim.pack.Plug[] --- @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 -- 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 for _, p in ipairs(plug_list) do
p.info.err = 'Installation was not confirmed' p.info.err = 'Installation was not confirmed'
end end
@@ -681,6 +681,8 @@ end
--- If function, called with plugin data and is fully responsible for loading plugin. --- If function, called with plugin data and is fully responsible for loading plugin.
--- Default `false` during startup and `true` afterwards. --- Default `false` during startup and `true` afterwards.
--- @field load? boolean|fun(plug_data: {spec: vim.pack.Spec, path: string}) --- @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 --- Add plugin to current session
--- ---
@@ -705,7 +707,7 @@ end
--- @param opts? vim.pack.keyset.add --- @param opts? vim.pack.keyset.add
function M.add(specs, opts) function M.add(specs, opts)
vim.validate('specs', specs, vim.islist, false, 'list') 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') vim.validate('opts', opts, 'table')
--- @type vim.pack.Plug[] --- @type vim.pack.Plug[]
@@ -720,7 +722,7 @@ function M.add(specs, opts)
if #plugs_to_install > 0 then if #plugs_to_install > 0 then
git_ensure_exec() git_ensure_exec()
install_list(plugs_to_install) install_list(plugs_to_install, opts.confirm)
end end
-- Register and load those actually on disk while collecting errors -- 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')) eq({ confirm_msg, 'Proceed? &Yes\n&No', 1, 'Question' }, exec_lua('return _G.confirm_args'))
end) 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() it('installs at proper version', function()
local out = exec_lua(function() local out = exec_lua(function()
vim.pack.add({ vim.pack.add({