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
|
2024-11-27 20:14:01 +00:00
|
|
|
for type, icon in pairs(require("icons").diagnostic_signs) do
|
2021-12-09 17:20:06 +00:00
|
|
|
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 = "▶",
|
2024-11-27 20:14:01 +00:00
|
|
|
icons = require("icons").nerd_font,
|
2022-01-01 01:31:46 +00:00
|
|
|
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
|
|
|
|
|
2024-10-28 23:18:12 +00:00
|
|
|
function M.get_default_attach(override_capabilities)
|
2022-01-12 17:21:57 +00:00
|
|
|
return function(client, bufnr)
|
|
|
|
-- Allow overriding capabilities to avoid duplicate lsps with capabilities
|
2022-11-02 23:57:18 +00:00
|
|
|
|
|
|
|
-- Using custom method to extract for <0.8 support
|
2024-07-18 00:27:53 +00:00
|
|
|
local server_capabilities = client.server_capabilities
|
2022-01-12 17:21:57 +00:00
|
|
|
if override_capabilities ~= nil then
|
2022-11-02 23:57:18 +00:00
|
|
|
server_capabilities = vim.tbl_extend("force", server_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
|
|
|
-- Mappings
|
2024-05-29 20:16:28 +00:00
|
|
|
local lsp_keymap = utils.curry_keymap("n", "<leader>l", { buffer = bufnr, group_desc = "LSP" })
|
|
|
|
lsp_keymap("h", vim.lsp.buf.hover, { desc = "Display hover" })
|
|
|
|
lsp_keymap("rn", vim.lsp.buf.rename, { desc = "Refactor rename" })
|
|
|
|
lsp_keymap("e", vim.diagnostic.open_float, { desc = "Open float dialog" })
|
|
|
|
lsp_keymap("D", vim.lsp.buf.declaration, { desc = "Go to declaration" })
|
|
|
|
lsp_keymap("d", vim.lsp.buf.definition, { desc = "Go to definition" })
|
|
|
|
lsp_keymap("t", vim.lsp.buf.type_definition, { desc = "Go to type definition" })
|
|
|
|
lsp_keymap("i", vim.lsp.buf.implementation, { desc = "Show implementations" })
|
|
|
|
lsp_keymap("s", vim.lsp.buf.signature_help, { desc = "Show signature help" })
|
|
|
|
lsp_keymap("wa", vim.lsp.buf.add_workspace_folder, { desc = "Workspace: Add folder" })
|
|
|
|
lsp_keymap("wr", vim.lsp.buf.remove_workspace_folder, { desc = "Workspace: Remove folder" })
|
|
|
|
lsp_keymap("wl", function()
|
|
|
|
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
|
|
|
|
end, { desc = "Workspace: List folders" })
|
|
|
|
lsp_keymap("r", vim.lsp.buf.references, { desc = "References" })
|
|
|
|
lsp_keymap("p", vim.diagnostic.goto_prev, { desc = "Previous diagnostic" })
|
|
|
|
lsp_keymap("n", vim.diagnostic.goto_next, { desc = "Next diagnostic" })
|
|
|
|
if server_capabilities.codeActionProvider then
|
|
|
|
lsp_keymap("A", vim.lsp.buf.code_action, { desc = "Select code actions" })
|
|
|
|
lsp_keymap("A", vim.lsp.buf.code_action, { mode = "v", desc = "Select code actions" })
|
|
|
|
end
|
2022-01-12 17:21:57 +00:00
|
|
|
|
2022-03-01 20:27:15 +00:00
|
|
|
-- Set insert keymap for signature help
|
2024-05-29 20:16:28 +00:00
|
|
|
utils.keymap_set("i", "<C-k>", vim.lsp.buf.signature_help, { buffer = bufnr, desc = "Show signature help" })
|
2022-03-01 20:27:15 +00:00
|
|
|
|
2023-06-26 22:01:52 +00:00
|
|
|
-- Some top level aliases or remaps
|
2024-05-29 20:16:28 +00:00
|
|
|
utils.keymap_set("n", "K", vim.lsp.buf.hover, { buffer = bufnr, desc = "Display hover" })
|
|
|
|
utils.keymap_set("n", "gD", vim.lsp.buf.declaration, { buffer = bufnr, desc = "Go to declaration" })
|
|
|
|
utils.keymap_set("n", "gd", vim.lsp.buf.definition, { buffer = bufnr, desc = "Go to definition" })
|
|
|
|
utils.keymap_set("n", "<leader>rn", vim.lsp.buf.rename, { buffer = bufnr, desc = "Refactor rename" })
|
|
|
|
utils.keymap_set("n", "[d", vim.diagnostic.goto_prev, { buffer = bufnr, desc = "Previous diagnostic" })
|
|
|
|
utils.keymap_set("n", "]d", vim.diagnostic.goto_next, { buffer = bufnr, desc = "Next diagnostic" })
|
2022-01-12 17:21:57 +00:00
|
|
|
|
|
|
|
-- Open diagnostic on hold
|
2024-05-29 20:16:28 +00:00
|
|
|
vim.api.nvim_create_autocmd({ "CursorHold" }, {
|
|
|
|
pattern = "<buffer>",
|
|
|
|
callback = function()
|
|
|
|
vim.diagnostic.open_float(nil, { focus = false, scope = "cursor" })
|
|
|
|
end,
|
|
|
|
group = vim.api.nvim_create_augroup("diagnostic_float", { clear = true }),
|
|
|
|
desc = "Open float dialog on hold",
|
|
|
|
})
|
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
|
2022-11-02 23:57:18 +00:00
|
|
|
vim.keymap.set("n", "<leader>rn", function()
|
|
|
|
return ":IncRename " .. vim.fn.expand("<cword>")
|
|
|
|
end, { expr = true, buffer = true, desc = "Rename current symbol" })
|
2022-06-07 16:52:22 +00:00
|
|
|
end
|
|
|
|
|
2022-01-12 17:21:57 +00:00
|
|
|
-- Set some keybinds conditional on server capabilities
|
2024-07-18 00:27:53 +00:00
|
|
|
lsp_keymap("f", function()
|
|
|
|
vim.lsp.buf.format({ async = true })
|
|
|
|
end, { desc = "Format code" })
|
|
|
|
lsp_keymap("f", function()
|
|
|
|
vim.lsp.buf.format({ async = true })
|
|
|
|
end, { mode = "v", desc = "Format selected code" })
|
|
|
|
if server_capabilities.documentFormattingProvider then
|
|
|
|
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
|
|
|
|
pattern = { "*.rs", "*.go", "*.sh", "*.lua" },
|
|
|
|
callback = function()
|
|
|
|
vim.lsp.buf.format({ async = false, timeout_ms = 1000 })
|
|
|
|
end,
|
|
|
|
group = vim.api.nvim_create_augroup("lsp_format", { clear = true }),
|
|
|
|
desc = "Auto format code on save",
|
|
|
|
})
|
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
|
|
|
-- Set autocommands conditional on server_capabilities
|
2022-11-02 23:57:18 +00:00
|
|
|
if server_capabilities.documentHighlightProvider then
|
2024-05-29 20:16:28 +00:00
|
|
|
vim.api.nvim_set_hl(0, "LspReferenceRead", { link = "MatchParen" })
|
|
|
|
vim.api.nvim_set_hl(0, "LspReferenceText", { link = "MatchParen" })
|
|
|
|
vim.api.nvim_set_hl(0, "LspReferenceWrite", { link = "MatchParen" })
|
|
|
|
local hl_group = vim.api.nvim_create_augroup("lsp_document_highlight", { clear = true })
|
|
|
|
vim.api.nvim_create_autocmd(
|
|
|
|
{ "CursorHold" },
|
|
|
|
{ pattern = "<buffer>", callback = vim.lsp.buf.document_highlight, group = hl_group }
|
|
|
|
)
|
|
|
|
vim.api.nvim_create_autocmd(
|
|
|
|
{ "CursorMoved" },
|
|
|
|
{ pattern = "<buffer>", callback = vim.lsp.buf.clear_references, group = hl_group }
|
|
|
|
)
|
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
|
2024-05-29 20:16:28 +00:00
|
|
|
utils.try_require("telescope.builtin", function(telescope_builtin)
|
2022-01-12 17:59:29 +00:00
|
|
|
-- Replace some Telescope bindings with LSP versions
|
2024-05-29 20:16:28 +00:00
|
|
|
local telescope_keymap = utils.curry_keymap("n", "<leader>f", { buffer = bufnr, group_desc = "Finder" })
|
2022-11-02 23:57:18 +00:00
|
|
|
if server_capabilities.documentSymbolProvider then
|
2024-05-29 20:16:28 +00:00
|
|
|
telescope_keymap("t", telescope_builtin.lsp_document_symbols, { desc = "Find buffer tags" })
|
2022-11-02 23:57:18 +00:00
|
|
|
end
|
|
|
|
if server_capabilities.workspaceSymbolProvider then
|
2024-05-29 20:16:28 +00:00
|
|
|
telescope_keymap("T", telescope_builtin.lsp_dynamic_workspace_symbols, { desc = "Find tags" })
|
2022-01-24 23:39:02 +00:00
|
|
|
end
|
2022-01-12 17:59:29 +00:00
|
|
|
|
|
|
|
-- Replace some LSP bindings with Telescope ones
|
2022-11-02 23:57:18 +00:00
|
|
|
if server_capabilities.definitionProvider then
|
2024-05-29 20:16:28 +00:00
|
|
|
lsp_keymap("d", telescope_builtin.lsp_definitions, { desc = "Find definition" })
|
2022-11-02 23:57:18 +00:00
|
|
|
end
|
|
|
|
if server_capabilities.typeDefinitionProvider then
|
2024-05-29 20:16:28 +00:00
|
|
|
lsp_keymap("t", telescope_builtin.lsp_type_definitions, { desc = "Find type definition" })
|
2022-01-24 23:39:02 +00:00
|
|
|
end
|
2024-05-29 20:16:28 +00:00
|
|
|
lsp_keymap("i", telescope_builtin.lsp_implementations, { desc = "Find implementations" })
|
|
|
|
lsp_keymap("r", telescope_builtin.lsp_references, { desc = "Find references" })
|
|
|
|
end)
|
2023-06-02 00:11:50 +00:00
|
|
|
|
|
|
|
-- Attach navic for statusline location
|
|
|
|
if server_capabilities.documentSymbolProvider then
|
|
|
|
utils.try_require("nvim-navic", function(navic)
|
|
|
|
navic.attach(client, bufnr)
|
|
|
|
end)
|
|
|
|
end
|
2021-12-08 00:07:57 +00:00
|
|
|
end
|
2021-08-24 17:38:14 +00:00
|
|
|
end
|
|
|
|
|
2024-10-28 23:18:12 +00:00
|
|
|
function M.merged_capabilities()
|
2021-09-30 17:58:22 +00:00
|
|
|
-- Maybe update capabilities
|
2022-11-02 23:57:18 +00:00
|
|
|
local capabilities = nil
|
2021-12-16 22:40:04 +00:00
|
|
|
utils.try_require("cmp-nvim-lsp", function(cmp_nvim_lsp)
|
2022-11-02 23:57:18 +00:00
|
|
|
capabilities = cmp_nvim_lsp.default_capabilities()
|
2021-12-16 22:40:04 +00:00
|
|
|
end)
|
2022-11-02 23:57:18 +00:00
|
|
|
|
2021-12-16 22:40:04 +00:00
|
|
|
return capabilities
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.config_lsp()
|
2021-12-17 04:51:46 +00:00
|
|
|
utils.try_require("lspconfig", function(lsp_config)
|
2024-10-28 23:18:12 +00:00
|
|
|
local capabilities = M.merged_capabilities()
|
|
|
|
local default_attach = M.get_default_attach()
|
2022-09-08 19:23:41 +00:00
|
|
|
local default_setup = { capabilities = capabilities, on_attach = default_attach }
|
2021-12-17 04:51:46 +00:00
|
|
|
|
2024-09-19 18:31:40 +00:00
|
|
|
local maybe_setup = function(config, options)
|
|
|
|
-- Setup LSP config if the lsp command exists
|
|
|
|
if vim.fn.executable(config.document_config.default_config.cmd[1]) == 1 then
|
|
|
|
config.setup(options)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-12-17 04:51:46 +00:00
|
|
|
-- Configure each server
|
2024-09-19 18:31:40 +00:00
|
|
|
maybe_setup(lsp_config.gopls, default_setup)
|
|
|
|
maybe_setup(lsp_config.pyright, default_setup)
|
|
|
|
maybe_setup(lsp_config.bashls, {
|
2023-11-15 17:21:54 +00:00
|
|
|
capabilities = capabilities,
|
|
|
|
on_attach = default_attach,
|
|
|
|
settings = {
|
|
|
|
bashIde = {
|
|
|
|
-- Disable shellcheck linting because we have it enabled in null-ls
|
|
|
|
-- Some machines I use aren't configured with npm so bashls cannot
|
|
|
|
-- be relied on as the sole source of shellcheck linting.
|
|
|
|
shellcheckPath = "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2023-06-13 21:59:37 +00:00
|
|
|
|
|
|
|
-- Set up rust analyzer (preferred) or rls
|
2024-10-28 23:18:12 +00:00
|
|
|
-- NOTE: For version 0.10 or higher, rustaceanvim is initialized in ftconfig
|
|
|
|
-- Maybe all lsp configs should be set up as part of their ftconfig
|
|
|
|
if vim.fn.has("nvim-0.10") == 1 then
|
|
|
|
maybe_setup(lsp_config.rust_analyzer, {
|
|
|
|
capabilities = capabilities,
|
|
|
|
on_attach = default_attach,
|
|
|
|
settings = {
|
|
|
|
["rust-analyzer"] = {
|
|
|
|
cargo = {
|
|
|
|
features = "all",
|
2023-06-13 21:59:37 +00:00
|
|
|
},
|
|
|
|
},
|
2024-10-28 23:18:12 +00:00
|
|
|
},
|
|
|
|
})
|
2024-09-19 18:31:40 +00:00
|
|
|
maybe_setup(lsp_config.rls, default_setup)
|
2023-06-13 21:59:37 +00:00
|
|
|
end
|
2022-10-14 21:53:03 +00:00
|
|
|
|
|
|
|
-- Configure neovim dev for when sumneko_lua is installed
|
|
|
|
utils.try_require("neodev", function(neodev)
|
2023-12-08 19:43:05 +00:00
|
|
|
local config = {}
|
2024-07-18 00:18:36 +00:00
|
|
|
utils.try_require("dapui", function()
|
2023-12-08 19:43:05 +00:00
|
|
|
config.plugins = { "nvim-dap-ui" }
|
|
|
|
config.types = true
|
2024-07-18 00:18:36 +00:00
|
|
|
end)
|
2023-12-08 19:43:05 +00:00
|
|
|
neodev.setup(config)
|
2022-10-14 21:53:03 +00:00
|
|
|
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
|
2024-05-29 20:16:28 +00:00
|
|
|
local already_setup = lsp_config.util.available_servers()
|
2022-09-08 19:23:41 +00:00
|
|
|
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)
|
2023-06-14 00:24:37 +00:00
|
|
|
if server == "lua_ls" then
|
2024-11-04 19:36:57 +00:00
|
|
|
-- Disable formatting with lua_ls because I use stylua
|
2023-06-14 00:24:37 +00:00
|
|
|
local config = vim.tbl_extend("force", default_setup, {
|
|
|
|
settings = {
|
|
|
|
Lua = {
|
|
|
|
format = {
|
|
|
|
enable = false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
lsp_config[server].setup(config)
|
|
|
|
else
|
|
|
|
lsp_config[server].setup(default_setup)
|
|
|
|
end
|
2022-09-08 19:23:41 +00:00
|
|
|
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
|