dotfiles/nvim/lua/lsp.lua

181 lines
4.3 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

vim.g.coq_settings = {
auto_start = 'shut-up',
}
local coq = require('coq')
local lspstatus = require('lsp-status')
lspstatus.register_progress()
local function find_repo_root(names)
local util = require("lspconfig.util")
local config = names.configfiles
for _, i in ipairs(config) do
local gitroot = util.root_pattern('.git')(i)
if gitroot then
for _, file in ipairs(config) do
if util.path.is_file(util.path.join(gitroot, file)) then
return gitroot
end
end
end
end
end
local lsp_confs = {
sumneko_lua = {
settings = {
Lua = {
-- Settings for working with nvim (copied from https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#sumneko_lua)
runtime = {
version = 'LuaJIT',
path = runtime_path,
},
diagnostics = {
globals = {'vim'},
},
workspace = {
library = vim.api.nvim_get_runtime_file("", true),
},
},
},
},
rust_analyzer = {
cmd_env = {
CARGO_TARGET_DIR = "/tmp/rust-analyzer"
},
settings = {
["rust-analyzer"] = {
checkOnSave = {
command = "clippy",
allTargets = false,
},
cargo = {
features = "all",
}
},
},
},
ccls = {},
jedi_language_server = {
filetypes = { "python", "sage.python" },
},
gopls = {},
dartls = {},
tsserver = {
root_dir = find_repo_root({
configfiles = { "tsconfig.json", "jsconfig.json", "package.json" }
}),
},
eslint = {
root_dir = find_repo_root({
configfiles = { ".eslintrc", ".eslintrc.js", "package.json" }
}),
on_new_config = function (config, new_root_dir)
local default_config = require('lspconfig.server_configurations.eslint').default_config
default_config.on_new_config(config, new_root_dir)
local util = require('lspconfig.util')
local pnp_cjs = util.path.join(new_root_dir, '.pnp.cjs')
local pnp_js = util.path.join(new_root_dir, '.pnp.js')
if util.path.exists(pnp_cjs) or util.path.exists(pnp_js) then
config.cmd = { 'yarn', 'exec', unpack(default_config.cmd) }
end
end
},
svelte = {},
svls = {},
}
local lsp_special_setup = {
}
local lspconfig = require('lspconfig')
for server, conf in pairs(lsp_confs) do
local source_on_attach = conf.on_attach
if source_on_attach == nil then
conf.on_attach = on_attach
else
conf.on_attach = function(client, bufnr)
on_attach(client, bufnr)
source_on_attach(client, bufnr)
end
end
conf.capabilities = vim.tbl_deep_extend(
"keep",
conf.capabilities or {},
lspstatus.capabilities
)
local final_config = coq.lsp_ensure_capabilities(conf)
local special_setup = lsp_special_setup[server]
if special_setup then
special_setup(final_config)
else
lspconfig[server].setup(final_config)
end
end
local null_ls = require('null-ls')
null_ls.setup({
sources = {
null_ls.builtins.diagnostics.chktex,
null_ls.builtins.diagnostics.cppcheck,
null_ls.builtins.diagnostics.shellcheck,
null_ls.builtins.code_actions.gitsigns,
},
})
vim.diagnostic.config({
severity_sort = true,
update_in_insert = true,
float = {
source = true,
},
})
do -- signs
local signs = {
{'DiagnosticSignError', text='×', texthl='DiagnosticSignError'},
{'DiagnosticSignWarn', text='>', texthl='DiagnosticSignWarn'},
{'DiagnosticSignInfo', text='I', texthl='DiagnosticSignInfo'},
{'DiagnosticSignHint', text='H', texthl='DiagnosticSignHint'},
}
for _, sign in pairs(signs) do
local name = sign[1]
local opts = sign
opts[1] = nil
vim.fn.sign_define(name, opts)
end
end
local map = vim.api.nvim_set_keymap
map(
'n',
'K',
[[luaeval('next(vim.lsp.buf_get_clients()) == nil') ? 'K' : '<Cmd>lua vim.lsp.buf.hover()<CR>']],
{noremap = true, expr = true}
)
map(
'n',
'<C-]>',
[[luaeval('next(vim.lsp.buf_get_clients()) == nil') ? '<C-]>' : '<Cmd>lua vim.lsp.buf.definition()<CR>']],
{noremap = true, expr = true}
)
vim.cmd [[
augroup lspconfig
au!
" diagnostic on hover
au CursorHold * lua vim.diagnostic.open_float({focus = false})
augroup END
]]