@@ -20,8 +20,6 @@ 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 = {}
2523
2624local function lsp_event (name ) vim .api .nvim_exec_autocmds (" User" , { pattern = " AstroLsp" .. name , modeline = false }) end
2725
5654function M .lsp_setup (server )
5755 local opts , default_handler
5856 if M .config .native_lsp_config then
59- opts = M .lsp_opts (server )
57+ opts = M .lsp_config (server )
6058 default_handler = function (server_name ) vim .lsp .enable (server_name ) end
6159 else
6260 -- if server doesn't exist, set it up from user server definition
@@ -191,29 +189,34 @@ function M.on_attach(client, bufnr)
191189 if not M .attached_clients [client .id ] then M .attached_clients [client .id ] = client end
192190end
193191
192+ -- A table of LSP clients that have been configured already
193+ local is_configured = {}
194+
194195--- Configure the language server using `vim.lsp.config`
195196--- @param server_name string The name of the server
197+ --- @return vim.lsp.Config # The resolved configuration
196198function 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
199+ if not is_configured [server_name ] then
200+ local config = M .config .config [server_name ] or {}
201+ local existing_on_attach = (vim .lsp .config [server_name ] or {}).on_attach
202+ local user_on_attach = config .on_attach
203+ config .on_attach = function (...)
204+ if type (existing_on_attach ) == " function" then existing_on_attach (... ) end
205+ M .on_attach (... )
206+ if type (user_on_attach ) == " function" then user_on_attach (... ) end
207+ end
208+ vim .lsp .config (server_name , config )
209+ is_configured [server_name ] = true
204210 end
205- vim .lsp .config ( server_name , config )
211+ return vim .lsp .config [ server_name ]
206212end
207213
208214--- Get the server configuration for a given language server to be provided to the server's `setup()` call
209215--- @param server_name string The name of the server
210216--- @return table # The table of LSP options used when setting up the given language server
211217function M .lsp_opts (server_name )
212218 -- 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
219+ if M .config .native_lsp_config then return M .lsp_config (server_name ) or {} end
217220 local opts = { capabilities = M .config .capabilities , flags = M .config .flags }
218221 if M .config .config [server_name ] then opts = vim .tbl_deep_extend (" force" , opts , M .config .config [server_name ]) end
219222 assert (opts )
0 commit comments