local components = { { name = "inlay_hint", callback = function(client, buf) if client.server_capabilities.inlayHintProvider then vim.lsp.inlay_hint.enable(true, { bufnr = buf }) end end, }, { name = "document_highlight", callback = function(client, buf) if client.server_capabilities.documentHighlightProvider then local augroup = vim.augroup(string.format("@ivy.lsp.document_highlight.%d", buf), true) augroup({ "CursorHold", "CursorHoldI" }, nil, { buffer = buf, desc = "highlight lsp reference", }, function() vim.lsp.buf.clear_references() vim.lsp.buf.document_highlight() end) augroup("CursorMoved", nil, { buffer = buf, desc = "clear lsp references", }, function() vim.lsp.buf.clear_references() end) end end, }, { name = "linked_editing_range", callback = function(client, _) if client.server_capabilities.linkedEditingRangeProvider then vim.lsp.linked_editing_range.enable(true, { client_id = client.id }) end end, }, { name = "document_color", callback = function(client, bufnr) if client.server_capabilities.documentColorProvider then vim.lsp.document_color.enable(true, bufnr) end end, }, { name = "user", callback = vim.g.lsp_on_attach }, } vim.iter(ipairs(components)):each(function(_, fn) vim.augroup("@ivy.lsp." .. fn.name, true)("LspAttach", nil, {}, function(ev) local client = vim.lsp.get_client_by_id(ev.data.client_id) if client == nil then return end local ok, result = pcall(fn.callback, client, ev.buf) if not ok then vim.notify( ("unable to run attach events for lsp server '%s':\n\t%s"):format(client.name, vim.inspect(result)), vim.log.levels.ERROR ) return end end) end)