aboutsummaryrefslogtreecommitdiffstats
path: root/lua/lspconfig/util.lua
diff options
context:
space:
mode:
authorRaphael <glepnir@neovim.pro>2022-12-27 11:59:28 +0800
committerGitHub <noreply@github.com>2022-12-27 11:59:28 +0800
commit95ae63dcefe91dae921dded0adf85343b288c491 (patch)
treeadab4f22eaa0149d05c2d5ad9a8415a01b8ee5a7 /lua/lspconfig/util.lua
parentfix: check workspace after client initial (#2356) (diff)
downloadnvim-lspconfig-95ae63dcefe91dae921dded0adf85343b288c491.tar
nvim-lspconfig-95ae63dcefe91dae921dded0adf85343b288c491.tar.gz
nvim-lspconfig-95ae63dcefe91dae921dded0adf85343b288c491.tar.bz2
nvim-lspconfig-95ae63dcefe91dae921dded0adf85343b288c491.tar.lz
nvim-lspconfig-95ae63dcefe91dae921dded0adf85343b288c491.tar.xz
nvim-lspconfig-95ae63dcefe91dae921dded0adf85343b288c491.tar.zst
nvim-lspconfig-95ae63dcefe91dae921dded0adf85343b288c491.zip
fix: generate correct workspace didchange request params (#2361)
* fix: generate correct workspace didchange request params * fix: logic rewrite * fix: logic rewrite * fix: spell
Diffstat (limited to 'lua/lspconfig/util.lua')
-rw-r--r--lua/lspconfig/util.lua135
1 files changed, 67 insertions, 68 deletions
diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua
index 92594077..1426b6b1 100644
--- a/lua/lspconfig/util.lua
+++ b/lua/lspconfig/util.lua
@@ -271,6 +271,56 @@ function M.server_per_root_dir_manager(make_config)
local new_config = make_config(root_dir)
local client = get_client_from_cache(new_config)
+ --TODO(glepnir): do we need check language server support the workspaceFOlders?
+ --some server support it but the it not show in server_capabilities
+ local register_workspace_folders = function(client_instance)
+ local params = {
+ event = {
+ added = { { uri = vim.uri_from_fname(root_dir), name = root_dir } },
+ removed = {},
+ },
+ }
+ for _, schema in ipairs(client_instance.workspace_folders or {}) do
+ if schema.name == root_dir then
+ return
+ end
+ end
+ client_instance.rpc.notify('workspace/didChangeWorkspaceFolders', params)
+ if not client_instance.workspace_folders then
+ client.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 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)
+ register_workspace_folders(client_instance)
+ timer:stop()
+ timer:close()
+ end
+ end)
+ )
+ end
+
+ if client and clients[root_dir] then
+ if client.initialized then
+ lsp.buf_attach_client(bufnr, client.id)
+ else
+ attach_after_client_initialized(bufnr, client)
+ end
+ return
+ end
+
local start_new_client = function()
-- do nothing if the client is not enabled
if new_config.enabled == false then
@@ -318,78 +368,27 @@ function M.server_per_root_dir_manager(make_config)
table.insert(clients[root_dir], client_id)
end
- if client then
- local register_workspace_folders = function(client_instance)
- local params = lsp.util.make_workspace_params(
- { { uri = vim.uri_from_fname(root_dir), name = root_dir } },
- { {} }
- )
- for _, schema in ipairs(client_instance.workspace_folders or {}) do
- if schema.name == params.event.added[1].name then
- return
- end
- end
- client_instance.rpc.notify('workspace/didChangeWorkspaceFolders', params)
- if not client_instance.workspace_folders then
- client.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 server_support_workspace = function(client_instance)
- if
- client_instance.server_capabilities and client_instance.server_capabilities.workspace
- -- according the lsp spec doc the server capability is optional
- -- some servers not add this field. so use the workspace to check is enough?
- -- and client_instance.server_capabilities.workspace.workspaceFolders
- then
- return true
- end
- return false
- 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
+ if not client then
+ start_new_client()
+ return
+ end
- if not client.initialized then
- local timer = vim.loop.new_timer()
- timer:start(
- 0,
- 10,
- vim.schedule_wrap(function()
- if client.initialized and not timer:is_closing() then
- if server_support_workspace(client) then
- lsp.buf_attach_client(bufnr, client.id)
- register_workspace_folders(client)
- else
- -- if not support workspace spawn a new one
- start_new_client()
- end
- timer:stop()
- timer:close()
- end
- end)
- )
- 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
- if server_support_workspace(client) then
- lsp.buf_attach_client(bufnr, client.id)
- register_workspace_folders(client)
- 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)
+ return
end
- start_new_client()
- return client_id
+ lsp.buf_attach_client(bufnr, client.id)
+ register_workspace_folders(client)
end
function manager.clients()