From 236f6543b093e54157f40ec0c021b35177e1713e Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 11 Jun 2026 00:16:27 +0300 Subject: Refactor lsp.Server God Object: extract chat and completion subsystems The Server type accumulated two large, tangled feature subsystems (in-editor chat and code completion) alongside its core LSP dispatch role. Pull them into cohesive types that own their state and logic while delegating shared infrastructure back to Server via a back-reference. - Add completionService (completion_service.go): owns the completion cache/throttle state (completionState) and all completion request-handling logic (handleCompletion, plan/jobs/execute, provider-native path, post-processing, message building, prefix heuristics). Methods moved off Server in handlers_completion.go to completionService. - Add chatService (chat_service.go + chat_handlers.go): owns the input-activity clock (lastInput, with its own mutex instead of Server.mu) and all in-editor chat logic (detection, transcript history, message building, edit application, inline prompts) plus the slash-command handlers (chat_commands.go). - Server now holds chat/completion fields, wires them in NewServer, and delegates (dispatch table, didChange, debounce gate) to them. Thin Server shims preserve the existing completion-state API for callers/tests. Pure refactor: no behavior or LSP protocol changes. Tests adjusted only to reach the relocated methods/state via the services. All tests pass with -race; coverage 86.1%. Co-Authored-By: Claude Opus 4.8 --- internal/lsp/server.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'internal/lsp/server.go') diff --git a/internal/lsp/server.go b/internal/lsp/server.go index 5fae78a..e42179d 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -32,18 +32,24 @@ type Server struct { statusSink StatusSink exited atomic.Bool inflight sync.WaitGroup // tracks background goroutines (inline prompt, chat, etc.) - // mu protects docs, cfg, logContext, configLoadOpts, nextID, and chatSubsystem.lastInput. - // It is never held while completionState.stateMu is held, and vice versa, - // so there is no lock ordering concern between them. + // mu protects docs, cfg, logContext, configLoadOpts, and nextID. + // It is never held while completionState.stateMu (owned by the completion + // service) is held, and vice versa, so there is no lock ordering concern + // between them. mu sync.RWMutex docs map[string]*document logContext bool configStore *runtimeconfig.Store cfg appconfig.App codeActionSubsystem - chatSubsystem llmStatsSubsystem - completionSubsystem + // chat and completion own the two large feature subsystems that were + // previously inlined on Server (the "God Object"). Server now delegates + // in-editor chat and code-completion behavior to these cohesive services, + // each of which holds a back-reference to the Server for shared + // infrastructure (config, LLM clients, stats, document access). + chat *chatService + completion *completionService configLoadOpts appconfig.LoadOptions // Outgoing JSON-RPC id counter for server-initiated requests nextID int64 @@ -55,14 +61,6 @@ type Server struct { handlers map[string]func(Request) } -type completionSubsystem struct { - completionState -} - -type chatSubsystem struct { - lastInput time.Time -} - type codeActionSubsystem struct { llmClientRegistry } @@ -125,10 +123,12 @@ func NewServer(r io.Reader, w io.Writer, logger *log.Logger, opts ServerOptions) codeActionSubsystem: codeActionSubsystem{ llmClientRegistry: llmClientRegistry{}, }, - completionSubsystem: completionSubsystem{ - completionState: completionState{}, - }, } + // Wire up the chat and completion services with a back-reference to the + // server so they can reach shared infrastructure while owning their own + // feature-specific state and logic. + s.chat = newChatService(s) + s.completion = newCompletionService(s) s.startTime = time.Now() s.applyOptions(opts) // Initialize dispatch table @@ -140,7 +140,7 @@ func NewServer(r io.Reader, w io.Writer, logger *log.Logger, opts ServerOptions) "textDocument/didOpen": s.handleDidOpen, "textDocument/didChange": s.handleDidChange, "textDocument/didClose": s.handleDidClose, - "textDocument/completion": s.handleCompletion, + "textDocument/completion": s.completion.handleCompletion, "textDocument/codeAction": s.handleCodeAction, "codeAction/resolve": s.handleCodeActionResolve, "workspace/executeCommand": s.handleExecuteCommand, -- cgit v1.2.3