()
| 143 | * However, if initialization previously failed, calling again will retry. |
| 144 | */ |
| 145 | export function initializeLspServerManager(): void { |
| 146 | // --bare / SIMPLE: no LSP. LSP is for editor integration (diagnostics, |
| 147 | // hover, go-to-def in the REPL). Scripted -p calls have no use for it. |
| 148 | if (isBareMode()) { |
| 149 | return |
| 150 | } |
| 151 | logForDebugging('[LSP MANAGER] initializeLspServerManager() called') |
| 152 | |
| 153 | // Skip if already initialized or currently initializing |
| 154 | if (lspManagerInstance !== undefined && initializationState !== 'failed') { |
| 155 | logForDebugging( |
| 156 | '[LSP MANAGER] Already initialized or initializing, skipping', |
| 157 | ) |
| 158 | return |
| 159 | } |
| 160 | |
| 161 | // Reset state for retry if previous initialization failed |
| 162 | if (initializationState === 'failed') { |
| 163 | lspManagerInstance = undefined |
| 164 | initializationError = undefined |
| 165 | } |
| 166 | |
| 167 | // Create the manager instance and mark as pending |
| 168 | lspManagerInstance = createLSPServerManager() |
| 169 | initializationState = 'pending' |
| 170 | logForDebugging('[LSP MANAGER] Created manager instance, state=pending') |
| 171 | |
| 172 | // Increment generation to invalidate any pending initializations |
| 173 | const currentGeneration = ++initializationGeneration |
| 174 | logForDebugging( |
| 175 | `[LSP MANAGER] Starting async initialization (generation ${currentGeneration})`, |
| 176 | ) |
| 177 | |
| 178 | // Start initialization asynchronously without blocking |
| 179 | // Store the promise so callers can await it via waitForInitialization() |
| 180 | initializationPromise = lspManagerInstance |
| 181 | .initialize() |
| 182 | .then(() => { |
| 183 | // Only update state if this is still the current initialization |
| 184 | if (currentGeneration === initializationGeneration) { |
| 185 | initializationState = 'success' |
| 186 | logForDebugging('LSP server manager initialized successfully') |
| 187 | |
| 188 | // Register passive notification handlers for diagnostics |
| 189 | if (lspManagerInstance) { |
| 190 | registerLSPNotificationHandlers(lspManagerInstance) |
| 191 | } |
| 192 | } |
| 193 | }) |
| 194 | .catch((error: unknown) => { |
| 195 | // Only update state if this is still the current initialization |
| 196 | if (currentGeneration === initializationGeneration) { |
| 197 | initializationState = 'failed' |
| 198 | initializationError = error as Error |
| 199 | // Clear the instance since it's not usable |
| 200 | lspManagerInstance = undefined |
| 201 | |
| 202 | logError(error as Error) |
no test coverage detected