dotfiles/nvim/lua/plugins/lspconfig.lua

155 lines
5.4 KiB
Lua

local lsp_installer = require("nvim-lsp-installer")
local nvim_runtime_path = vim.split(package.path, ';')
table.insert(nvim_runtime_path, "lua/?.lua")
table.insert(nvim_runtime_path, "lua/?/init.lua")
local language_servers = {
"ansiblels", "bashls", "clangd", "dockerls", "efm", "eslint", "html",
"jsonls", "pyright", "rust_analyzer", "sumneko_lua", "svelte", "taplo",
"terraformls", "tflint", "tsserver", "volar"
}
for _, server_name in pairs(language_servers) do
local server_found, server = lsp_installer.get_server(server_name)
if server_found and not server:is_installed() then
print("Installing " .. server_name)
server:install()
end
end
local extra_server_opts = {
["efm"] = function(opts)
opts.filetypes = {
"lua", "html", "javascript", "markdown", "typescript",
"typescriptreact"
}
opts.init_options = {documentFormatting = true}
opts.settings = {
rootMarkers = {".git/"},
languages = {
lua = {{formatCommand = "lua-format -i", formatStdin = true}},
html = {
{
formatCommand = "yarn run --silent prettier --stdin-filepath ${INPUT} --parser html",
formatStdin = true
}
},
javascript = {
{
formatCommand = "prettier --stdin-filepath ${INPUT}",
formatStdin = true
}
},
typescript = {
{
formatCommand = "yarn run --silent prettier --stdin-filepath ${INPUT} --parser typescript",
formatStdin = true
}
},
typescriptreact = {
{
formatCommand = "yarn run --silent prettier --stdin-filepath ${INPUT} --parser typescript",
formatStdin = true
}
},
markdown = {
{
formatCommand = "yarn run --silent prettier --stdin-filepath ${INPUT} --parser markdown",
formatStdin = true
}
}
}
-- prettier-parser
-- flow|babel|babel-flow|babel-ts|typescript|espree|meriyah|css|
-- less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|glimmer|html|angular|lwc
}
end,
["rust_analyzer"] = function(opts)
opts.settings = {
["rust-analyzer"] = {checkOnSave = {command = "clippy"}}
}
end,
["sumneko_lua"] = function(opts)
opts.settings = {
Lua = {
runtime = {version = 'LuaJIT', path = nvim_runtime_path},
diagnostics = {globals = {'vim'}},
workspace = {library = vim.api.nvim_get_runtime_file("", true)},
telemetry = {enable = false}
}
}
end
}
local function custom_on_attach(client, buffer_nr)
-- onmifunc
vim.api.nvim_buf_set_option(buffer_nr, "omnifunc", "v:lua.vim.lsp.omnifunc")
-- Helper function
local opts = {noremap = true, silent = true}
local function bufnnoremap(key, action)
vim.api.nvim_buf_set_keymap(buffer_nr, 'n', key, action, opts)
end
-- Inspect function
bufnnoremap("K", "<Cmd>lua vim.lsp.buf.hover()<CR>")
bufnnoremap("<C-k>", "<Cmd>lua vim.lsp.buf.signature_help()<CR>")
-- Navigation
bufnnoremap("gd", "<Cmd>lua vim.lsp.buf.definition()<CR>")
bufnnoremap("gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>")
bufnnoremap("gi", "<Cmd>lua vim.lsp.buf.implementation()<CR>")
bufnnoremap("gr", "<Cmd>lua vim.lsp.buf.references()<CR>")
bufnnoremap("ga", "<Cmd>Telescope lsp_code_actions theme=cursor<CR>")
-- Rename all references of symbol
bufnnoremap("<leader>r", "<Cmd>lua vim.lsp.buf.rename()<CR>")
-- Format buffer
bufnnoremap("<leader>F", "<Cmd>lua vim.lsp.buf.formatting_sync()<CR>")
-- Navigate diagnostics
bufnnoremap("<C-n>", "<Cmd>lua vim.diagnostic.goto_next()<CR>")
bufnnoremap("<C-p>", "<Cmd>lua vim.diagnostic.goto_prev()<CR>")
-- Open diagnostics
bufnnoremap("<leader>d", "<Cmd>Telescope diagnostics<CR>")
-- disable conflicting formatters
if client.name == "tsserver" or client.name == "html" then
client.resolved_capabilities.document_formatting = false
end
if client.resolved_capabilities.document_formatting then
vim.cmd("autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()")
end
-- vim-illuminate
require("illuminate").on_attach(client)
end
lsp_installer.on_server_ready(function(server)
local cmp = require("cmp_nvim_lsp")
local opts = {
on_attach = custom_on_attach,
capabilities = cmp.update_capabilities(vim.lsp.protocol
.make_client_capabilities())
}
if extra_server_opts[server.name] then
extra_server_opts[server.name](opts)
end
if server.name == "rust_analyzer" then
require("rust-tools").setup({
server = vim.tbl_deep_extend("force", server:get_default_options(),
opts)
})
server:attach_buffers()
else
server:setup(opts)
end
end)