From 1a14333548ca42a3d822f0036d8dd6a9d19e262d Mon Sep 17 00:00:00 2001 From: Kujtim Hoxha Date: Mon, 19 Jan 2026 11:43:32 +0100 Subject: [PATCH] chore: pass context to lsp state --- internal/app/lsp.go | 12 ++++++------ internal/app/lsp_events.go | 8 ++++---- internal/lsp/client.go | 8 ++++---- internal/lsp/handlers.go | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/internal/app/lsp.go b/internal/app/lsp.go index 23a5447af92872223f91d3283cf6663aae0d1d07..05baf5b57e44a91e6f9cb36ae9bb356c666b4b97 100644 --- a/internal/app/lsp.go +++ b/internal/app/lsp.go @@ -28,18 +28,18 @@ func (app *App) createAndStartLSPClient(ctx context.Context, name string, config // Check if any root markers exist in the working directory (config now has defaults) if !lsp.HasRootMarkers(app.config.WorkingDir(), config.RootMarkers) { slog.Debug("Skipping LSP client: no root markers found", "name", name, "rootMarkers", config.RootMarkers) - updateLSPState(name, lsp.StateDisabled, nil, nil, 0) + updateLSPState(ctx, name, lsp.StateDisabled, nil, nil, 0) return } // Update state to starting - updateLSPState(name, lsp.StateStarting, nil, nil, 0) + updateLSPState(ctx, name, lsp.StateStarting, nil, nil, 0) // Create LSP client. lspClient, err := lsp.New(ctx, name, config, app.config.Resolver()) if err != nil { slog.Error("Failed to create LSP client for", "name", name, "error", err) - updateLSPState(name, lsp.StateError, err, nil, 0) + updateLSPState(ctx, name, lsp.StateError, err, nil, 0) return } @@ -54,7 +54,7 @@ func (app *App) createAndStartLSPClient(ctx context.Context, name string, config _, err = lspClient.Initialize(initCtx, app.config.WorkingDir()) if err != nil { slog.Error("LSP client initialization failed", "name", name, "error", err) - updateLSPState(name, lsp.StateError, err, lspClient, 0) + updateLSPState(ctx, name, lsp.StateError, err, lspClient, 0) lspClient.Close(ctx) return } @@ -65,12 +65,12 @@ func (app *App) createAndStartLSPClient(ctx context.Context, name string, config // Server never reached a ready state, but let's continue anyway, as // some functionality might still work. lspClient.SetServerState(lsp.StateError) - updateLSPState(name, lsp.StateError, err, lspClient, 0) + updateLSPState(ctx, name, lsp.StateError, err, lspClient, 0) } else { // Server reached a ready state successfully. slog.Debug("LSP server is ready", "name", name) lspClient.SetServerState(lsp.StateReady) - updateLSPState(name, lsp.StateReady, nil, lspClient, 0) + updateLSPState(ctx, name, lsp.StateReady, nil, lspClient, 0) } slog.Info("LSP client initialized", "name", name) diff --git a/internal/app/lsp_events.go b/internal/app/lsp_events.go index 971c7a27766ce314ff8628293bd2d8a622f01fd4..d06d6fac3270f2483ef96b0147492938b30eb7b3 100644 --- a/internal/app/lsp_events.go +++ b/internal/app/lsp_events.go @@ -57,7 +57,7 @@ func GetLSPState(name string) (LSPClientInfo, bool) { } // updateLSPState updates the state of an LSP client and publishes an event -func updateLSPState(name string, state lsp.ServerState, err error, client *lsp.Client, diagnosticCount int) { +func updateLSPState(ctx context.Context, name string, state lsp.ServerState, err error, client *lsp.Client, diagnosticCount int) { info := LSPClientInfo{ Name: name, State: state, @@ -71,7 +71,7 @@ func updateLSPState(name string, state lsp.ServerState, err error, client *lsp.C lspStates.Set(name, info) // Publish state change event - lspBroker.Publish(context.Background(), pubsub.UpdatedEvent, LSPEvent{ + lspBroker.Publish(ctx, pubsub.UpdatedEvent, LSPEvent{ Type: LSPEventStateChanged, Name: name, State: state, @@ -81,13 +81,13 @@ func updateLSPState(name string, state lsp.ServerState, err error, client *lsp.C } // updateLSPDiagnostics updates the diagnostic count for an LSP client and publishes an event -func updateLSPDiagnostics(name string, diagnosticCount int) { +func updateLSPDiagnostics(ctx context.Context, name string, diagnosticCount int) { if info, exists := lspStates.Get(name); exists { info.DiagnosticCount = diagnosticCount lspStates.Set(name, info) // Publish diagnostics change event - lspBroker.Publish(context.Background(), pubsub.UpdatedEvent, LSPEvent{ + lspBroker.Publish(ctx, pubsub.UpdatedEvent, LSPEvent{ Type: LSPEventDiagnosticsChanged, Name: name, State: info.State, diff --git a/internal/lsp/client.go b/internal/lsp/client.go index 79220cc1f315fec30a1bee2aa0dcd106bc311a02..ba74a4c158c33487579819d19eabb34c5cf23f5b 100644 --- a/internal/lsp/client.go +++ b/internal/lsp/client.go @@ -41,7 +41,7 @@ type Client struct { config config.LSPConfig // Diagnostic change callback - onDiagnosticsChanged func(name string, count int) + onDiagnosticsChanged func(ctx context.Context, name string, count int) // Diagnostic cache diagnostics *csync.VersionedMap[protocol.DocumentURI, []protocol.Diagnostic] @@ -144,8 +144,8 @@ func (c *Client) Initialize(ctx context.Context, workspaceDir string) (*protocol c.RegisterServerRequestHandler("workspace/configuration", HandleWorkspaceConfiguration) c.RegisterServerRequestHandler("client/registerCapability", HandleRegisterCapability) c.RegisterNotificationHandler("window/showMessage", HandleServerMessage) - c.RegisterNotificationHandler("textDocument/publishDiagnostics", func(_ context.Context, _ string, params json.RawMessage) { - HandleDiagnostics(c, params) + c.RegisterNotificationHandler("textDocument/publishDiagnostics", func(ctx context.Context, _ string, params json.RawMessage) { + HandleDiagnostics(ctx, c, params) }) return result, nil @@ -192,7 +192,7 @@ func (c *Client) GetName() string { } // SetDiagnosticsCallback sets the callback function for diagnostic changes -func (c *Client) SetDiagnosticsCallback(callback func(name string, count int)) { +func (c *Client) SetDiagnosticsCallback(callback func(ctx context.Context, name string, count int)) { c.onDiagnosticsChanged = callback } diff --git a/internal/lsp/handlers.go b/internal/lsp/handlers.go index b386e0780f6f6db6db13be380496c60a6e3c457e..9a226a531b78583115d1af7592f54aca043d9175 100644 --- a/internal/lsp/handlers.go +++ b/internal/lsp/handlers.go @@ -104,7 +104,7 @@ func HandleServerMessage(_ context.Context, method string, params json.RawMessag } // HandleDiagnostics handles diagnostic notifications from the LSP server -func HandleDiagnostics(client *Client, params json.RawMessage) { +func HandleDiagnostics(ctx context.Context, client *Client, params json.RawMessage) { var diagParams protocol.PublishDiagnosticsParams if err := json.Unmarshal(params, &diagParams); err != nil { slog.Error("Error unmarshaling diagnostics params", "error", err) @@ -121,6 +121,6 @@ func HandleDiagnostics(client *Client, params json.RawMessage) { // Trigger callback if set if client.onDiagnosticsChanged != nil { - client.onDiagnosticsChanged(client.name, totalCount) + client.onDiagnosticsChanged(ctx, client.name, totalCount) } }