From acff86601efd3923fdb1ef4088574fff6befbe37 Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Sat, 9 Aug 2025 17:28:14 +0300 Subject: [PATCH] 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. --- runtime/doc/pack.txt | 2 ++ runtime/lua/vim/pack.lua | 10 ++++++---- test/functional/plugin/pack_spec.lua | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/runtime/doc/pack.txt b/runtime/doc/pack.txt index ef11e96a29..a3b479c54e 100644 --- a/runtime/doc/pack.txt +++ b/runtime/doc/pack.txt @@ -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 diff --git a/runtime/lua/vim/pack.lua b/runtime/lua/vim/pack.lua index dbd426d37d..da4456d399 100644 --- a/runtime/lua/vim/pack.lua +++ b/runtime/lua/vim/pack.lua @@ -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 diff --git a/test/functional/plugin/pack_spec.lua b/test/functional/plugin/pack_spec.lua index 5123321121..165afa1f49 100644 --- a/test/functional/plugin/pack_spec.lua +++ b/test/functional/plugin/pack_spec.lua @@ -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({