diff options
| author | Raphael <glepnir@neovim.pro> | 2022-12-30 13:38:26 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-30 13:38:26 +0800 |
| commit | 9c73b57ed03ad0a906e2cdc2fc348bf86ae53e3a (patch) | |
| tree | 6f3d6299a8f3abfe0226a9d1115958ae366368cb /lua/lspconfig/util.lua | |
| parent | docs: update server_configurations.md (diff) | |
| download | nvim-lspconfig-9c73b57ed03ad0a906e2cdc2fc348bf86ae53e3a.tar nvim-lspconfig-9c73b57ed03ad0a906e2cdc2fc348bf86ae53e3a.tar.gz nvim-lspconfig-9c73b57ed03ad0a906e2cdc2fc348bf86ae53e3a.tar.bz2 nvim-lspconfig-9c73b57ed03ad0a906e2cdc2fc348bf86ae53e3a.tar.lz nvim-lspconfig-9c73b57ed03ad0a906e2cdc2fc348bf86ae53e3a.tar.xz nvim-lspconfig-9c73b57ed03ad0a906e2cdc2fc348bf86ae53e3a.tar.zst nvim-lspconfig-9c73b57ed03ad0a906e2cdc2fc348bf86ae53e3a.zip | |
refactor: rewrite client reuse logic (#2371)
* refactor: rewrite client reuse logic
* fix: not register workspace after new start
Diffstat (limited to 'lua/lspconfig/util.lua')
| -rw-r--r-- | lua/lspconfig/util.lua | 65 |
1 files changed, 28 insertions, 37 deletions
diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index 9b0bb53e..843e63cb 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -235,7 +235,6 @@ function M.server_per_root_dir_manager(make_config) local manager = {} function manager.add(root_dir, single_file, bufnr) - local client_id root_dir = M.path.sanitize(root_dir) local client_id_iterator = function(client_ids, conf) @@ -247,6 +246,15 @@ function M.server_per_root_dir_manager(make_config) end end + local register_to_clients = function(id) + if not clients[root_dir] then + clients[root_dir] = {} + end + if not vim.tbl_contains(clients[root_dir], id) then + table.insert(clients[root_dir], id) + end + end + -- get client which support workspace from clients table local get_client_from_cache = function(conf) if vim.tbl_count(clients) == 0 then @@ -287,26 +295,23 @@ function M.server_per_root_dir_manager(make_config) end client_instance.rpc.notify('workspace/didChangeWorkspaceFolders', params) if not client_instance.workspace_folders then - client.workspace_folders = {} + client_instance.workspace_folders = {} end table.insert(client_instance.workspace_folders, params.event.added[1]) - if not clients[root_dir] then - clients[root_dir] = {} - end - table.insert(clients[root_dir], client_instance.id) end - local attach_after_client_initialized = function(buffer_nr, client_instance) + local attach_after_client_initialized = function(client_instance) local timer = vim.loop.new_timer() timer:start( 0, 10, vim.schedule_wrap(function() if client_instance.initialized and not timer:is_closing() then - lsp.buf_attach_client(buffer_nr, client_instance.id) + lsp.buf_attach_client(bufnr, client_instance.id) if not single_file then register_workspace_folders(client_instance) end + register_to_clients(client_instance.id) timer:stop() timer:close() end @@ -314,15 +319,20 @@ function M.server_per_root_dir_manager(make_config) ) end - if client and clients[root_dir] then + if client then if client.initialized then lsp.buf_attach_client(bufnr, client.id) + if not single_file then + register_workspace_folders(client) + end + register_to_clients(client.id) else - attach_after_client_initialized(bufnr, client) + attach_after_client_initialized(client) end return end + local client_id local start_new_client = function() -- do nothing if the client is not enabled if new_config.enabled == false then @@ -339,7 +349,11 @@ function M.server_per_root_dir_manager(make_config) return end new_config.on_exit = M.add_hook_before(new_config.on_exit, function() - clients[root_dir] = nil + for k, v in pairs(clients[root_dir]) do + if v == client_id then + table.remove(clients[root_dir], k) + end + end end) -- Launch the server in the root directory used internally by lspconfig, if otherwise unset @@ -356,41 +370,18 @@ function M.server_per_root_dir_manager(make_config) new_config.workspace_folders = nil end client_id = lsp.start_client(new_config) - - -- Handle failures in start_client - if not client_id then - return - end - - lsp.buf_attach_client(bufnr, client_id) - - if not clients[root_dir] then - clients[root_dir] = {} - end - table.insert(clients[root_dir], client_id) end if not client then start_new_client() - return - end - - -- if in single file mode just return this client id don't insert the new - -- root dir into the workspace_folders - if single_file then - lsp.buf_attach_client(bufnr, client.id) - return end - --this for reload from session if have multiple same filetype buffers in session. - --first buffer spawn a new client second buffer need wait for the client initialized - if not client.initialized then - attach_after_client_initialized(bufnr, client) + if not client_id then return end - lsp.buf_attach_client(bufnr, client.id) - register_workspace_folders(client) + lsp.buf_attach_client(bufnr, client_id) + register_to_clients(client_id) end function manager.clients() |
