( serverName: string, onCrash?: (error: Error) => void, )
| 49 | * to propagate crash state so the server can be restarted on next use. |
| 50 | */ |
| 51 | export function createLSPClient( |
| 52 | serverName: string, |
| 53 | onCrash?: (error: Error) => void, |
| 54 | ): LSPClient { |
| 55 | // State variables in closure |
| 56 | let process: ChildProcess | undefined |
| 57 | let connection: MessageConnection | undefined |
| 58 | let capabilities: ServerCapabilities | undefined |
| 59 | let isInitialized = false |
| 60 | let startFailed = false |
| 61 | let startError: Error | undefined |
| 62 | let isStopping = false // Track intentional shutdown to avoid spurious error logging |
| 63 | // Queue handlers registered before connection ready (lazy initialization support) |
| 64 | const pendingHandlers: Array<{ |
| 65 | method: string |
| 66 | handler: (params: unknown) => void |
| 67 | }> = [] |
| 68 | const pendingRequestHandlers: Array<{ |
| 69 | method: string |
| 70 | handler: (params: unknown) => unknown | Promise<unknown> |
| 71 | }> = [] |
| 72 | |
| 73 | function checkStartFailed(): void { |
| 74 | if (startFailed) { |
| 75 | throw startError || new Error(`LSP server ${serverName} failed to start`) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return { |
| 80 | get capabilities(): ServerCapabilities | undefined { |
| 81 | return capabilities |
| 82 | }, |
| 83 | |
| 84 | get isInitialized(): boolean { |
| 85 | return isInitialized |
| 86 | }, |
| 87 | |
| 88 | async start( |
| 89 | command: string, |
| 90 | args: string[], |
| 91 | options?: { |
| 92 | env?: Record<string, string> |
| 93 | cwd?: string |
| 94 | }, |
| 95 | ): Promise<void> { |
| 96 | try { |
| 97 | // 1. Spawn LSP server process |
| 98 | process = spawn(command, args, { |
| 99 | stdio: ['pipe', 'pipe', 'pipe'], |
| 100 | env: { ...subprocessEnv(), ...options?.env }, |
| 101 | cwd: options?.cwd, |
| 102 | // Prevent visible console window on Windows (no-op on other platforms) |
| 103 | windowsHide: true, |
| 104 | }) |
| 105 | |
| 106 | if (!process.stdout || !process.stdin) { |
| 107 | throw new Error('LSP server process stdio not available') |
| 108 | } |
no outgoing calls
no test coverage detected