summaryrefslogtreecommitdiff
path: root/internal/lsp
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-08-16 16:26:12 +0300
committerPaul Buetow <paul@buetow.org>2025-08-16 16:26:12 +0300
commit833bb66706dd991ecd3973da360c472d818e970a (patch)
tree62d8465b305883af1002063eb54ef38a08de299d /internal/lsp
parent148cda5f7ed4513528e3a46164b990708eeb1bc6 (diff)
logging: migrate LSP logs to global singleton (internal/logging); use consistent colors/prefix; refactor LLM provider to use global logger and remove per-client loggers
Diffstat (limited to 'internal/lsp')
-rw-r--r--internal/lsp/context.go9
-rw-r--r--internal/lsp/handlers.go29
-rw-r--r--internal/lsp/server.go25
-rw-r--r--internal/lsp/transport.go37
4 files changed, 50 insertions, 50 deletions
diff --git a/internal/lsp/context.go b/internal/lsp/context.go
index 8b7ed67..8f345df 100644
--- a/internal/lsp/context.go
+++ b/internal/lsp/context.go
@@ -2,6 +2,7 @@ package lsp
import (
"strings"
+ "hexai/internal/logging"
)
// buildAdditionalContext builds extra context messages based on the configured mode.
@@ -33,9 +34,7 @@ func (s *Server) buildAdditionalContext(newFunc bool, uri string, pos Position)
func (s *Server) windowContext(uri string, pos Position) string {
d := s.getDocument(uri)
if d == nil || len(d.lines) == 0 {
- if s.logger != nil {
- s.logger.Printf("context: window requested but document not open; skipping uri=%s", uri)
- }
+ logging.Logf("lsp ", "context: window requested but document not open; skipping uri=%s", uri)
return ""
}
n := len(d.lines)
@@ -55,9 +54,7 @@ func (s *Server) windowContext(uri string, pos Position) string {
func (s *Server) fullFileContext(uri string) string {
d := s.getDocument(uri)
if d == nil {
- if s.logger != nil {
- s.logger.Printf("context: full-file requested but document not open; skipping uri=%s", uri)
- }
+ logging.Logf("lsp ", "context: full-file requested but document not open; skipping uri=%s", uri)
return ""
}
return truncateToApproxTokens(d.text, s.maxContextTokens)
diff --git a/internal/lsp/handlers.go b/internal/lsp/handlers.go
index 8a782c4..1e77141 100644
--- a/internal/lsp/handlers.go
+++ b/internal/lsp/handlers.go
@@ -1,14 +1,15 @@
package lsp
import (
- "context"
- "encoding/json"
- "fmt"
- "hexai/internal"
- "hexai/internal/llm"
- "os"
- "strings"
- "time"
+ "context"
+ "encoding/json"
+ "fmt"
+ "hexai/internal"
+ "hexai/internal/llm"
+ "hexai/internal/logging"
+ "os"
+ "strings"
+ "time"
)
func (s *Server) handle(req Request) {
@@ -52,7 +53,7 @@ func (s *Server) handleInitialize(req Request) {
}
func (s *Server) handleInitialized() {
- s.logger.Println("client initialized")
+ logging.Logf("lsp ", "client initialized")
}
func (s *Server) handleShutdown(req Request) {
@@ -126,7 +127,7 @@ func (s *Server) buildDocString(p CompletionParams, above, current, below, funcC
}
func (s *Server) logCompletionContext(p CompletionParams, above, current, below, funcCtx string) {
- s.logger.Printf("completion ctx uri=%s line=%d char=%d above=%q current=%q below=%q function=%q",
+ logging.Logf("lsp ", "completion ctx uri=%s line=%d char=%d above=%q current=%q below=%q function=%q",
p.TextDocument.URI, p.Position.Line, p.Position.Character, trimLen(above), trimLen(current), trimLen(below), trimLen(funcCtx))
}
@@ -145,10 +146,10 @@ func (s *Server) tryLLMCompletion(p CompletionParams, above, current, below, fun
}
text, err := s.llmClient.Chat(ctx, messages, llm.WithMaxTokens(s.maxTokens), llm.WithTemperature(0.2))
- if err != nil {
- s.logger.Printf("llm completion error: %v", err)
- return nil, false
- }
+ if err != nil {
+ logging.Logf("lsp ", "llm completion error: %v", err)
+ return nil, false
+ }
cleaned := strings.TrimSpace(text)
if cleaned == "" {
return nil, false
diff --git a/internal/lsp/server.go b/internal/lsp/server.go
index 865d033..65d0b95 100644
--- a/internal/lsp/server.go
+++ b/internal/lsp/server.go
@@ -1,13 +1,14 @@
package lsp
import (
- "bufio"
- "encoding/json"
- "hexai/internal/llm"
- "io"
- "log"
- "sync"
- "time"
+ "bufio"
+ "encoding/json"
+ "hexai/internal/llm"
+ "hexai/internal/logging"
+ "io"
+ "log"
+ "sync"
+ "time"
)
// Server implements a minimal LSP over stdio.
@@ -47,8 +48,8 @@ func NewServer(r io.Reader, w io.Writer, logger *log.Logger, logContext bool, ma
s.windowLines = windowLines
s.maxContextTokens = maxContextTokens
s.noDiskIO = noDiskIO
- if c, err := llm.NewDefault(logger); err != nil {
- s.logger.Printf("llm disabled: %v", err)
+ if c, err := llm.NewDefault(); err != nil {
+ logging.Logf("lsp ", "llm disabled: %v", err)
} else {
s.llmClient = c
}
@@ -66,9 +67,9 @@ func (s *Server) Run() error {
}
var req Request
if err := json.Unmarshal(body, &req); err != nil {
- s.logger.Printf("invalid JSON: %v", err)
- continue
- }
+ logging.Logf("lsp ", "invalid JSON: %v", err)
+ continue
+ }
if req.Method == "" {
// A response from client; ignore
continue
diff --git a/internal/lsp/transport.go b/internal/lsp/transport.go
index 671d69b..dfdb5fc 100644
--- a/internal/lsp/transport.go
+++ b/internal/lsp/transport.go
@@ -1,12 +1,13 @@
package lsp
import (
- "encoding/json"
- "fmt"
- "io"
- "net/textproto"
- "strconv"
- "strings"
+ "encoding/json"
+ "fmt"
+ "hexai/internal/logging"
+ "io"
+ "net/textproto"
+ "strconv"
+ "strings"
)
func (s *Server) readMessage() ([]byte, error) {
@@ -47,17 +48,17 @@ func (s *Server) readMessage() ([]byte, error) {
func (s *Server) writeMessage(v any) {
data, err := json.Marshal(v)
- if err != nil {
- s.logger.Printf("marshal error: %v", err)
- return
- }
+ if err != nil {
+ logging.Logf("lsp ", "marshal error: %v", err)
+ return
+ }
header := fmt.Sprintf("Content-Length: %d\r\n\r\n", len(data))
- if _, err := io.WriteString(s.out, header); err != nil {
- s.logger.Printf("write header error: %v", err)
- return
- }
- if _, err := s.out.Write(data); err != nil {
- s.logger.Printf("write body error: %v", err)
- return
- }
+ if _, err := io.WriteString(s.out, header); err != nil {
+ logging.Logf("lsp ", "write header error: %v", err)
+ return
+ }
+ if _, err := s.out.Write(data); err != nil {
+ logging.Logf("lsp ", "write body error: %v", err)
+ return
+ }
}