dotfiles/nix/home/programs/nvim/nvim-lspconfig.lua
2022-07-13 19:53:58 -07:00

165 lines
5.2 KiB
Lua

local nvim_lsp = require("lspconfig")
-- local format_async = function(err, _, result, _, bufnr)
-- if err ~= nil or result == nil then return end
-- if not vim.api.nvim_buf_get_option(bufnr, "modified") then
-- local view = vim.fn.winsaveview()
-- vim.lsp.util.apply_text_edits(result, bufnr)
-- vim.fn.winrestview(view)
-- if bufnr == vim.api.nvim_get_current_buf() then
-- vim.api.nvim_command("noautocmd :update")
-- end
-- end
-- end
-- vim.lsp.handlers["textDocument/formatting"] = format_async
local on_attach = function(client, bufnr)
local buf_map = vim.api.nvim_buf_set_keymap
vim.cmd("command! LspDef lua vim.lsp.buf.definition()")
vim.cmd("command! LspFormatting lua vim.lsp.buf.formatting()")
vim.cmd("command! LspCodeAction lua vim.lsp.buf.code_action()")
vim.cmd("command! LspHover lua vim.lsp.buf.hover()")
vim.cmd("command! LspRename lua vim.lsp.buf.rename()")
vim.cmd("command! LspOrganize lua lsp_organize_imports()")
vim.cmd("command! LspRefs lua vim.lsp.buf.references()")
vim.cmd("command! LspTypeDef lua vim.lsp.buf.type_definition()")
vim.cmd("command! LspImplementation lua vim.lsp.buf.implementation()")
vim.cmd("command! LspDiagPrev lua vim.lsp.diagnostic.goto_prev()")
vim.cmd("command! LspDiagNext lua vim.lsp.diagnostic.goto_next()")
vim.cmd("command! LspDiagLine lua vim.lsp.diagnostic.show_line_diagnostics()")
vim.cmd("command! LspSignatureHelp lua vim.lsp.buf.signature_help()")
buf_map(bufnr, "n", "gd", ":LspDef<CR>", {silent = true})
buf_map(bufnr, "n", "gr", ":LspRename<CR>", {silent = true})
buf_map(bufnr, "n", "gR", ":LspRefs<CR>", {silent = true})
buf_map(bufnr, "n", "gy", ":LspTypeDef<CR>", {silent = true})
buf_map(bufnr, "n", "K", ":LspHover<CR>", {silent = true})
buf_map(bufnr, "n", "gs", ":LspOrganize<CR>", {silent = true})
buf_map(bufnr, "n", "[a", ":LspDiagPrev<CR>", {silent = true})
buf_map(bufnr, "n", "]a", ":LspDiagNext<CR>", {silent = true})
buf_map(bufnr, "n", "ga", ":LspCodeAction<CR>", {silent = true})
buf_map(bufnr, "n", "<Leader>a", ":LspDiagLine<CR>", {silent = true})
buf_map(bufnr, "i", "<C-x><C-x>", "<cmd> LspSignatureHelp<CR>", {silent = true})
if client.resolved_capabilities.document_formatting then
vim.api.nvim_exec([[
augroup LspAutocommands
autocmd! * <buffer>
autocmd BufWritePost <buffer> LspFormatting
augroup END
]], true)
end
end
nvim_lsp.diagnosticls.setup {
on_attach = on_attach;
filetypes = { 'javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'css', 'scss', 'markdown', 'pandoc' },
init_options = {
linters = {
eslint = {
command = 'eslint',
rootPatterns = { '.git' },
debounce = 100,
args = { '--stdin', '--stdin-filename', '%filepath', '--format', 'json' },
sourceName = 'eslint',
parseJson = {
errorsRoot = '[0].messages',
line = 'line',
column = 'column',
endLine = 'endLine',
endColumn = 'endColumn',
message = '[eslint] ${message} [${ruleId}]',
security = 'severity'
},
securities = {
[2] = 'error',
[1] = 'warning'
}
},
markdownlint = {
command = 'markdownlint',
rootPatterns = { '.git' },
isStderr = true,
debounce = 100,
args = { '--stdin' },
offsetLine = 0,
offsetColumn = 0,
sourceName = 'markdownlint',
securities = {
undefined = 'hint'
},
formatLines = 1,
formatPattern = {
'^.*:(\\d+)\\s+(.*)$',
{
line = 1,
column = -1,
message = 2,
}
}
}
},
filetypes = {
javascript = 'eslint',
javascriptreact = 'eslint',
typescript = 'eslint',
typescriptreact = 'eslint',
markdown = 'markdownlint',
pandoc = 'markdownlint'
},
formatters = {
prettier = {
command = 'prettier',
args = { '--stdin-filepath', '%filename' }
}
},
formatFiletypes = {
css = 'prettier',
javascript = 'prettier',
javascriptreact = 'prettier',
json = 'prettier',
scss = 'prettier',
typescript = 'prettier',
typescriptreact = 'prettier'
}
}
}
nvim_lsp.elmls.setup{
on_attach = on_attach;
}
nvim_lsp.tsserver.setup {
on_attach = function(client)
-- Avoid conflict with prettier and eslint
client.resolved_capabilities.document_formatting = false
on_attach(client)
end
}
nvim_lsp.elixirls.setup{
cmd = { "elixir-ls" };
on_attach = on_attach;
}
nvim_lsp.rnix.setup{
cmd = { "@rnix_lsp@/bin/rnix-lsp" };
on_attach = on_attach;
}
nvim_lsp.rust_analyzer.setup{
on_attach = on_attach;
}
nvim_lsp.sumneko_lua.setup{
cmd = { "@lua_ls@/bin/lua-language-server" };
on_attach = on_attach;
settings = {
Lua = {
diagnostics = {
globals = { 'vim' }
}
}
};
}
nvim_lsp.texlab.setup{
on_attach = on_attach;
}