11local M = {}
22
3- local utils = require " astrolsp.utils"
43local tbl_contains = vim .tbl_contains
54local tbl_isempty = vim .tbl_isempty
65
76M .lsp_progress = {}
87
8+ local function event (name )
9+ vim .schedule (function () vim .api .nvim_exec_autocmds (" User" , { pattern = " AstroLsp" .. name , modeline = false }) end )
10+ end
11+
912function M .setup (opts )
1013 opts = opts or {}
1114 for section , default in pairs (require " astrolsp.config" ) do
@@ -44,12 +47,18 @@ function M.setup(opts)
4447 if progress [id ].kind == " end" then
4548 vim .defer_fn (function ()
4649 progress [id ] = nil
47- utils . event " Progress"
50+ event " Progress"
4851 end , 100 )
4952 end
50- utils . event " Progress"
53+ event " Progress"
5154 orig_handler (_ , msg , info )
5255 end
56+
57+ if M .options .features .lsp_handlers then
58+ vim .lsp .handlers [" textDocument/hover" ] = vim .lsp .with (vim .lsp .handlers .hover , { border = " rounded" , silent = true })
59+ vim .lsp .handlers [" textDocument/signatureHelp" ] =
60+ vim .lsp .with (vim .lsp .handlers .signature_help , { border = " rounded" , silent = true })
61+ end
5362end
5463
5564M .diagnostics = { [0 ] = {}, {}, {}, {} }
@@ -73,7 +82,7 @@ function M.setup_diagnostics()
7382 M .options .diagnostics ,
7483 }
7584
76- vim .diagnostic .config (M .diagnostics [vim . g .diagnostics_mode ])
85+ vim .diagnostic .config (M .diagnostics [M . options . features .diagnostics_mode ])
7786end
7887
7988--- Helper function to set up a given server with the Neovim LSP client
@@ -109,6 +118,18 @@ local function add_buffer_autocmd(augroup, bufnr, autocmds)
109118 end
110119end
111120
121+ local function del_buffer_autocmd (augroup , bufnr )
122+ local cmds_found , cmds = pcall (vim .api .nvim_get_autocmds , { group = augroup , buffer = bufnr })
123+ if cmds_found then vim .tbl_map (function (cmd ) vim .api .nvim_del_autocmd (cmd .id ) end , cmds ) end
124+ end
125+
126+ local function has_capability (capability , filter )
127+ for _ , client in ipairs ((vim .lsp .get_clients or vim .lsp .get_active_clients )(filter )) do
128+ if client .supports_method (capability ) then return true end
129+ end
130+ return false
131+ end
132+
112133--- The `on_attach` function used by AstroNvim
113134--- @param client table The LSP client details when attaching
114135--- @param bufnr number The buffer that the LSP client is attaching to
@@ -118,14 +139,14 @@ M.on_attach = function(client, bufnr)
118139 events = { " InsertLeave" , " BufEnter" },
119140 desc = " Refresh codelens" ,
120141 callback = function ()
121- if not utils . has_capability (" textDocument/codeLens" , { bufnr = bufnr }) then
122- utils . del_buffer_autocmd (" lsp_codelens_refresh" , bufnr )
142+ if not has_capability (" textDocument/codeLens" , { bufnr = bufnr }) then
143+ del_buffer_autocmd (" lsp_codelens_refresh" , bufnr )
123144 return
124145 end
125- if vim . g . codelens_enabled then vim .lsp .codelens .refresh () end
146+ if M . options . features . codelens then vim .lsp .codelens .refresh () end
126147 end ,
127148 })
128- if vim . g . codelens_enabled then vim .lsp .codelens .refresh () end
149+ if M . options . features . codelens then vim .lsp .codelens .refresh () end
129150 end
130151
131152 if
@@ -148,8 +169,8 @@ M.on_attach = function(client, bufnr)
148169 events = " BufWritePre" ,
149170 desc = " autoformat on save" ,
150171 callback = function ()
151- if not utils . has_capability (" textDocument/formatting" , { bufnr = bufnr }) then
152- utils . del_buffer_autocmd (" lsp_auto_format" , bufnr )
172+ if not has_capability (" textDocument/formatting" , { bufnr = bufnr }) then
173+ del_buffer_autocmd (" lsp_auto_format" , bufnr )
153174 return
154175 end
155176 local autoformat_enabled = vim .b [bufnr ].autoformat_enabled
@@ -167,8 +188,8 @@ M.on_attach = function(client, bufnr)
167188 events = { " CursorHold" , " CursorHoldI" },
168189 desc = " highlight references when cursor holds" ,
169190 callback = function ()
170- if not utils . has_capability (" textDocument/documentHighlight" , { bufnr = bufnr }) then
171- utils . del_buffer_autocmd (" lsp_document_highlight" , bufnr )
191+ if not has_capability (" textDocument/documentHighlight" , { bufnr = bufnr }) then
192+ del_buffer_autocmd (" lsp_document_highlight" , bufnr )
172193 return
173194 end
174195 vim .lsp .buf .document_highlight ()
@@ -183,16 +204,14 @@ M.on_attach = function(client, bufnr)
183204 end
184205
185206 if client .supports_method " textDocument/inlayHint" then
186- if vim .b [bufnr ].inlay_hints_enabled == nil then vim .b [bufnr ].inlay_hints_enabled = vim . g . inlay_hints_enabled end
207+ if vim .b [bufnr ].inlay_hints == nil then vim .b [bufnr ].inlay_hints = M . options . features . inlay_hints end
187208 -- TODO: remove check after dropping support for Neovim v0.9
188- if vim .lsp .inlay_hint and vim .b [bufnr ].inlay_hints_enabled then vim .lsp .inlay_hint (bufnr , true ) end
209+ if vim .lsp .inlay_hint and vim .b [bufnr ].inlay_hints then vim .lsp .inlay_hint (bufnr , true ) end
189210 end
190211
191212 if client .supports_method and vim .lsp .semantic_tokens then
192- if vim .b [bufnr ].semantic_tokens_enabled == nil then
193- vim .b [bufnr ].semantic_tokens_enabled = vim .g .semantic_tokens_enabled
194- end
195- if not vim .g .semantic_tokens_enabled then vim .lsp .semantic_tokens [" stop" ](bufnr , client .id ) end
213+ if vim .b [bufnr ].semantic_tokens == nil then vim .b [bufnr ].semantic_tokens = M .options .features .semantic_tokens end
214+ if not vim .b [bufnr ].semantic_tokens then vim .lsp .semantic_tokens [" stop" ](bufnr , client .id ) end
196215 end
197216
198217 for mode , maps in pairs (M .options .mappings ) do
@@ -229,15 +248,15 @@ function M.config(server_name)
229248 local opts = vim .tbl_deep_extend (
230249 " force" ,
231250 vim .tbl_deep_extend (" force" , server .document_config .default_config , server ),
232- { capabilities = M .capabilities , flags = M .flags }
251+ { capabilities = M .options . capabilities , flags = M . options .flags }
233252 )
234253 -- HACK: add astronvim interoperability, remove after AstroNvim v4
235254 if type (astronvim ) == " table" and type (astronvim .user_opts ) == " function" then
236255 opts = astronvim .user_opts (" lsp.config." .. server_name , opts )
237256 end
238- if M .options .config [server_name ] then
239- opts = assert (vim . tbl_deep_extend ( " force " , opts , M . options . config [ server_name ]) )
240- end
257+ if M .options .config [server_name ] then opts = vim . tbl_deep_extend ( " force " , opts , M . options . config [ server_name ]) end
258+ assert (opts )
259+
241260 local old_on_attach = require (" lspconfig" )[server_name ].on_attach
242261 local user_on_attach = opts .on_attach
243262 opts .on_attach = function (client , bufnr )
0 commit comments