From 7505ee134320457269f3376de8eae978ad6acb2a Mon Sep 17 00:00:00 2001 From: cos Date: Tue, 15 Oct 2024 17:01:32 +0200 Subject: Workaround broken workspaceFolders capability handling According to [language server specification][spec] clients not announcing the workspaceFolders capability should fallback. Primarily to rootUri, and further to rootPath when needed. This commit works around the buggy behaviour with vim-ale. Since I have never ever seen typescript code prior to today, attempting to fix this properly seems a bit too challenging for me. [spec]: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/ --- server/src/server.ts | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/server/src/server.ts b/server/src/server.ts index de62214..4be7e27 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -51,6 +51,7 @@ const documents: TextDocuments = new TextDocuments(TextDocument); let hasConfigurationCapability = false; let hasWorkspaceFolderCapability = false; +let rootUri = ''; connection.onInitialize(async (params: InitializeParams) => { const capabilities = params.capabilities; @@ -87,6 +88,11 @@ connection.onInitialize(async (params: InitializeParams) => { supported: true, }, }; + } else { + process.stderr.write("No hasWorkspaceFolderCapability in onInitialize().\n"); + if (params.rootUri!=null) { + rootUri = params.rootUri; + } } await getPerlAssetsPath(); // Ensures assets are unpacked. Should this be in onInitialized? return result; @@ -183,15 +189,30 @@ async function dispatchForMods(textDocument: TextDocument) { } async function getWorkspaceFoldersSafe(): Promise { - try { - const workspaceFolders = await connection.workspace.getWorkspaceFolders(); - if (!workspaceFolders) { + process.stderr.write("Now in getWorkspaceFoldersSafe().\n"); + if (hasWorkspaceFolderCapability) { + process.stderr.write("getWorkspaceFoldersSafe() has hasWorkspaceFolderCapability.\n"); + try { + const workspaceFolders = await connection.workspace.getWorkspaceFolders(); + if (!workspaceFolders) { + return []; + } else { + return workspaceFolders; + } + } catch (error) { return []; + } + } else { + process.stderr.write("No hasWorkspaceFolderCapability in getWorkspaceFoldersSafe() .\n"); + if (rootUri!=null) { + const dummyFolder: WorkspaceFolder = { + uri: rootUri, + name: rootUri, + } + return [dummyFolder]; } else { - return workspaceFolders; + return []; } - } catch (error) { - return []; } } -- cgit v1.2.3