@@ -20,6 +20,8 @@ M.config = require "astrolsp.config"
2020M .lsp_progress = {}
2121--- A table of LSP clients that have been attached with AstroLSP
2222M .attached_clients = {}
23+ -- A table of LSP clients that have been configured already
24+ M .is_configured = {}
2325
2426local function lsp_event (name ) vim .api .nvim_exec_autocmds (" User" , { pattern = " AstroLsp" .. name , modeline = false }) end
2527
5254--- Helper function to set up a given server with the Neovim LSP client
5355--- @param server string The name of the server to be setup
5456function M .lsp_setup (server )
55- -- if server doesn't exist, set it up from user server definition
56- local lspconfig_avail , lspconfig = pcall (require , " lspconfig" )
57- if lspconfig_avail then
58- local config_avail , config = pcall (require , " lspconfig.configs." .. server )
59- if not config_avail or not config .default_config then
60- local server_definition = M .config .config [server ]
61- if server_definition and server_definition .cmd then
62- require (" lspconfig.configs" )[server ] = { default_config = server_definition }
57+ local opts , default_handler
58+ if M .config .native_lsp_config then
59+ opts = M .lsp_opts (server )
60+ default_handler = function (server_name ) vim .lsp .enable (server_name ) end
61+ else
62+ -- if server doesn't exist, set it up from user server definition
63+ local lspconfig_avail , lspconfig = pcall (require , " lspconfig" )
64+ if lspconfig_avail then
65+ local config_avail , config = pcall (require , " lspconfig.configs." .. server )
66+ if not config_avail or not config .default_config then
67+ local server_definition = M .config .config [server ]
68+ if server_definition and server_definition .cmd then
69+ require (" lspconfig.configs" )[server ] = { default_config = server_definition }
70+ end
71+ end
72+ end
73+ opts = M .lsp_opts (server )
74+ default_handler = function (server_name , _opts )
75+ if lspconfig_avail then
76+ lspconfig [server_name ].setup (_opts )
77+ else
78+ vim .notify ((" No handler defined for `%s`" ):format (server_name ), vim .log .levels .WARN )
6379 end
6480 end
6581 end
66- local opts = M .lsp_opts (server )
6782 local handler = M .config .handlers [server ]
6883 if handler == nil then handler = M .config .handlers [1 ] end
69- if handler then
70- handler (server , opts )
71- elseif handler == nil then
72- if lspconfig_avail then
73- lspconfig [server ].setup (opts )
74- else
75- vim .notify ((" No handler defined for `%s`" ):format (server ), vim .log .levels .WARN )
76- end
77- end
84+ (handler or default_handler )(server , opts )
7885end
7986
8087--- The `on_attach` function used by AstroNvim
@@ -184,10 +191,29 @@ function M.on_attach(client, bufnr)
184191 if not M .attached_clients [client .id ] then M .attached_clients [client .id ] = client end
185192end
186193
194+ --- Configure the language server using `vim.lsp.config`
195+ --- @param server_name string The name of the server
196+ function M .lsp_config (server_name )
197+ local config = M .config .config [server_name ] or {}
198+ local existing_on_attach = (vim .lsp .config [server_name ] or {}).on_attach
199+ local user_on_attach = config .on_attach
200+ config .on_attach = function (...)
201+ if type (existing_on_attach ) == " function" then existing_on_attach (... ) end
202+ M .on_attach (... )
203+ if type (user_on_attach ) == " function" then user_on_attach (... ) end
204+ end
205+ vim .lsp .config (server_name , config )
206+ end
207+
187208--- Get the server configuration for a given language server to be provided to the server's `setup()` call
188209--- @param server_name string The name of the server
189210--- @return table # The table of LSP options used when setting up the given language server
190211function M .lsp_opts (server_name )
212+ -- if native vim.lsp.config, then just return current configuration
213+ if M .config .native_lsp_config then
214+ if not M .is_configured [server_name ] then M .lsp_config (server_name ) end
215+ return vim .lsp .config [server_name ]
216+ end
191217 local opts = { capabilities = M .config .capabilities , flags = M .config .flags }
192218 if M .config .config [server_name ] then opts = vim .tbl_deep_extend (" force" , opts , M .config .config [server_name ]) end
193219 assert (opts )
@@ -230,13 +256,22 @@ function M.setup(opts)
230256 normalize_mappings (opts .mappings )
231257 M .config = vim .tbl_deep_extend (" force" , M .config , opts )
232258
259+ if not vim .lsp .config then -- disable native `vim.lsp.config` if not available
260+ M .config .native_lsp_config = false
261+ end
262+
233263 -- enable necessary capabilities for enabled LSP file operations
234264 local fileOperations = vim .tbl_get (M .config , " file_operations" , " operations" )
235265 if fileOperations and not vim .tbl_isempty (fileOperations ) then
236266 M .config .capabilities = vim .tbl_deep_extend (" force" , M .config .capabilities or {}, {
237267 workspace = { fileOperations = fileOperations },
238268 })
239269 end
270+
271+ if M .config .native_lsp_config then
272+ vim .lsp .config (" *" , { capabilities = M .config .capabilities , flags = M .config .flags })
273+ end
274+
240275 local rename_augroup = vim .api .nvim_create_augroup (" astrolsp_rename_operations" , { clear = true })
241276 vim .api .nvim_create_autocmd (" User" , {
242277 group = rename_augroup ,
0 commit comments