Files
nvim-config/lua/plugins/language-manager.lua

530 lines
13 KiB
Lua

local utils = require('utils')
local M = {
ts = {},
lsp = {},
lint = {},
format = {},
}
M.group = vim.api.nvim_create_augroup('language-manager', { clear = true })
local cache_path = vim.fn.stdpath('cache') .. '/language-manager.json'
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)
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 read_file(path)
local f = io.open(path, 'r')
if not f then
return nil
end
local content = f:read('*a')
f:close()
return content
end
local function write_file(path, content)
local f = io.open(path, 'w')
if not f then
return false
end
f:write(content)
f:close()
return true
end
local function to_json(tbl)
local ok, result = pcall(vim.json.encode, tbl)
return ok and result or nil
end
local function from_json(str)
local ok, result = pcall(vim.json.decode, str)
return ok and result or nil
end
local function hash_spec(tbl)
local encoded = to_json(tbl) or ''
if vim.fn.exists('*sha256') == 1 then
return vim.fn.sha256(encoded)
else
local tmp = vim.fn.tempname()
write_file(tmp, encoded)
local handle = io.popen('openssl dgst -sha256 ' .. tmp)
local result = handle and handle:read('*a') or ''
if handle then
handle:close()
end
return result:match('([a-f0-9]+)') or ''
end
end
local function save_cache(data)
local encoded = to_json(data)
if encoded then
write_file(cache_path, encoded)
end
end
local function load_cache()
local raw = read_file(cache_path)
if not raw then
return nil
end
return from_json(raw)
end
-- ======== Spec Builder ========
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
pointer[group] = pointer[group] or {}
table.insert(pointer[group], item)
end
end
else
pointer[group] = pointer[group] or {}
pointer = pointer[group]
end
end
end
return S
end
-- ======== Spec Generation ========
local function get_mason_registry()
vim.cmd.packadd('mason.nvim')
require('mason').setup()
local registry = require('mason-registry')
registry.refresh()
return registry
end
function M.generate_specs(specs_raw)
local install_spec = create_spec()
local specs = create_spec()
local registry = get_mason_registry()
local lsp_map = {}
for _, spec in ipairs(specs_raw) do
if type(spec) == 'string' then
spec = { ft = spec }
end
spec.ts = spec.ts or spec.ft
specs.add(spec.ts, 'ts_parsers')
install_spec.add(spec.lsp, 'code_tools')
local resolved_lsps = {}
for _, language_server in ipairs(wrap(spec.lsp)) do
if registry.has_package(language_server) then
local pkg = registry.get_package(language_server)
if pkg.spec and pkg.spec.neovim and pkg.spec.neovim.lspconfig then
local lspconfig_name = pkg.spec and pkg.spec.neovim and pkg.spec.neovim.lspconfig
lsp_map[lspconfig_name] = language_server
table.insert(resolved_lsps, lspconfig_name)
else
print('Package found but not lspconfig name: ' .. language_server)
end
else
print('Package not found: ' .. language_server)
end
end
specs.add(resolved_lsps, 'language_servers')
install_spec.add(spec.lint, 'code_tools')
for _, ft in ipairs(wrap(spec.ft)) do
specs.add(spec.lint, 'linters_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
local result = specs.get()
result.lsp_map = lsp_map
return result, install_spec.get()
end
-- ======== Cache ========
function M.load_specs()
local specs_raw = require('modules.language-specs').get()
local hash = hash_spec(specs_raw)
local cache = load_cache()
if cache and cache.hash == hash then
M.general = cache.spec
else
M.general, M.mason = M.generate_specs(specs_raw)
save_cache({ hash = hash, spec = M.general })
end
return M.general, M.mason
end
function M.invalidate_cache()
local ok = os.remove(cache_path)
if ok then
vim.notify('Language manager cache invalidated', vim.log.levels.INFO)
else
vim.notify('No cache to invalidate or failed to delete', vim.log.levels.WARN)
end
M.general = nil
M.mason = nil
end
function M.ts.install()
vim.cmd.packadd('nvim-treesitter')
local ts_install = require('nvim-treesitter.install').ensure_installed_sync
ts_install(M.general.ts_parsers)
end
function M.mason_install()
local packages = M.mason.code_tools
local registry = get_mason_registry()
local pending = #packages
local result = utils.await(function(resolve)
for _, name in ipairs(packages) do
local pkg = registry.get_package(name)
if pkg:is_installed() then
print('Mason package already installed: ' .. name)
pending = pending - 1
if pending == 0 then
resolve(true)
end
else
print('Mason package installing: ' .. name)
pkg:install({}, function(success, error)
if success then
print('Mason package installed: ' .. name)
else
print('Mason package failed: ' .. name)
print(' > Error: ' .. vim.inspect(error))
end
pending = pending - 1
if pending == 0 then
resolve(true)
end
end)
end
end
end, 5 * 60 * 1000, 200)
if not result.ok or pending ~= 0 then
print('\n!! >> Exited timeout, possible clean up needed!')
print(' > status: ' .. result.ok)
print(' > pending: ' .. pending)
end
end
-- ======== Public API ========
function M.install()
print('\n> Starting ts parsers install')
M.ts.install()
print('\n> Starting mason install: lsp, lint, format')
M.mason_install()
end
function M.ts.setup()
require('nvim-treesitter.configs').setup({
highlight = { enable = true },
incremental_selection = { enable = true },
})
end
function M.lsp.setup()
vim.lsp.config('*', {
capabilities = {
general = {
positionEncodings = { 'utf-8' },
},
},
})
for _, lsp_name in ipairs((M.general and M.general.language_servers) or {}) do
vim.lsp.enable(lsp_name)
end
vim.api.nvim_create_user_command('LspInfo', function()
vim.cmd('checkhealth vim.lsp')
end, {})
end
function M.lint.setup()
vim.api.nvim_create_autocmd({ 'BufReadPre', 'BufNewFile' }, {
group = M.group,
once = true,
callback = function()
local lint = require('lint')
lint.linters_by_ft = M.general.linters_by_ft
function M.debounce(ms, fn)
local timer = vim.uv.new_timer()
return function(...)
local argv = { ... }
timer:start(ms, 0, function()
timer:stop()
vim.schedule_wrap(fn)(unpack(argv))
end)
end
end
function M.lint()
if vim.bo.modifiable then
lint.try_lint()
end
end
vim.api.nvim_create_autocmd({ 'BufReadPost', 'InsertLeave', 'TextChanged' }, {
group = vim.api.nvim_create_augroup('language-manager.lint', { clear = true }),
callback = M.debounce(100, M.lint),
})
vim.api.nvim_create_autocmd('BufEnter', {
group = M.group,
callback = function(args)
local bufnr = args.buf
local ft = vim.bo[bufnr].filetype
local linters = lint.linters_by_ft[ft]
if linters then
vim.api.nvim_buf_create_user_command(bufnr, 'LintInfo', function()
print('Linters for ' .. ft .. ': ' .. table.concat(linters, ', '))
end, {})
end
end,
})
end,
})
end
local biome_save_action_kinds = {
'source.organizeImports.biome',
'source.biome',
}
local function matches_code_action_kind(action, action_kind)
local kind = action.kind or ''
return kind == action_kind or vim.startswith(kind, action_kind .. '.')
end
local function get_client_diagnostics(bufnr, client)
local diagnostics = {}
for _, pull in ipairs({ false, true }) do
local namespace = vim.lsp.diagnostic.get_namespace(client.id, pull)
for _, diagnostic in ipairs(vim.diagnostic.get(bufnr, { namespace = namespace })) do
local lsp_diagnostic = diagnostic.user_data and diagnostic.user_data.lsp
if lsp_diagnostic then
table.insert(diagnostics, lsp_diagnostic)
end
end
end
return diagnostics
end
local function get_document_range(bufnr, client)
local last_line = math.max(vim.api.nvim_buf_line_count(bufnr) - 1, 0)
local line = vim.api.nvim_buf_get_lines(bufnr, last_line, last_line + 1, true)[1] or ''
local last_character = vim.str_utfindex(line, client.offset_encoding or 'utf-16', #line)
return {
start = { line = 0, character = 0 },
['end'] = { line = last_line, character = last_character },
}
end
local function apply_code_action(bufnr, client, action)
local methods = vim.lsp.protocol.Methods
if
not (action.edit and action.command) and client:supports_method(methods.codeAction_resolve)
then
local resolved, resolve_error =
client:request_sync(methods.codeAction_resolve, action, 1000, bufnr)
if resolved and resolved.err then
vim.notify(resolved.err.message, vim.log.levels.ERROR)
return false
end
if resolved and resolved.result then
action = resolved.result
elseif resolve_error then
vim.notify(resolve_error, vim.log.levels.ERROR)
return false
end
end
local applied = false
if action.edit then
vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding)
applied = true
end
if action.command then
local command = type(action.command) == 'table' and action.command or action
client:exec_cmd(command, { bufnr = bufnr })
applied = true
end
return applied
end
local function apply_biome_save_actions(bufnr)
local methods = vim.lsp.protocol.Methods
local clients = vim.lsp.get_clients({
bufnr = bufnr,
name = 'biome',
method = methods.textDocument_codeAction,
})
for _, client in ipairs(clients) do
for _, action_kind in ipairs(biome_save_action_kinds) do
local remaining_passes = 20
local applied_action_kinds = {}
local noop_action_kinds = {}
while remaining_passes > 0 do
remaining_passes = remaining_passes - 1
local result, request_error = client:request_sync(methods.textDocument_codeAction, {
textDocument = vim.lsp.util.make_text_document_params(bufnr),
range = get_document_range(bufnr, client),
context = {
diagnostics = get_client_diagnostics(bufnr, client),
only = { action_kind },
triggerKind = vim.lsp.protocol.CodeActionTriggerKind.Automatic,
},
}, 1000, bufnr)
local applied = false
if result and result.err then
vim.notify(result.err.message, vim.log.levels.ERROR)
elseif request_error then
vim.notify(request_error, vim.log.levels.ERROR)
elseif result and result.result then
for _, action in ipairs(result.result) do
local kind = action.kind or ''
if
matches_code_action_kind(action, action_kind)
and not applied_action_kinds[kind]
and not noop_action_kinds[kind]
then
applied = apply_code_action(bufnr, client, action)
if applied then
applied_action_kinds[kind] = true
break
else
noop_action_kinds[kind] = true
end
end
end
end
if not applied then
break
end
end
if remaining_passes == 0 then
vim.notify('Biome source actions did not converge on save', vim.log.levels.ERROR)
end
end
end
end
function M.format.setup()
require('conform').setup({
formatters_by_ft = (M.general and M.general.formatters_by_ft) or {},
default_format_opts = { stop_after_first = true, lsp_format = 'fallback' },
})
vim.api.nvim_create_autocmd('BufWritePre', {
group = vim.api.nvim_create_augroup('language-manager.format', { clear = true }),
callback = function(args)
if not vim.bo[args.buf].modifiable then
return
end
apply_biome_save_actions(args.buf)
require('conform').format({ bufnr = args.buf, timeout_ms = 500, lsp_format = 'fallback' })
end,
})
end
function M.setup()
M.load_specs()
M.ts.setup()
M.lsp.setup()
M.lint.setup()
M.format.setup()
end
return M