From 0ecc58c2775ae576a3325aec163aa1018aad9e0a Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Thu, 11 Nov 2021 01:15:59 -0800 Subject: [PATCH] fix(lsp): gracefully handle nil workspaceFolders (#16284) * handle when workspaceFolder is nil in buf.list_workspace_folders and buf.add_workspace_folder --- runtime/lua/vim/lsp/buf.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index b0348e8307..747d761730 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -369,7 +369,7 @@ end function M.list_workspace_folders() local workspace_folders = {} for _, client in pairs(vim.lsp.buf_get_clients()) do - for _, folder in pairs(client.workspaceFolders) do + for _, folder in pairs(client.workspaceFolders or {}) do table.insert(workspace_folders, folder.name) end end @@ -389,7 +389,7 @@ function M.add_workspace_folder(workspace_folder) local params = util.make_workspace_params({{uri = vim.uri_from_fname(workspace_folder); name = workspace_folder}}, {{}}) for _, client in pairs(vim.lsp.buf_get_clients()) do local found = false - for _, folder in pairs(client.workspaceFolders) do + for _, folder in pairs(client.workspaceFolders or {}) do if folder.name == workspace_folder then found = true print(workspace_folder, "is already part of this workspace") @@ -398,6 +398,9 @@ function M.add_workspace_folder(workspace_folder) end if not found then vim.lsp.buf_notify(0, 'workspace/didChangeWorkspaceFolders', params) + if not client.workspaceFolders then + client.workspaceFolders = {} + end table.insert(client.workspaceFolders, params.event.added[1]) end end