config/.config/nvim/lua/chris/lsp/handlers.lua

127 lines
3.0 KiB
Lua

local M = {}
M.capabilities = vim.lsp.protocol.make_client_capabilities()
local status_cmp_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
if not status_cmp_ok then
print("cmp_nvim_lsp error!")
return
end
M.capabilities.textDocument.completion.completionItem.snippetSupport = true
M.capabilities = cmp_nvim_lsp.default_capabilities(M.capabilities)
M.setup = function()
local icons = require("chris.icons")
local signs = {
{ name = "DiagnosticSignError", text = icons.diagnostics.Error },
{ name = "DiagnosticSignWarn", text = icons.diagnostics.Warning },
{ name = "DiagnosticSignHint", text = icons.diagnostics.Hint },
{ name = "DiagnosticSignInfo", text = icons.diagnostics.Information },
}
for _, sign in ipairs(signs) do
vim.fn.sign_define(sign.name, {
texthl = sign.name,
text = sign.text,
numhl = "",
})
end
local config = {
-- disable virtual text
virtual_lines = false,
virtual_text = false,
-- show signs
signs = {
active = signs,
},
update_in_insert = true,
underline = true,
severity_sort = true,
float = {
focusable = true,
style = "minimal",
source = "if_many", -- Or "always"
},
}
vim.diagnostic.config(config)
end
local function attach_navic(client, bufnr)
vim.g.navic_silence = false
local status_ok, navic = pcall(require, "nvim-navic")
if not status_ok then
print("nvim-navic error!")
return
end
if client.server_capabilities.documentSymbolProvider then
navic.attach(client, bufnr)
end
end
local function disable_deno_formatting(client)
if client.name == "denols" then
require("null-ls").disable({ "prettier" })
end
end
local function resolve_tsserver_deno(client)
local active_clients = vim.lsp.get_active_clients()
if client.name == "denols" then
for _, client_ in pairs(active_clients) do
-- stop tsserver if denols is already active
if client_.name == "tsserver" then
client_.stop()
end
end
elseif client.name == "tsserver" then
for _, client_ in pairs(active_clients) do
-- prevent tsserver from starting if denols is already active
if client_.name == "denols" then
client.stop()
end
end
end
end
M.on_attach = function(client, bufnr)
attach_navic(client, bufnr)
disable_deno_formatting(client)
resolve_tsserver_deno(client)
end
function M.enable_format_on_save()
vim.cmd([[
augroup format_on_save
autocmd!
autocmd BufWritePre * lua vim.lsp.buf.format({ async = false })
augroup end
]])
vim.notify("Enabled format on save")
end
function M.disable_format_on_save()
M.remove_augroup("format_on_save")
vim.notify("Disabled format on save")
end
function M.toggle_format_on_save()
if vim.fn.exists("#format_on_save#BufWritePre") == 0 then
M.enable_format_on_save()
else
M.disable_format_on_save()
end
end
function M.remove_augroup(name)
if vim.fn.exists("#" .. name) == 1 then
vim.cmd("au! " .. name)
end
end
vim.cmd([[ command! LspToggleAutoFormat execute 'lua require("chris.lsp.handlers").toggle_format_on_save()' ]])
return M