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/provider_native_success_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'internal/lsp/provider_native_success_test.go') diff --git a/internal/lsp/provider_native_success_test.go b/internal/lsp/provider_native_success_test.go index 5bfe434..9951372 100644 --- a/internal/lsp/provider_native_success_test.go +++ b/internal/lsp/provider_native_success_test.go @@ -26,7 +26,7 @@ func TestProviderNativeCompletion_Success(t *testing.T) { current := "fmt." p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: "file:///x.go"}, Position: Position{Line: 0, Character: len(current)}} plan := completionPlan{current: current, params: p, funcCtx: "func f(){}", docStr: "doc", cacheKey: "k"} - items, ok := s.tryProviderNativeCompletion(context.Background(), plan, spec, s.llmClient, "0000") + items, ok := s.completion.tryProviderNativeCompletion(context.Background(), plan, spec, s.llmClient, "0000") if !ok || len(items) == 0 { t.Fatalf("expected provider-native items") } @@ -53,7 +53,7 @@ func TestProviderNativeCompletion_IndentWithDoubleOpen(t *testing.T) { current := " >>!do>" // leading indent + double-open marker p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: "file:///x.go"}, Position: Position{Line: 0, Character: len(current)}} plan := completionPlan{current: current, params: p, funcCtx: "func f(){}", docStr: "doc", cacheKey: "k"} - items, ok := s.tryProviderNativeCompletion(context.Background(), plan, spec, s.llmClient, "0000") + items, ok := s.completion.tryProviderNativeCompletion(context.Background(), plan, spec, s.llmClient, "0000") if !ok || len(items) == 0 { t.Fatalf("expected provider-native items") } @@ -91,7 +91,7 @@ func TestProviderNativeCompletion_UsesPromptTemplate(t *testing.T) { // Cursor at line 1, char 1 -> before should be "AAA\nB" p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: uri}, Position: Position{Line: 1, Character: 1}} plan := completionPlan{current: current, params: p, funcCtx: "func f(){}", docStr: "doc", cacheKey: "k"} - if _, ok := s.tryProviderNativeCompletion(context.Background(), plan, spec, s.llmClient, "0000"); !ok { + if _, ok := s.completion.tryProviderNativeCompletion(context.Background(), plan, spec, s.llmClient, "0000"); !ok { t.Fatalf("expected provider-native path") } if cap.lastPrompt == "" { -- cgit v1.2.3