|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import * as vscode from 'vscode'; |
| 5 | +import { inject, injectable } from 'inversify'; |
| 6 | +import { LanguageClient, LanguageClientOptions, Executable } from 'vscode-languageclient/node'; |
| 7 | +import { IDisposable, IDisposableRegistry } from '../../platform/common/types'; |
| 8 | +import { IExtensionSyncActivationService } from '../../platform/activation/types'; |
| 9 | +import { DeepnoteServerInfo, IDeepnoteLspClientManager } from './types'; |
| 10 | +import { PythonEnvironment } from '../../platform/pythonEnvironments/info'; |
| 11 | +import { logger } from '../../platform/logging'; |
| 12 | +import { noop } from '../../platform/common/utils/misc'; |
| 13 | + |
| 14 | +interface LspClientInfo { |
| 15 | + pythonClient?: LanguageClient; |
| 16 | + sqlClient?: LanguageClient; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Manages LSP client connections to Deepnote Toolkit's language servers. |
| 21 | + * Creates and manages Python and SQL LSP clients for code intelligence. |
| 22 | + */ |
| 23 | +@injectable() |
| 24 | +export class DeepnoteLspClientManager |
| 25 | + implements IDeepnoteLspClientManager, IExtensionSyncActivationService, IDisposable |
| 26 | +{ |
| 27 | + // Map notebook URIs to their LSP clients |
| 28 | + private readonly clients = new Map<string, LspClientInfo>(); |
| 29 | + private disposed = false; |
| 30 | + |
| 31 | + constructor(@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry) { |
| 32 | + this.disposables.push(this); |
| 33 | + } |
| 34 | + |
| 35 | + public activate(): void { |
| 36 | + // This service is activated synchronously and doesn't need async initialization |
| 37 | + logger.info('DeepnoteLspClientManager activated'); |
| 38 | + } |
| 39 | + |
| 40 | + public async startLspClients( |
| 41 | + _serverInfo: DeepnoteServerInfo, |
| 42 | + notebookUri: vscode.Uri, |
| 43 | + interpreter: PythonEnvironment |
| 44 | + ): Promise<void> { |
| 45 | + if (this.disposed) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const notebookKey = notebookUri.toString(); |
| 50 | + |
| 51 | + // Check if clients already exist for this notebook |
| 52 | + if (this.clients.has(notebookKey)) { |
| 53 | + logger.trace(`LSP clients already started for ${notebookKey}`); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + logger.info(`Starting LSP clients for ${notebookKey} using interpreter ${interpreter.uri.fsPath}`); |
| 58 | + |
| 59 | + try { |
| 60 | + // Start Python LSP client |
| 61 | + const pythonClient = await this.createPythonLspClient(notebookUri, interpreter); |
| 62 | + |
| 63 | + // Store the client info |
| 64 | + const clientInfo: LspClientInfo = { |
| 65 | + pythonClient |
| 66 | + // TODO: Add SQL client when endpoint is determined |
| 67 | + }; |
| 68 | + |
| 69 | + this.clients.set(notebookKey, clientInfo); |
| 70 | + |
| 71 | + logger.info(`LSP clients started successfully for ${notebookKey}`); |
| 72 | + } catch (error) { |
| 73 | + logger.error(`Failed to start LSP clients for ${notebookKey}:`, error); |
| 74 | + throw error; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public async stopLspClients(notebookUri: vscode.Uri): Promise<void> { |
| 79 | + const notebookKey = notebookUri.toString(); |
| 80 | + const clientInfo = this.clients.get(notebookKey); |
| 81 | + |
| 82 | + if (!clientInfo) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + logger.info(`Stopping LSP clients for ${notebookKey}`); |
| 87 | + |
| 88 | + try { |
| 89 | + // Stop Python client |
| 90 | + if (clientInfo.pythonClient) { |
| 91 | + await clientInfo.pythonClient.stop(); |
| 92 | + } |
| 93 | + |
| 94 | + // Stop SQL client |
| 95 | + if (clientInfo.sqlClient) { |
| 96 | + await clientInfo.sqlClient.stop(); |
| 97 | + } |
| 98 | + |
| 99 | + this.clients.delete(notebookKey); |
| 100 | + logger.info(`LSP clients stopped for ${notebookKey}`); |
| 101 | + } catch (error) { |
| 102 | + logger.error(`Error stopping LSP clients for ${notebookKey}:`, error); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public async stopAllClients(): Promise<void> { |
| 107 | + logger.info('Stopping all LSP clients'); |
| 108 | + |
| 109 | + const stopPromises: Promise<void>[] = []; |
| 110 | + for (const [, clientInfo] of this.clients.entries()) { |
| 111 | + if (clientInfo.pythonClient) { |
| 112 | + stopPromises.push(clientInfo.pythonClient.stop().catch(noop)); |
| 113 | + } |
| 114 | + if (clientInfo.sqlClient) { |
| 115 | + stopPromises.push(clientInfo.sqlClient.stop().catch(noop)); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + await Promise.all(stopPromises); |
| 120 | + this.clients.clear(); |
| 121 | + } |
| 122 | + |
| 123 | + public dispose(): void { |
| 124 | + this.disposed = true; |
| 125 | + // Stop all clients asynchronously but don't wait |
| 126 | + void this.stopAllClients(); |
| 127 | + } |
| 128 | + |
| 129 | + private async createPythonLspClient( |
| 130 | + notebookUri: vscode.Uri, |
| 131 | + interpreter: PythonEnvironment |
| 132 | + ): Promise<LanguageClient> { |
| 133 | + // Start python-lsp-server as a child process using stdio |
| 134 | + const pythonPath = interpreter.uri.fsPath; |
| 135 | + |
| 136 | + logger.trace(`Creating Python LSP client using interpreter: ${pythonPath}`); |
| 137 | + |
| 138 | + // Define the server executable |
| 139 | + const serverOptions: Executable = { |
| 140 | + command: pythonPath, |
| 141 | + args: ['-m', 'pylsp'], // Start python-lsp-server |
| 142 | + options: { |
| 143 | + env: { ...process.env } |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + const clientOptions: LanguageClientOptions = { |
| 148 | + // Document selector for Python cells in Deepnote notebooks |
| 149 | + documentSelector: [ |
| 150 | + { |
| 151 | + scheme: 'vscode-notebook-cell', |
| 152 | + language: 'python', |
| 153 | + pattern: '**/*.deepnote' |
| 154 | + }, |
| 155 | + { |
| 156 | + scheme: 'file', |
| 157 | + language: 'python', |
| 158 | + pattern: '**/*.deepnote' |
| 159 | + } |
| 160 | + ], |
| 161 | + // Synchronization settings |
| 162 | + synchronize: { |
| 163 | + // Notify the server about file changes to '.py' files in the workspace |
| 164 | + fileEvents: vscode.workspace.createFileSystemWatcher('**/*.py') |
| 165 | + }, |
| 166 | + // Output channel for diagnostics |
| 167 | + outputChannelName: 'Deepnote Python LSP' |
| 168 | + }; |
| 169 | + |
| 170 | + // Create the language client with stdio connection |
| 171 | + const client = new LanguageClient( |
| 172 | + 'deepnote-python-lsp', |
| 173 | + 'Deepnote Python Language Server', |
| 174 | + serverOptions, |
| 175 | + clientOptions |
| 176 | + ); |
| 177 | + |
| 178 | + // Start the client |
| 179 | + await client.start(); |
| 180 | + logger.info(`Python LSP client started for ${notebookUri.toString()}`); |
| 181 | + |
| 182 | + return client; |
| 183 | + } |
| 184 | + |
| 185 | + // TODO: Implement SQL LSP client when endpoint information is available |
| 186 | + // private async createSqlLspClient(serverInfo: DeepnoteServerInfo, notebookUri: vscode.Uri): Promise<LanguageClient> { |
| 187 | + // // Similar to Python client but for SQL |
| 188 | + // } |
| 189 | +} |
0 commit comments