Reconcile Neovim setup packages

This commit is contained in:
2026-05-14 10:58:44 +03:00
parent 713af0f937
commit 5acac8cb17
10 changed files with 234 additions and 41 deletions

View File

@@ -29,38 +29,50 @@ vim.api.nvim_create_user_command('MessagesToBuffer', function()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
end, { desc = 'Open :messages output in a scratch buffer' })
vim.api.nvim_create_user_command('CmdToBuffer', function(opts)
local cmd = table.concat(opts.fargs, ' ')
vim.api.nvim_create_user_command(
'CmdToBuffer',
function(opts)
local cmd = table.concat(opts.fargs, ' ')
vim.g.__cmd_to_buffer_cmd = cmd
vim.g.__cmd_to_buffer_out = nil
vim.g.__cmd_to_buffer_cmd = cmd
vim.g.__cmd_to_buffer_out = nil
local exec_line = opts.bang and 'silent execute' or 'execute'
local ok, err = pcall(vim.cmd, string.format(
[[
local exec_line = opts.bang and 'silent execute' or 'execute'
local ok, err = pcall(
vim.cmd,
string.format(
[[
redir => g:__cmd_to_buffer_out
%s g:__cmd_to_buffer_cmd
redir END
]],
exec_line
))
if not ok then
pcall(vim.cmd, 'redir END')
error(err)
end
exec_line
)
)
if not ok then
pcall(vim.cmd, 'redir END')
error(err)
end
local output = vim.g.__cmd_to_buffer_out or ''
vim.g.__cmd_to_buffer_cmd = nil
vim.g.__cmd_to_buffer_out = nil
local lines = vim.split(output, '\n', { plain = true, trimempty = false })
local output = vim.g.__cmd_to_buffer_out or ''
vim.g.__cmd_to_buffer_cmd = nil
vim.g.__cmd_to_buffer_out = nil
local lines = vim.split(output, '\n', { plain = true, trimempty = false })
vim.cmd('new')
local bufnr = vim.api.nvim_get_current_buf()
vim.bo[bufnr].buftype = 'nofile'
vim.bo[bufnr].bufhidden = 'wipe'
vim.bo[bufnr].swapfile = false
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
end, { nargs = '+', bang = true, complete = 'command', desc = 'Run an Ex command and open its output in a scratch buffer' })
vim.cmd('new')
local bufnr = vim.api.nvim_get_current_buf()
vim.bo[bufnr].buftype = 'nofile'
vim.bo[bufnr].bufhidden = 'wipe'
vim.bo[bufnr].swapfile = false
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
end,
{
nargs = '+',
bang = true,
complete = 'command',
desc = 'Run an Ex command and open its output in a scratch buffer',
}
)
-- Automatically create a scratch buffer if Neovim starts with no files
au('VimEnter', {
@@ -166,3 +178,9 @@ au('UIEnter', {
end)
end,
})
vim.api.nvim_create_autocmd('VimResized', {
callback = function()
vim.opt.scroll = 20
end,
})

View File

@@ -21,11 +21,17 @@ function M.get()
-- ts = 'helm',
-- lsp = 'helm-ls',
-- },
{
ft = 'python',
ts = 'python',
lsp = 'ruff',
format = { name = 'ruff_format', install = false },
},
{
ft = 'yaml',
ts = 'yaml',
lsp = 'yaml-language-server',
format = { 'prettierd', 'prettier' },
-- format = { 'prettierd', 'prettier' },
},
{ ts = { 'yaml', 'toml', 'sql', 'diff', 'dockerfile', 'gitcommit', 'gitignore' } },
{ ts = { 'c', 'cpp', 'go', 'rust', 'python' } },

View File

@@ -181,10 +181,20 @@ function M.generate_specs(specs_raw)
specs.add(resolved_lsps, 'language_servers')
install_spec.add(spec.lint, 'code_tools')
install_spec.add(spec.format, 'code_tools')
for _, ft in ipairs(wrap(spec.ft)) do
specs.add(spec.lint, 'linters_by_ft', ft)
specs.add(spec.format, 'formatters_by_ft', ft)
end
for _, raw in ipairs(wrap(spec.format)) do
local f = type(raw) == 'table' and { name = raw.name, install = raw.install }
or { name = raw, install = raw }
if f.install then
install_spec.add(f.install, 'code_tools')
end
for _, ft in ipairs(wrap(spec.ft)) do
specs.add(f.name, 'formatters_by_ft', ft)
end
end
end

View File

@@ -1,4 +1,5 @@
local lock_path = vim.fs.joinpath(vim.fn.stdpath('config'), 'paq-lock.json')
local paq_removed_status = 3
local function clone_paq()
local path = vim.fn.stdpath('data') .. '/site/pack/paqs/opt/paq-nvim'
@@ -19,32 +20,102 @@ local function load_paq()
return paq
end
local function install_packages()
clone_paq()
local function run_paq(event, operation)
local done = false
vim.api.nvim_create_autocmd('User', {
pattern = 'PaqDoneInstall',
pattern = event,
once = true,
callback = function()
done = true
end,
})
local paq = load_paq()
paq.install()
operation()
vim.wait(60000, function()
return done
end, 200)
if not done then
return done
end
local function install_packages()
clone_paq()
local paq = load_paq()
if not run_paq('PaqDoneInstall', paq.install) then
print('Paq installation timeout or failed')
else
print('Paq installation completed')
end
end
local function assert_no_unlisted_package_dirs(paq)
local expected_dirs = {}
for _, filter in ipairs({ 'installed', 'to_install' }) do
for _, pkg in ipairs(paq.query(filter)) do
expected_dirs[pkg.dir] = true
end
end
local stale_dirs = {}
local paq_path = vim.fs.joinpath(vim.fn.stdpath('data'), 'site', 'pack', 'paqs')
for _, packdir in ipairs({ 'start', 'opt' }) do
local path = vim.fs.joinpath(paq_path, packdir)
if vim.uv.fs_stat(path) then
for name, type in vim.fs.dir(path) do
local dir = vim.fs.joinpath(path, name)
if type == 'directory' and not expected_dirs[dir] then
table.insert(stale_dirs, dir)
end
end
end
end
if #stale_dirs > 0 then
error('Paq cleanup left unlisted package directories:\n' .. table.concat(stale_dirs, '\n'))
end
end
local function prune_removed_lock_entries()
if vim.fn.filereadable(lock_path) == 0 then
return
end
local raw = table.concat(vim.fn.readfile(lock_path), '\n')
local lock = vim.json.decode(raw)
local changed = false
for name, pkg in pairs(lock) do
if pkg.status == paq_removed_status then
lock[name] = nil
changed = true
end
end
if changed then
vim.fn.writefile({ vim.json.encode(lock) }, lock_path)
end
end
local function reconcile_packages()
clone_paq()
local paq = load_paq()
if not run_paq('PaqDoneRemove', paq.clean) then
print('Paq cleanup timeout or failed')
return
end
assert_no_unlisted_package_dirs(paq)
prune_removed_lock_entries()
if not run_paq('PaqDoneInstall', paq.install) then
print('Paq installation timeout or failed')
else
print('Paq packages reconciled')
end
end
local function install_languages()
vim.cmd.packadd('mason.nvim')
require('mason').setup()
@@ -58,7 +129,13 @@ end
local function sync_packages()
clone_paq()
local paq = load_paq()
paq:sync()
if not run_paq('PaqDoneSync', function()
paq:sync()
end) then
print('Paq sync timeout or failed')
else
print('Paq sync completed')
end
end
local function fetch_lsp_configs()
@@ -94,8 +171,8 @@ local function create_command(name, callback, desc)
end
create_command('Setup', function()
print('> Installing packages...')
install_packages()
print('> Reconciling packages...')
reconcile_packages()
print('\n> Installing languages: treesitter parsers, language servers, linters, formatters...')
install_languages()
print('\n=== Setup Finished ===\n')