2021-09-16 01:13:17 +00:00
|
|
|
-- luacheck: globals packer_plugins
|
2021-12-08 00:07:57 +00:00
|
|
|
local M = {}
|
2021-09-30 17:57:37 +00:00
|
|
|
local utils = require("utils")
|
|
|
|
|
2021-12-10 20:04:13 +00:00
|
|
|
function M.config_lsp_ui()
|
2021-12-09 17:20:06 +00:00
|
|
|
local border = {
|
2021-12-15 17:37:51 +00:00
|
|
|
{ "┌", "FloatBorder" },
|
|
|
|
{ "─", "FloatBorder" },
|
|
|
|
{ "┐", "FloatBorder" },
|
|
|
|
{ "│", "FloatBorder" },
|
|
|
|
{ "┘", "FloatBorder" },
|
|
|
|
{ "─", "FloatBorder" },
|
|
|
|
{ "└", "FloatBorder" },
|
|
|
|
{ "│", "FloatBorder" },
|
2021-12-09 17:20:06 +00:00
|
|
|
}
|
|
|
|
local orig_util_open_floating_preview = vim.lsp.util.open_floating_preview
|
|
|
|
function vim.lsp.util.open_floating_preview(contents, syntax, opts, ...)
|
|
|
|
opts = opts or {}
|
|
|
|
opts.border = opts.border or border
|
|
|
|
return orig_util_open_floating_preview(contents, syntax, opts, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Diagnostics signs
|
2022-08-31 19:09:15 +00:00
|
|
|
local signs = utils.diagnostic_signs
|
2022-08-31 18:23:03 +00:00
|
|
|
|
2021-12-09 17:20:06 +00:00
|
|
|
for type, icon in pairs(signs) do
|
|
|
|
local hl = "DiagnosticSign" .. type
|
|
|
|
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
|
|
|
|
end
|
|
|
|
|
2021-12-16 22:39:34 +00:00
|
|
|
utils.try_require("trouble", function(trouble)
|
|
|
|
trouble.setup({
|
2021-12-11 01:10:28 +00:00
|
|
|
fold_open = "▼",
|
|
|
|
fold_closed = "▶",
|
2022-01-01 01:31:46 +00:00
|
|
|
icons = false,
|
|
|
|
use_diagnostic_signs = true,
|
2021-12-15 17:37:51 +00:00
|
|
|
})
|
2021-12-16 22:39:34 +00:00
|
|
|
end)
|
2021-12-09 17:20:06 +00:00
|
|
|
end
|
|
|
|
|
2022-01-12 17:21:57 +00:00
|
|
|
local function get_default_attach(override_capabilities)
|
|
|
|
return function(client, bufnr)
|
|
|
|
-- Allow overriding capabilities to avoid duplicate lsps with capabilities
|
|
|
|
if override_capabilities ~= nil then
|
2022-10-14 21:53:03 +00:00
|
|
|
client.resolved_capabilities =
|
|
|
|
vim.tbl_extend("force", client.resolved_capabilities, override_capabilities or {})
|
2022-01-12 17:21:57 +00:00
|
|
|
end
|
2022-01-10 18:47:51 +00:00
|
|
|
|
2022-01-12 17:21:57 +00:00
|
|
|
local function buf_set_keymap(...)
|
|
|
|
vim.api.nvim_buf_set_keymap(bufnr, ...)
|
|
|
|
end
|
2022-09-08 19:23:41 +00:00
|
|
|
|
2022-01-12 17:21:57 +00:00
|
|
|
local function buf_set_option(...)
|
|
|
|
vim.api.nvim_buf_set_option(bufnr, ...)
|
|
|
|
end
|
2022-01-10 18:47:51 +00:00
|
|
|
|
2022-01-12 17:21:57 +00:00
|
|
|
-- Set built in features to use lsp functions
|
|
|
|
buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
|
|
|
|
if client.resolved_capabilities.goto_definition then
|
|
|
|
buf_set_option("tagfunc", "v:lua.vim.lsp.tagfunc")
|
|
|
|
end
|
|
|
|
if client.resolved_capabilities.document_formatting then
|
|
|
|
buf_set_option("formatexpr", "v:lua.vim.lsp.formatexpr()")
|
|
|
|
end
|
2021-08-24 17:38:14 +00:00
|
|
|
|
2022-01-12 17:21:57 +00:00
|
|
|
-- Mappings
|
|
|
|
local opts = { noremap = true, silent = true }
|
2022-01-12 18:06:21 +00:00
|
|
|
local lsp_keymap = utils.keymap_group("n", "<leader>l", opts, bufnr)
|
|
|
|
lsp_keymap("h", "<cmd>lua vim.lsp.buf.hover()<CR>")
|
|
|
|
lsp_keymap("rn", "<cmd>lua vim.lsp.buf.rename()<CR>")
|
2022-03-01 20:28:25 +00:00
|
|
|
lsp_keymap("e", "<cmd>lua vim.diagnostic.open_float()<CR>")
|
2022-01-12 18:06:21 +00:00
|
|
|
lsp_keymap("D", "<cmd>lua vim.lsp.buf.declaration()<CR>")
|
|
|
|
lsp_keymap("d", "<cmd>lua vim.lsp.buf.definition()<CR>")
|
|
|
|
lsp_keymap("t", "<cmd>lua vim.lsp.buf.type_definition()<CR>")
|
|
|
|
lsp_keymap("i", "<cmd>lua vim.lsp.buf.implementation()<CR>")
|
|
|
|
lsp_keymap("s", "<cmd>lua vim.lsp.buf.signature_help()<CR>")
|
|
|
|
lsp_keymap("wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
|
|
|
|
lsp_keymap("wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
|
|
|
|
lsp_keymap("wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
|
|
|
|
lsp_keymap("r", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
|
|
|
|
lsp_keymap("p", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
|
|
|
|
lsp_keymap("n", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
|
2022-01-12 17:21:57 +00:00
|
|
|
|
2022-03-01 20:27:15 +00:00
|
|
|
-- Set insert keymap for signature help
|
|
|
|
buf_set_keymap("i", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
|
|
|
|
|
2022-01-12 17:59:29 +00:00
|
|
|
-- Older keymaps
|
2022-01-12 17:21:57 +00:00
|
|
|
buf_set_keymap("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
|
|
|
|
buf_set_keymap("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
|
|
|
|
buf_set_keymap("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>", opts)
|
|
|
|
buf_set_keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
|
|
|
|
buf_set_keymap("n", "<leader>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
|
|
|
|
buf_set_keymap("n", "<leader>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
|
|
|
|
buf_set_keymap("n", "<leader>wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
|
|
|
|
buf_set_keymap("n", "<leader>D", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
|
|
|
|
buf_set_keymap("n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
|
|
|
|
buf_set_keymap("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
|
2022-03-01 20:28:25 +00:00
|
|
|
buf_set_keymap("n", "<leader>e", "<cmd>lua vim.diagnostic.open_float()<CR>", opts)
|
2022-01-12 17:21:57 +00:00
|
|
|
buf_set_keymap("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
|
|
|
|
buf_set_keymap("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
|
|
|
|
buf_set_keymap("n", "<leader>q", "<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>", opts)
|
|
|
|
|
|
|
|
-- Open diagnostic on hold
|
|
|
|
if vim["diagnostic"] ~= nil then
|
2022-03-01 20:28:25 +00:00
|
|
|
vim.cmd([[autocmd CursorHold * lua vim.diagnostic.open_float(nil, {focus=false, scope="cursor"})]])
|
2022-01-12 17:21:57 +00:00
|
|
|
end
|
2021-12-10 20:04:13 +00:00
|
|
|
|
2022-06-07 16:52:22 +00:00
|
|
|
-- Use IncRename if available
|
|
|
|
if utils.try_require("inc_rename") ~= nil then
|
|
|
|
lsp_keymap("rn", "<cmd>IncRename()<CR>")
|
|
|
|
end
|
|
|
|
|
2022-01-12 17:21:57 +00:00
|
|
|
-- Set some keybinds conditional on server capabilities
|
|
|
|
if client.resolved_capabilities.document_formatting then
|
|
|
|
buf_set_keymap("n", "<leader>lf", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
|
|
|
|
vim.cmd([[
|
2021-08-24 17:38:14 +00:00
|
|
|
augroup lsp_format
|
|
|
|
autocmd!
|
2022-03-01 20:29:30 +00:00
|
|
|
autocmd BufWritePre *.rs,*.go,*.sh,*.lua lua vim.lsp.buf.formatting_sync(nil, 1000)
|
2021-09-09 23:09:38 +00:00
|
|
|
" autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync(nil, 1000)
|
2021-08-24 17:38:14 +00:00
|
|
|
augroup END
|
|
|
|
]])
|
2022-01-12 17:21:57 +00:00
|
|
|
end
|
|
|
|
if client.resolved_capabilities.document_range_formatting then
|
|
|
|
buf_set_keymap("n", "<leader>lfr", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
|
|
|
|
end
|
2021-08-24 17:38:14 +00:00
|
|
|
|
2022-01-12 17:21:57 +00:00
|
|
|
-- Set autocommands conditional on server_capabilities
|
|
|
|
if client.resolved_capabilities.document_highlight then
|
|
|
|
vim.cmd([[
|
2022-03-01 20:25:03 +00:00
|
|
|
:highlight link LspReferenceRead MatchParen
|
|
|
|
:highlight link LspReferenceText MatchParen
|
|
|
|
:highlight link LspReferenceWrite MatchParen
|
|
|
|
augroup lsp_document_highlight
|
|
|
|
autocmd!
|
|
|
|
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
|
|
|
|
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
|
|
|
|
augroup END
|
|
|
|
]])
|
2022-01-12 17:21:57 +00:00
|
|
|
end
|
2021-08-24 17:38:14 +00:00
|
|
|
|
2022-01-12 17:21:57 +00:00
|
|
|
-- Some override some fuzzy finder bindings to use lsp sources
|
2022-01-12 17:59:29 +00:00
|
|
|
if utils.try_require("telescope") ~= nil then
|
|
|
|
-- Replace some Telescope bindings with LSP versions
|
2022-01-24 23:39:02 +00:00
|
|
|
if client.resolved_capabilities.goto_definition then
|
|
|
|
buf_set_keymap("n", "<leader>t", "<cmd>Telescope lsp_document_symbols<CR>", opts)
|
|
|
|
buf_set_keymap("n", "<leader>ft", "<cmd>Telescope lsp_dynamic_workspace_symbols<CR>", opts)
|
|
|
|
buf_set_keymap("n", "<leader>ft", "<cmd>Telescope lsp_dynamic_workspace_symbols<CR>", opts)
|
|
|
|
end
|
2022-01-12 17:59:29 +00:00
|
|
|
|
|
|
|
-- Replace some LSP bindings with Telescope ones
|
2022-01-24 23:39:02 +00:00
|
|
|
if client.resolved_capabilities.goto_definition then
|
|
|
|
lsp_keymap("d", "<cmd>Telescope lsp_definitions<CR>")
|
|
|
|
lsp_keymap("t", "<cmd>Telescope lsp_type_definition()<CR>")
|
|
|
|
end
|
2022-01-12 17:59:29 +00:00
|
|
|
lsp_keymap("i", "<cmd>Telescope lsp_implementations<CR>")
|
|
|
|
lsp_keymap("r", "<cmd>Telescope lsp_references<CR>")
|
|
|
|
lsp_keymap("A", "<cmd>Telescope lsp_code_actions<CR>")
|
|
|
|
|
|
|
|
buf_set_keymap("n", "<leader>ca", "<cmd>Telescope lsp_code_actions<CR>", opts)
|
|
|
|
buf_set_keymap("v", "<leader>lA", "<cmd>Telescope lsp_range_code_actions<CR>", opts)
|
2022-01-12 17:21:57 +00:00
|
|
|
end
|
2021-12-08 00:07:57 +00:00
|
|
|
end
|
2021-08-24 17:38:14 +00:00
|
|
|
end
|
|
|
|
|
2021-12-16 22:40:04 +00:00
|
|
|
local function merged_capabilities()
|
2021-09-30 17:58:22 +00:00
|
|
|
-- Maybe update capabilities
|
|
|
|
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
2021-12-16 22:40:04 +00:00
|
|
|
utils.try_require("cmp-nvim-lsp", function(cmp_nvim_lsp)
|
|
|
|
capabilities = cmp_nvim_lsp.update_capabilities(capabilities)
|
|
|
|
end)
|
|
|
|
return capabilities
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.config_lsp()
|
2021-12-17 04:51:46 +00:00
|
|
|
utils.try_require("lspconfig", function(lsp_config)
|
|
|
|
local capabilities = merged_capabilities()
|
2022-09-08 19:23:41 +00:00
|
|
|
local default_attach = get_default_attach()
|
|
|
|
local default_setup = { capabilities = capabilities, on_attach = default_attach }
|
2021-12-17 04:51:46 +00:00
|
|
|
|
|
|
|
-- Configure each server
|
2022-09-08 19:23:41 +00:00
|
|
|
lsp_config.bashls.setup(default_setup)
|
|
|
|
lsp_config.gopls.setup(default_setup)
|
|
|
|
lsp_config.pyright.setup(default_setup)
|
2021-12-17 04:51:46 +00:00
|
|
|
lsp_config.rls.setup({
|
|
|
|
capabilities = capabilities,
|
2022-09-08 19:23:41 +00:00
|
|
|
on_attach = default_attach,
|
2021-12-17 04:51:46 +00:00
|
|
|
settings = {
|
|
|
|
rust = {
|
|
|
|
build_on_save = false,
|
|
|
|
all_features = true,
|
|
|
|
unstable_features = true,
|
|
|
|
},
|
2021-08-24 17:38:14 +00:00
|
|
|
},
|
2021-12-17 04:51:46 +00:00
|
|
|
})
|
2022-10-14 21:53:03 +00:00
|
|
|
|
|
|
|
-- Configure neovim dev for when sumneko_lua is installed
|
|
|
|
utils.try_require("neodev", function(neodev)
|
|
|
|
neodev.setup({})
|
|
|
|
end)
|
2022-09-08 19:23:41 +00:00
|
|
|
|
|
|
|
-- Auto setup mason installed servers
|
|
|
|
utils.try_require("mason-lspconfig", function(mason_lspconfig)
|
|
|
|
-- Get list of servers that are installed but not set up
|
|
|
|
local already_setup = lsp_config.available_servers()
|
|
|
|
local needs_setup = vim.tbl_filter(function(server)
|
|
|
|
return not vim.tbl_contains(already_setup, server)
|
|
|
|
end, mason_lspconfig.get_installed_servers())
|
|
|
|
|
|
|
|
-- Setup each server with default config
|
|
|
|
vim.tbl_map(function(server)
|
|
|
|
lsp_config[server].setup(default_setup)
|
|
|
|
end, needs_setup)
|
|
|
|
end)
|
2022-01-12 22:35:07 +00:00
|
|
|
|
|
|
|
-- Config null-ls after lsps so we can disable for languages that have language servers
|
2022-09-08 19:23:41 +00:00
|
|
|
require("plugins.null-ls").configure(default_setup)
|
2021-12-17 04:51:46 +00:00
|
|
|
end)
|
2021-08-24 17:38:14 +00:00
|
|
|
end
|
|
|
|
|
2022-01-01 01:32:33 +00:00
|
|
|
function M.config_lsp_intaller()
|
2022-09-08 19:23:41 +00:00
|
|
|
utils.try_require("mason", function(mason)
|
|
|
|
mason.setup()
|
|
|
|
end)
|
|
|
|
utils.try_require("mason-lspconfig", function(mason_lspconfig)
|
|
|
|
mason_lspconfig.setup()
|
2022-01-01 01:32:33 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-12-08 00:07:57 +00:00
|
|
|
return M
|