feat: lazy, mason, ts
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
-- <treesitter> or { <treesitter>, <lsp>, <linter>, <formatter>, filetype=<ft> }
|
||||
-- filetype defaults to <treesitter>. If different, specify it.
|
||||
-- any positional argument can be skipped with nil
|
||||
local languages = {
|
||||
-- language_spec = { treesitter?, lsp?, linter?, formatter?, filetype? }
|
||||
local language_specs = {
|
||||
-- Docs / Config
|
||||
'vim',
|
||||
'vimdoc',
|
||||
@@ -15,74 +13,212 @@ local languages = {
|
||||
'gitignore',
|
||||
'dockerfile',
|
||||
'diff',
|
||||
{ 'json', 'jsonls', nil, 'prettier' },
|
||||
{ 'jsonc', 'jsonls', nil, 'prettier' },
|
||||
{ 'json', nil, nil, 'prettier' },
|
||||
{ 'jsonc', nil, nil, 'prettier' },
|
||||
|
||||
-- Shell / Scripting
|
||||
{ 'bash', 'bashls', 'shellcheck', 'shfmt' },
|
||||
{ 'lua', 'luals', 'luacheck', 'stylua' },
|
||||
{ 'sql', 'sqlls', nil, 'sqlfluff' },
|
||||
-- Shell / scripting
|
||||
{ 'bash', 'bash-language-server', 'shellcheck', 'shfmt' },
|
||||
{ 'lua', 'lua-language-server', 'luacheck', 'stylua' },
|
||||
'sql',
|
||||
|
||||
-- Programming
|
||||
{ 'c', 'clangd', nil, 'clang-format' },
|
||||
{ 'cpp', 'clangd', nil, 'clang-format' },
|
||||
{ 'go', 'gopls', 'golangci-lint', 'gofmt' },
|
||||
{ 'rust', 'rust_analyzer', 'clippy', 'rustfmt' },
|
||||
{ 'python', 'pyright', 'ruff', 'black' },
|
||||
'c',
|
||||
'cpp',
|
||||
'go',
|
||||
'rust',
|
||||
'python',
|
||||
-- { 'python', 'pyright', 'ruff', 'ruff' }, -- install ensurepip
|
||||
|
||||
-- Web stack
|
||||
{ 'html', 'html', nil, 'prettier' },
|
||||
{ 'css', 'cssls', 'stylelint', 'prettier' },
|
||||
{ 'javascript', 'tsls', 'eslint', 'prettier' },
|
||||
{ 'typescript', 'tsls', 'eslint', 'prettier' },
|
||||
{ nil, 'tsls', 'eslint', 'prettier', filetype = 'javascriptreact' },
|
||||
{ 'tsx', 'tsls', 'eslint', 'prettier', filetype = 'typescriptreact' },
|
||||
{ 'html', nil, nil, 'prettier' },
|
||||
{ 'css', nil, nil, 'prettier' },
|
||||
{ 'javascript', 'vtsls', 'eslint_d', 'prettier' },
|
||||
{ 'typescript', 'vtsls', 'eslint_d', 'prettier' },
|
||||
{ nil, 'vtsls', 'eslint_d', 'prettier', filetype = 'javascriptreact' },
|
||||
{ 'tsx', 'vtsls', 'eslint_d', 'prettier', filetype = 'typescriptreact' },
|
||||
}
|
||||
|
||||
local function normalize(lang)
|
||||
if type(lang) == 'string' then
|
||||
lang = { lang }
|
||||
local function normalize(spec)
|
||||
if type(spec) == 'string' then
|
||||
spec = { spec }
|
||||
end
|
||||
local t = {
|
||||
treesitter = lang[1],
|
||||
lsp = lang[2],
|
||||
linter = lang[3],
|
||||
formatter = lang[4],
|
||||
filetype = lang.filetype or lang[1],
|
||||
return {
|
||||
treesitter = spec[1],
|
||||
lsp = spec[2],
|
||||
linter = spec[3],
|
||||
formatter = spec[4],
|
||||
filetype = spec.filetype or spec[1],
|
||||
}
|
||||
return t
|
||||
end
|
||||
|
||||
local normalized = vim.tbl_map(normalize, languages)
|
||||
local general = {
|
||||
ts_parsers = {},
|
||||
lsps = {},
|
||||
linters_by_ft = {},
|
||||
formatters_by_ft = {},
|
||||
}
|
||||
for _, opts in ipairs(normalized) do
|
||||
if opts.treesitter then
|
||||
table.insert(general.ts_parsers, opts.treesitter)
|
||||
end
|
||||
local normalized = vim.tbl_map(normalize, language_specs)
|
||||
|
||||
if opts.lsp then
|
||||
table.insert(general.lsps, opts.lsp)
|
||||
end
|
||||
|
||||
if opts.linter then
|
||||
general.linters_by_ft[opts.filetype] = { opts.linter }
|
||||
end
|
||||
|
||||
if opts.formatter then
|
||||
general.formatters_by_ft[opts.filetype] = { opts.formatter }
|
||||
local function uniq(tbl)
|
||||
local seen, out = {}, {}
|
||||
for _, v in ipairs(tbl) do
|
||||
if v and not seen[v] then
|
||||
seen[v] = true
|
||||
table.insert(out, v)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function collect(specs)
|
||||
local ts, lsps, linters, formatters = {}, {}, {}, {}
|
||||
for _, s in ipairs(specs) do
|
||||
if s.treesitter then
|
||||
table.insert(ts, s.treesitter)
|
||||
end
|
||||
if s.lsp then
|
||||
table.insert(lsps, s.lsp)
|
||||
end
|
||||
if s.linter then
|
||||
linters[s.filetype] = { s.linter }
|
||||
end
|
||||
if s.formatter then
|
||||
formatters[s.filetype] = { s.formatter }
|
||||
end
|
||||
end
|
||||
return {
|
||||
ts_parsers = uniq(ts),
|
||||
lsps = uniq(lsps),
|
||||
linters_by_ft = linters,
|
||||
formatters_by_ft = formatters,
|
||||
}
|
||||
end
|
||||
|
||||
local general = collect(normalized)
|
||||
|
||||
---------------------------------------------------------------------
|
||||
-- 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' })
|
||||
---------------------------------------------------------------------
|
||||
-- Plugins
|
||||
---------------------------------------------------------------------
|
||||
return {
|
||||
{ 'windwp/nvim-ts-autotag', config = true },
|
||||
{ 'windwp/nvim-autopairs', event = 'InsertEnter', config = true },
|
||||
{ 'mason-org/mason.nvim', config = true },
|
||||
-- { 'saghen/blink.cmp', version = '1.*' },
|
||||
{
|
||||
'mason-org/mason.nvim',
|
||||
config = function()
|
||||
local mason = require('mason')
|
||||
local registry = require('mason-registry')
|
||||
mason.setup()
|
||||
|
||||
local lsp_configs = {}
|
||||
|
||||
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
|
||||
local lsp_name = (spec and spec.lspconfig) or lsp
|
||||
table.insert(lsp_configs, lsp_name)
|
||||
|
||||
-- Native enable call (Neovim ≥ 0.11)
|
||||
vim.lsp.enable(lsp_name)
|
||||
else
|
||||
vim.notify('Unknown LSP: ' .. lsp, vim.log.levels.WARN)
|
||||
end
|
||||
end
|
||||
|
||||
vim.notify('Enabled LSPs: ' .. table.concat(lsp_configs, ', '), vim.log.levels.INFO)
|
||||
end,
|
||||
},
|
||||
{
|
||||
'nvim-treesitter/nvim-treesitter',
|
||||
build = ':TSUpdate',
|
||||
@@ -90,7 +226,6 @@ return {
|
||||
opts = {
|
||||
highlight = { enable = true },
|
||||
incremental_selection = { enable = true },
|
||||
textobjects = { enable = true },
|
||||
ensure_installed = general.ts_parsers,
|
||||
},
|
||||
},
|
||||
@@ -101,6 +236,7 @@ return {
|
||||
config = function(_, opts)
|
||||
local lint = require('lint')
|
||||
lint.linters_by_ft = opts.linters_by_ft
|
||||
|
||||
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost', 'InsertLeave' }, {
|
||||
group = vim.api.nvim_create_augroup('lint_autocmd', { clear = true }),
|
||||
callback = function()
|
||||
@@ -113,27 +249,9 @@ return {
|
||||
'stevearc/conform.nvim',
|
||||
event = { 'BufWritePre' },
|
||||
opts = {
|
||||
-- Automatically format on save
|
||||
format_on_save = {
|
||||
timeout_ms = 500,
|
||||
lsp_format = 'fallback', -- Use LSP when no formatter is configured
|
||||
},
|
||||
|
||||
-- Formatters per filetype
|
||||
format_on_save = { timeout_ms = 500, lsp_format = 'fallback' },
|
||||
default_format_opts = { stop_after_first = true },
|
||||
formatters_by_ft = general.formatters_by_ft,
|
||||
},
|
||||
|
||||
config = function(_, opts)
|
||||
require('conform').setup(opts)
|
||||
|
||||
-- Create a command to format manually
|
||||
vim.api.nvim_create_user_command('Format', function()
|
||||
require('conform').format({
|
||||
async = true,
|
||||
lsp_format = 'fallback',
|
||||
})
|
||||
end, { desc = 'Format current buffer' })
|
||||
end,
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user