summaryrefslogtreecommitdiff
path: root/internal/lsp/completion_service.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-06-11 00:16:27 +0300
committerPaul Buetow <paul@buetow.org>2026-06-11 00:16:27 +0300
commit236f6543b093e54157f40ec0c021b35177e1713e (patch)
treec6214c549739c997c8b572bb8904159c85fd2140 /internal/lsp/completion_service.go
parent247b79114d83e13cdfb2136333a949ff4dbe385b (diff)
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 <noreply@anthropic.com>
Diffstat (limited to 'internal/lsp/completion_service.go')
-rw-r--r--internal/lsp/completion_service.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/internal/lsp/completion_service.go b/internal/lsp/completion_service.go
new file mode 100644
index 0000000..5bfa131
--- /dev/null
+++ b/internal/lsp/completion_service.go
@@ -0,0 +1,76 @@
+package lsp
+
+import (
+ "context"
+)
+
+// completionService owns the entire code-completion subsystem that used to be
+// scattered across Server. It bundles the completion cache/throttle state
+// (completionState) with the request-handling logic, and reaches back into the
+// Server (via srv) for shared infrastructure such as configuration, LLM
+// clients, document access and stats counters.
+//
+// Splitting this out of Server keeps the completion logic cohesive and lets the
+// completionState mutex stay private to this subsystem, independent of
+// Server.mu (the two locks are never held simultaneously).
+type completionService struct {
+ srv *Server
+ completionState
+}
+
+// newCompletionService constructs the completion subsystem bound to srv.
+func newCompletionService(srv *Server) *completionService {
+ return &completionService{
+ srv: srv,
+ completionState: newCompletionState(),
+ }
+}
+
+// waitForThrottle gates completion (and chat) LLM calls using the configured
+// throttle interval. It lives here because the throttle clock is part of the
+// completion state.
+func (cs *completionService) waitForThrottle(ctx context.Context) bool {
+ return cs.completionState.waitForThrottle(ctx, cs.srv.completionThrottle())
+}
+
+// completionSvc returns the completion subsystem, lazily constructing it for
+// the bare Server literals used in some tests. Production code always has it
+// wired up by NewServer.
+func (s *Server) completionSvc() *completionService {
+ if s.completion == nil {
+ s.completion = newCompletionService(s)
+ }
+ return s.completion
+}
+
+// --- Server delegation shims ---------------------------------------------
+// These keep existing Server call sites (and tests) working while the real
+// state and behavior live on completionService.
+
+func (s *Server) storePendingCompletion(key string, items []CompletionItem) {
+ s.completionSvc().storePendingCompletion(key, items)
+}
+
+func (s *Server) setCompletionsDisabled(disabled bool) bool {
+ return s.completionSvc().setCompletionsDisabled(disabled)
+}
+
+func (s *Server) completionDisabled() bool {
+ return s.completionSvc().completionDisabled()
+}
+
+func (s *Server) takePendingCompletion(key string) []CompletionItem {
+ return s.completionSvc().takePendingCompletion(key)
+}
+
+func (s *Server) completionCacheGet(key string) (string, bool) {
+ return s.completionSvc().cacheGet(key)
+}
+
+func (s *Server) completionCachePut(key, value string) {
+ s.completionSvc().cachePut(key, value)
+}
+
+func (s *Server) waitForThrottle(ctx context.Context) bool {
+ return s.completionSvc().waitForThrottle(ctx)
+}