171 lines
3.8 KiB
Lua
171 lines
3.8 KiB
Lua
--[[
|
|
local lm = require('language_manager')
|
|
lm.install() or .install({'ts_parsers','language_servers','linters','formatters'})
|
|
- treesitter.install specs.ts_parsers
|
|
- mason.install mason-registry.get_package(name):install()
|
|
lm.generate_specs() or {use_cache=true, only_installed=true}
|
|
lm.delete_cache()
|
|
lm.enable()
|
|
--]]
|
|
|
|
|
|
vim.pack.add({ 'https://github.com/mason-org/mason.nvim' })
|
|
|
|
local M = {}
|
|
M.lsp = {}
|
|
M.ts = {}
|
|
M.lint = {}
|
|
M.format = {}
|
|
|
|
M.group = vim.api.nvim_create_augroup('language-manager', { clear = true })
|
|
|
|
local function wrap(item)
|
|
if type(item) == 'string' then
|
|
return { item }
|
|
elseif type(item) == 'table' then
|
|
return item
|
|
else
|
|
return {}
|
|
end
|
|
end
|
|
|
|
local function create_set()
|
|
local seen = {}
|
|
return function(ref, item) -- unique()
|
|
if not seen[ref] then
|
|
seen[ref] = {}
|
|
end
|
|
if item and not seen[ref][item] then
|
|
seen[ref][item] = true
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
end
|
|
|
|
local function create_spec()
|
|
local S = {}
|
|
local unique = create_set()
|
|
local specs = {}
|
|
|
|
function S.set(item, group)
|
|
specs[group] = item
|
|
end
|
|
|
|
function S.get()
|
|
return specs
|
|
end
|
|
|
|
function S.add(list, ...)
|
|
local groups = { ... }
|
|
local ref = table.concat(groups, '.')
|
|
local pointer = specs
|
|
local last_i = #groups
|
|
|
|
for i = 1, last_i do
|
|
local group = groups[i]
|
|
|
|
if i == last_i then
|
|
for _, item in ipairs(wrap(list)) do
|
|
if unique(ref, item) then
|
|
if not pointer[group] then
|
|
pointer[group] = {}
|
|
end
|
|
table.insert(pointer[group], item)
|
|
end
|
|
end
|
|
else
|
|
if not pointer[group] then
|
|
pointer[group] = {}
|
|
end
|
|
pointer = pointer[group]
|
|
end
|
|
end
|
|
end
|
|
|
|
return S
|
|
end
|
|
|
|
function M.generate_specs(specs_raw)
|
|
local specs = create_spec()
|
|
|
|
for _, spec in ipairs(specs_raw) do
|
|
-- <filetype> -> { ft = <filetype> }
|
|
if type(spec) == 'string' then
|
|
spec = { ft = spec }
|
|
end
|
|
|
|
-- Filetype = TS Parser
|
|
if not spec.ts then
|
|
spec.ts = spec.ft
|
|
end
|
|
|
|
specs.add(spec.ts, 'ts_parsers')
|
|
specs.add(spec.lsp, 'language_servers')
|
|
|
|
for _, filetype in ipairs(wrap(spec.ft)) do
|
|
specs.add(spec.lint, 'linters_by_ft', filetype)
|
|
specs.add(spec.format, 'formatters_by_ft', filetype)
|
|
end
|
|
end
|
|
|
|
M.general = specs.get()
|
|
return M.general
|
|
end
|
|
|
|
function M.lsp.setup()
|
|
require('mason').setup()
|
|
local registry = require('mason-registry')
|
|
|
|
for _, lsp in ipairs(M.general.language_servers) 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
|
|
|
|
vim.lsp.enable(lsp_name)
|
|
else
|
|
vim.notify('Unknown LSP: ' .. lsp, vim.log.levels.WARN)
|
|
end
|
|
end
|
|
end
|
|
|
|
function M.ts.setup()
|
|
require('nvim-treesitter.configs').setup({
|
|
highlight = { enable = true },
|
|
incremental_selection = { enable = true },
|
|
})
|
|
end
|
|
|
|
function M.lint.setup()
|
|
vim.api.nvim_create_autocmd({ 'BufReadPre', 'BufNewFile' }, {
|
|
group = M.group,
|
|
callback = function()
|
|
local lint = require('lint')
|
|
lint.linters_by_ft = M.general.linters_by_ft
|
|
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost', 'InsertLeave' }, {
|
|
group = vim.api.nvim_create_augroup('language-manager.lint', { clear = true }),
|
|
callback = function()
|
|
lint.try_lint()
|
|
end,
|
|
})
|
|
end,
|
|
})
|
|
end
|
|
|
|
function M.format.setup()
|
|
vim.api.nvim_create_autocmd('BufWritePre', {
|
|
group = M.group,
|
|
once = true,
|
|
callback = function()
|
|
require('conform').setup({
|
|
format_on_save = { timeout_ms = 500, lsp_format = 'fallback' },
|
|
default_format_opts = { stop_after_first = true },
|
|
formatters_by_ft = M.general.formatters_by_ft,
|
|
})
|
|
end,
|
|
})
|
|
end
|
|
|
|
return M
|