local general = {} --------------------------------------------------------------------- -- Mason Install Command --------------------------------------------------------------------- vim.api.nvim_create_user_command('MasonInstallAll', function() local registry = require('mason-registry') local list = vim.list_extend(vim.list_extend({}, general.lsps), {}) for _, ftmap in pairs({ general.linters_by_ft, general.formatters_by_ft }) do for _, tools in pairs(ftmap) do vim.list_extend(list, tools) end end list = uniq(list) local installed = {} for _, pkg in ipairs(registry.get_installed_packages()) do installed[pkg.name] = true end for _, name in ipairs(list) do if registry.has_package(name) then if not installed[name] then vim.notify('Installing ' .. name, vim.log.levels.INFO) registry.get_package(name):install() else vim.notify('Already installed ' .. name, vim.log.levels.INFO) end else vim.notify('Package not found in registry: ' .. name, vim.log.levels.WARN) end end end, { desc = 'Install all Mason LSPs, linters, and formatters' }) --------------------------------------------------------------------- -- Fetch LSP default configs from nvim-lspconfig --------------------------------------------------------------------- vim.api.nvim_create_user_command('FetchLspConfigs', function() local registry = require('mason-registry') local lspconfig_names = {} for _, lsp in ipairs(general.lsps) do if registry.has_package(lsp) then local pkg = registry.get_package(lsp) local spec = pkg.spec and pkg.spec.neovim if spec and spec.lspconfig then table.insert(lspconfig_names, spec.lspconfig) else table.insert(lspconfig_names, lsp) end else table.insert(lspconfig_names, lsp) end end lspconfig_names = uniq(lspconfig_names) -- base URL same as your original local base_url = 'https://raw.githubusercontent.com/neovim/nvim-lspconfig/master/lsp/' -- write to current working directory local lsp_dir = vim.fs.joinpath(vim.fn.getcwd(), 'lsp') vim.fn.mkdir(lsp_dir, 'p') for _, name in ipairs(lspconfig_names) do local file = vim.fs.joinpath(lsp_dir, name .. '.lua') if vim.fn.filereadable(file) == 0 then local url = base_url .. name .. '.lua' local cmd = string.format('curl -fsSL -o %q %q', file, url) vim.fn.system(cmd) if vim.v.shell_error ~= 0 then vim.notify('Failed to fetch ' .. name .. '.lua', vim.log.levels.ERROR) vim.fn.delete(file) else vim.notify('Fetched ' .. name .. '.lua', vim.log.levels.INFO) end else vim.notify('Skipped existing ' .. name .. '.lua', vim.log.levels.INFO) end end end, { desc = 'Fetch default LSP configs into ./lsp in cwd' }) vim.api.nvim_create_user_command('TreesitterInstallAll', function() local parsers = require('nvim-treesitter.parsers') local configs = require('nvim-treesitter.configs') local langs = configs.get_module('ensure_installed') or {} if type(langs) == 'string' and langs == 'all' then vim.notify('Treesitter ensure_installed = "all" not supported here', vim.log.levels.WARN) return end for _, lang in ipairs(langs) do if not parsers.has_parser(lang) then vim.cmd('TSInstall ' .. lang) else vim.notify('Parser already installed: ' .. lang, vim.log.levels.INFO) end end end, { desc = 'Install all Treesitter parsers defined in ensure_installed' })