From 28db44820a94d6c9ebf81bc2129ec7af7fbdd2aa Mon Sep 17 00:00:00 2001 From: Alex Carney Date: Fri, 17 May 2024 19:31:13 +0100 Subject: [PATCH] refactor: rely on the `pygls.server.cwd` setting Rather than try and "guess" the path to `examples/servers` let's just set it via a config option which should be more reliable --- .../pygls-playground/src/extension.ts | 29 +++++++++++++------ .vscode/settings.json | 8 ++--- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.vscode/extensions/pygls-playground/src/extension.ts b/.vscode/extensions/pygls-playground/src/extension.ts index 3ce1acbf..76ced783 100644 --- a/.vscode/extensions/pygls-playground/src/extension.ts +++ b/.vscode/extensions/pygls-playground/src/extension.ts @@ -268,22 +268,33 @@ async function executeServerCommand() { } /** - * If the user has explicitly provided a src directory use that. - * Otherwise, fallback to the examples/servers directory. * * @returns The working directory from which to launch the server */ function getCwd(): string { const config = vscode.workspace.getConfiguration("pygls.server") - const cwd = config.get('cwd') - if (cwd) { - return cwd + let cwd = config.get('cwd') + if (!cwd) { + const message = "Please set a working directory via the `pygls.server.cwd` setting" + logger.error(message) + throw new Error(message) } - const serverDir = path.resolve( - path.join(__dirname, "..", "..", "..", "..", "examples", "servers") - ) - return serverDir + // Check for ${workspaceFolder} etc. + const match = cwd.match(/^\${(\w+)}/) + if (match && (match[1] === 'workspaceFolder' || match[1] === 'workspaceRoot')) { + if (!vscode.workspace.workspaceFolders) { + const message = "The 'pygls-playground' extension requires an open workspace" + logger.error(message) + throw new Error(message) + } + + // Assume a single workspace... + const workspaceFolder = vscode.workspace.workspaceFolders[0].uri.fsPath + cwd = cwd.replace(match[0], workspaceFolder) + } + + return cwd } /** diff --git a/.vscode/settings.json b/.vscode/settings.json index cdf441f0..2b8c92b2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,14 +8,10 @@ "pygls.server.debug": false, // "pygls.server.debugHost": "localhost", // "pygls.server.debugPort": 5678, - "pygls.server.launchScript": "formatting.py", + "pygls.server.launchScript": "formatting.py", // This is relative to `pygls.server.cwd` + "pygls.server.cwd": "${workspaceFolder}/examples/servers", "pygls.trace.server": "off", "pygls.client.documentSelector": [ - // { - // "scheme": "file", - // "language": "json" - // } - // Uncomment to use code_actions or inlay_hints servers { "scheme": "file", "language": "plaintext"