local utils = require("utils") vim.api.nvim_create_autocmd("LspAttach", { group = vim.api.nvim_create_augroup("lsp_buf_conf", { clear = true }), callback = function(event_context) local client = vim.lsp.get_client_by_id(event_context.data.client_id) if not client then return end local bufnr = event_context.buf local map = function(mode, l, r, opts) opts = opts or {} opts.silent = true opts.buffer = bufnr vim.keymap.set(mode, l, r, opts) end map("n", "gd", function() vim.lsp.buf.definition { on_list = function(options) local unique_defs = {} local def_loc_hash = {} for _, def_location in pairs(options.items) do local hash_key = def_location.filename .. def_location.lnum if not def_loc_hash[hash_key] then def_loc_hash[hash_key] = true table.insert(unique_defs, def_location) end end options.items = unique_defs ---@diagnostic disable-next-line: param-type-mismatch vim.fn.setloclist(0, {}, " ", options) if #options.items > 1 then vim.cmd.lopen() else vim.cmd([[silent! lfirst]]) end end, } end, { desc = "go to definition" }) map("n", "", vim.lsp.buf.definition) map("n", "K", function() vim.lsp.buf.hover { border = "single", max_height = 25, max_width = 120 } end) map("n", "", vim.lsp.buf.signature_help) map("n", "rn", vim.lsp.buf.rename, { desc = "variable rename" }) map("n", "ca", vim.lsp.buf.code_action, { desc = "LSP code action" }) map("n", "wa", vim.lsp.buf.add_workspace_folder, { desc = "add workspace folder" }) map("n", "wr", vim.lsp.buf.remove_workspace_folder, { desc = "remove workspace folder" }) map("n", "wl", function() vim.print(vim.lsp.buf.list_workspace_folders()) end, { desc = "list workspace folder" }) if client.name == "ruff" then client.server_capabilities.hoverProvider = false end vim.lsp.inlay_hint.enable(true, { buffer = bufnr }) if client.server_capabilities.documentHighlightProvider then local gid = vim.api.nvim_create_augroup("lsp_document_highlight", { clear = true }) vim.api.nvim_create_autocmd("CursorHold", { group = gid, buffer = bufnr, callback = function() vim.lsp.buf.document_highlight() end, }) vim.api.nvim_create_autocmd("CursorMoved", { group = gid, buffer = bufnr, callback = function() vim.lsp.buf.clear_references() end, }) vim.api.nvim_create_autocmd("CursorMoved", { group = gid, buffer = bufnr, callback = function() vim.lsp.buf.clear_references() end, }) end end, nested = true, desc = "configure buffer keymap and behavior based on LSP", }) local capabilities = require("lsp_utils").get_default_capabilities() vim.lsp.config("*", { capabilities = capabilities, flags = { debounce_text_changes = 500, }, }) local enabled_lsp_servers = { pyright = "delance-langserver", ruff = "ruff", lua_ls = "lua-language-server", vimls = "vim-language-server", bashls = "bash-language-server", yamlls = "yaml-language-server", } for server_name, lsp_executable in pairs(enabled_lsp_servers) do if utils.executable(lsp_executable) then vim.lsp.enable(server_name) else local msg = string.format( "Executable '%s' for server '%s' not found! Server will not be enabled", lsp_executable, server_name ) vim.notify(msg, vim.log.levels.WARN, { title = "nvim-config" }) end end