From 6d95058866c83cb391d19967dd9185da8de14291 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Tue, 9 Sep 2025 14:07:46 -0500 Subject: [PATCH] add the ability to start and stop the lsp --- client/package-lock.json | 4 +- client/src/extension.ts | 131 ++++++++++++++++++++++++++++----------- package-lock.json | 6 +- package.json | 12 +++- 4 files changed, 111 insertions(+), 42 deletions(-) diff --git a/client/package-lock.json b/client/package-lock.json index 37cea08..ff07819 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -1,12 +1,12 @@ { "name": "nushell-lsp-client", - "version": "2.0.0", + "version": "2.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "nushell-lsp-client", - "version": "2.0.0", + "version": "2.0.1", "license": "MIT", "dependencies": { "vscode-languageclient": "9.0.1", diff --git a/client/src/extension.ts b/client/src/extension.ts index c932597..728e7b5 100644 --- a/client/src/extension.ts +++ b/client/src/extension.ts @@ -44,6 +44,63 @@ function findNushellExecutable(): string | null { } } +function startLanguageServer( + context: vscode.ExtensionContext, + found_nushell_path: string, +): void { + // Use Nushell's native LSP server + const serverOptions: ServerOptions = { + run: { + command: found_nushell_path, + args: ['--lsp'], + }, + debug: { + command: found_nushell_path, + args: ['--lsp'], + }, + }; + + // Options to control the language client + const clientOptions: LanguageClientOptions = { + initializationOptions: { + timeout: 10000, // 10 seconds + }, + // Register the server for nushell files + documentSelector: [{ scheme: 'file', language: 'nushell' }], + synchronize: { + // Notify the server about file changes to nushell files + fileEvents: vscode.workspace.createFileSystemWatcher('**/*.nu'), + }, + }; + + // Create the language client and start the client. + client = new LanguageClient( + 'nushellLanguageServer', + 'Nushell Language Server', + serverOptions, + clientOptions, + ); + + // Start the language client and register a disposable that stops it when disposed + client.start().catch((error) => { + vscode.window.showErrorMessage( + `Failed to start Nushell language server: ${error.message}`, + ); + }); + + const disposable = new vscode.Disposable(() => { + if (client) { + client.stop().catch((error) => { + console.error( + 'Failed to stop Nushell Language Server on dispose:', + error, + ); + }); + } + }); + context.subscriptions.push(disposable); +} + export function activate(context: vscode.ExtensionContext) { console.log('Terminals: ' + (vscode.window).terminals.length); @@ -111,45 +168,47 @@ export function activate(context: vscode.ExtensionContext) { return; } - // Use Nushell's native LSP server - const serverOptions: ServerOptions = { - run: { - command: found_nushell_path, - args: ['--lsp'], - }, - debug: { - command: found_nushell_path, - args: ['--lsp'], - }, - }; - - // Options to control the language client - const clientOptions: LanguageClientOptions = { - initializationOptions: { - timeout: 10000, // 10 seconds + // Start the language server when the extension is activated + startLanguageServer(context, found_nushell_path); + + // Register a command to stop the language server + const stopCommand = vscode.commands.registerCommand( + 'nushell.stopLanguageServer', + async () => { + if (client) { + try { + await client.stop(); + client = undefined; + vscode.window.showInformationMessage( + 'Nushell Language Server stopped.', + ); + } catch (error) { + vscode.window.showErrorMessage( + `Failed to stop Nushell Language Server: ${error}`, + ); + } + } else { + vscode.window.showInformationMessage( + 'Nushell Language Server is not running.', + ); + } }, - // Register the server for nushell files - documentSelector: [{ scheme: 'file', language: 'nushell' }], - synchronize: { - // Notify the server about file changes to nushell files - fileEvents: vscode.workspace.createFileSystemWatcher('**/*.nu'), + ); + context.subscriptions.push(stopCommand); + + // Register a command to start the language server + const startCommand = vscode.commands.registerCommand( + 'nushell.startLanguageServer', + () => { + startLanguageServer(context, found_nushell_path); + if (client) { + vscode.window.showInformationMessage( + 'Nushell Language Server started.', + ); + } }, - }; - - // Create the language client and start the client. - client = new LanguageClient( - 'nushellLanguageServer', - 'Nushell Language Server', - serverOptions, - clientOptions, ); - - // Start the client. This will also launch the server - client.start().catch((error) => { - vscode.window.showErrorMessage( - `Failed to start Nushell language server: ${error.message}`, - ); - }); + context.subscriptions.push(startCommand); } export function deactivate(): Thenable | undefined { diff --git a/package-lock.json b/package-lock.json index d0545f5..e54ce78 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "vscode-nushell-lang", - "version": "2.0.0", + "version": "2.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "vscode-nushell-lang", - "version": "2.0.0", + "version": "2.0.1", "hasInstallScript": true, "license": "MIT", "dependencies": { @@ -38,7 +38,7 @@ }, "client": { "name": "nushell-lsp-client", - "version": "2.0.0", + "version": "2.0.1", "license": "MIT", "dependencies": { "vscode-languageclient": "9.0.1", diff --git a/package.json b/package.json index e3b3d9f..da59a55 100644 --- a/package.json +++ b/package.json @@ -139,7 +139,17 @@ "scope": "window" } } - } + }, + "commands": [ + { + "command": "nushell.stopLanguageServer", + "title": "Nushell: Stop Language Server" + }, + { + "command": "nushell.startLanguageServer", + "title": "Nushell: Start Language Server" + } + ] }, "scripts": { "vscode:prepublish": "npm run lint && npm run compile",