dotfiles/modules/programs/nvim/plugins/nvim-lspconfig.lua

163 lines
4.1 KiB
Lua

local lsp_lines = require("lsp_lines")
lsp_lines.setup()
-- Disable virtual_text since it's redundant due to lsp_lines.
vim.diagnostic.config({
virtual_text = false,
})
local capabilities = vim.lsp.protocol.make_client_capabilities()
-- NOTE for nvim-ufo
-- Tell the server the capability of foldingRange,
-- Neovim hasn't added foldingRange to default capabilities, users must add it manually
capabilities.textDocument.foldingRange = {
dynamicRegistration = false,
lineFoldingOnly = true,
}
local lspconfig = require("lspconfig")
local on_attach_def = function(client, bufnr)
require("which-key").register({
K = {
function()
local winid = require("ufo").peekFoldedLinesUnderCursor()
if not winid then
vim.lsp.buf.hover()
end
end,
"Hover",
},
["<leader>"] = {
l = {
name = "lsp",
d = { vim.diagnostic.open_float, "Open diagnostic window" },
c = { vim.lsp.buf.code_action, "Code action" },
r = {
function()
return ":IncRename " .. vim.fn.expand("<cword>")
end,
"Rename",
expr = true,
},
f = {
function()
vim.lsp.buf.format({ async = true })
end,
"Format (lsp)",
mode = { "n", "v" },
},
},
t = {
l = { lsp_lines.toggle, "LSP lines" },
i = {
function()
vim.lsp.inlay_hint(bufnr, nil)
end,
"LSP inlay hints",
},
},
},
g = {
d = {
function()
require("telescope.builtin").lsp_definitions({ reuse_win = true })
end,
"Goto definition",
},
t = {
function()
require("telescope.builtin").lsp_type_definitions({ reuse_win = true })
end,
"Goto type defininition",
},
r = { "<cmd>Telescope lsp_references<cr>", "Goto references" },
D = { vim.lsp.buf.declaration, "Goto declaration" },
I = { "<cmd>Telescope lsp_implementations<cr>", "Goto implementation" },
K = { vim.lsp.buf.signature_help, "Signature help" },
},
["["] = {
d = { vim.diagnostic.goto_prev, "Previous diagnostic" },
},
["]"] = {
d = { vim.diagnostic.goto_next, "Next diagnostic" },
},
}, { buffer = bufnr, silent = true })
if client.server_capabilities.inlayHintProvider then
local slow_lsp_servers = {
"rust_analyzer",
}
local timeout = vim.tbl_contains(slow_lsp_servers, client.name, {}) and 500 or 0
vim.defer_fn(function()
vim.lsp.inlay_hint(bufnr, true)
end, timeout)
end
end
local lspconfig_default_options = {
on_attach = on_attach_def,
capabilities = capabilities,
}
---function to add default options to lspconfig
---@param lsp string
---@param options table
---@return nil
local function lspconfig_setup(lsp, options)
local coq_options = require("coq").lsp_ensure_capabilities({})
local merged_options = vim.tbl_deep_extend("force", coq_options, lspconfig_default_options, options)
lspconfig[lsp].setup(merged_options)
end
local servers = {
"bashls",
"nil_ls",
"pylsp",
"ruff_lsp",
"typst_lsp",
"gopls",
}
for _, lsp in ipairs(servers) do
lspconfig_setup(lsp, {})
end
lspconfig_setup("rust_analyzer", {
settings = {
["rust-analyzer"] = {
checkOnSave = {
command = "clippy",
},
},
},
})
lspconfig_setup("lua_ls", {
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = "LuaJIT",
path = vim.split(package.path, ";"),
},
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = { "vim" },
},
workspace = {
-- Make the server aware of Neovim runtime files
library = vim.api.nvim_get_runtime_file("", true),
checkThirdParty = false,
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = {
enable = false,
},
format = {
enable = false,
},
hint = {
enable = true,
},
},
},
})