fix(lsp): gracefully handle nil workspaceFolders (#16284)

* handle when workspaceFolder is nil in buf.list_workspace_folders and buf.add_workspace_folder
This commit is contained in:
Michael Lingelbach 2021-11-11 01:15:59 -08:00 committed by GitHub
parent 14def4d227
commit 0ecc58c277
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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