| package app |
|
|
| import ( |
| "context" |
| "log/slog" |
| "time" |
|
|
| "github.com/charmbracelet/crush/internal/config" |
| "github.com/charmbracelet/crush/internal/log" |
| "github.com/charmbracelet/crush/internal/lsp" |
| "github.com/charmbracelet/crush/internal/lsp/watcher" |
| ) |
|
|
| |
| func (app *App) initLSPClients(ctx context.Context) { |
| for name, clientConfig := range app.config.LSP { |
| go app.createAndStartLSPClient(ctx, name, clientConfig) |
| } |
| slog.Info("LSP clients initialization started in background") |
| } |
|
|
| |
| func (app *App) createAndStartLSPClient(ctx context.Context, name string, config config.LSPConfig) { |
| slog.Info("Creating LSP client", "name", name, "command", config.Command, "fileTypes", config.FileTypes, "args", config.Args) |
|
|
| |
| updateLSPState(name, lsp.StateStarting, nil, nil, 0) |
|
|
| |
| lspClient, err := lsp.NewClient(ctx, name, config) |
| if err != nil { |
| slog.Error("Failed to create LSP client for", name, err) |
| updateLSPState(name, lsp.StateError, err, nil, 0) |
| return |
| } |
|
|
| |
| lspClient.SetDiagnosticsCallback(updateLSPDiagnostics) |
|
|
| |
| initCtx, cancel := context.WithTimeout(ctx, 30*time.Second) |
| defer cancel() |
|
|
| |
| _, err = lspClient.InitializeLSPClient(initCtx, app.config.WorkingDir()) |
| if err != nil { |
| slog.Error("Initialize failed", "name", name, "error", err) |
| updateLSPState(name, lsp.StateError, err, lspClient, 0) |
| lspClient.Close() |
| return |
| } |
|
|
| |
| if err := lspClient.WaitForServerReady(initCtx); err != nil { |
| slog.Error("Server failed to become ready", "name", name, "error", err) |
| |
| |
| lspClient.SetServerState(lsp.StateError) |
| updateLSPState(name, lsp.StateError, err, lspClient, 0) |
| } else { |
| |
| slog.Info("LSP server is ready", "name", name) |
| lspClient.SetServerState(lsp.StateReady) |
| updateLSPState(name, lsp.StateReady, nil, lspClient, 0) |
| } |
|
|
| slog.Info("LSP client initialized", "name", name) |
|
|
| |
| |
| watchCtx, cancelFunc := context.WithCancel(ctx) |
|
|
| |
| workspaceWatcher := watcher.NewWorkspaceWatcher(name, lspClient) |
|
|
| |
| app.watcherCancelFuncs.Append(cancelFunc) |
|
|
| |
| app.clientsMutex.Lock() |
| app.LSPClients[name] = lspClient |
| app.clientsMutex.Unlock() |
|
|
| |
| app.lspWatcherWG.Add(1) |
| go app.runWorkspaceWatcher(watchCtx, name, workspaceWatcher) |
| } |
|
|
| |
| func (app *App) runWorkspaceWatcher(ctx context.Context, name string, workspaceWatcher *watcher.WorkspaceWatcher) { |
| defer app.lspWatcherWG.Done() |
| defer log.RecoverPanic("LSP-"+name, func() { |
| |
| app.restartLSPClient(ctx, name) |
| }) |
|
|
| workspaceWatcher.WatchWorkspace(ctx, app.config.WorkingDir()) |
| slog.Info("Workspace watcher stopped", "client", name) |
| } |
|
|
| |
| func (app *App) restartLSPClient(ctx context.Context, name string) { |
| |
| clientConfig, exists := app.config.LSP[name] |
| if !exists { |
| slog.Error("Cannot restart client, configuration not found", "client", name) |
| return |
| } |
|
|
| |
| app.clientsMutex.Lock() |
| oldClient, exists := app.LSPClients[name] |
| if exists { |
| |
| delete(app.LSPClients, name) |
| } |
| app.clientsMutex.Unlock() |
|
|
| if exists && oldClient != nil { |
| |
| shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| _ = oldClient.Shutdown(shutdownCtx) |
| cancel() |
| } |
|
|
| |
| app.createAndStartLSPClient(ctx, name, clientConfig) |
| slog.Info("Successfully restarted LSP client", "client", name) |
| } |
|
|