1package lsp
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "log/slog"
8 "maps"
9 "os"
10 "path/filepath"
11 "strings"
12 "sync"
13 "sync/atomic"
14 "time"
15
16 "github.com/bmatcuk/doublestar/v4"
17 "github.com/charmbracelet/crush/internal/config"
18 "github.com/charmbracelet/crush/internal/csync"
19 "github.com/charmbracelet/crush/internal/fsext"
20 "github.com/charmbracelet/crush/internal/home"
21 powernapconfig "github.com/charmbracelet/x/powernap/pkg/config"
22 powernap "github.com/charmbracelet/x/powernap/pkg/lsp"
23 "github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
24 "github.com/charmbracelet/x/powernap/pkg/transport"
25)
26
27// DiagnosticCounts holds the count of diagnostics by severity.
28type DiagnosticCounts struct {
29 Error int
30 Warning int
31 Information int
32 Hint int
33}
34
35type Client struct {
36 client *powernap.Client
37 name string
38 debug bool
39
40 // Working directory this LSP is scoped to.
41 workDir string
42
43 // File types this LSP server handles (e.g., .go, .rs, .py)
44 fileTypes []string
45
46 // Configuration for this LSP client
47 config config.LSPConfig
48
49 // Original context and resolver for recreating the client
50 ctx context.Context
51 resolver config.VariableResolver
52
53 // Diagnostic change callback
54 onDiagnosticsChanged func(name string, count int)
55
56 // Diagnostic cache
57 diagnostics *csync.VersionedMap[protocol.DocumentURI, []protocol.Diagnostic]
58
59 // Cached diagnostic counts to avoid map copy on every UI render.
60 diagCountsCache DiagnosticCounts
61 diagCountsVersion uint64
62 diagCountsMu sync.Mutex
63
64 // Files are currently opened by the LSP
65 openFiles *csync.Map[string, *OpenFileInfo]
66
67 // Server state
68 serverState atomic.Value
69}
70
71// New creates a new LSP client using the powernap implementation.
72func New(ctx context.Context, name string, cfg config.LSPConfig, resolver config.VariableResolver, debug bool) (*Client, error) {
73 client := &Client{
74 name: name,
75 fileTypes: cfg.FileTypes,
76 diagnostics: csync.NewVersionedMap[protocol.DocumentURI, []protocol.Diagnostic](),
77 openFiles: csync.NewMap[string, *OpenFileInfo](),
78 config: cfg,
79 ctx: ctx,
80 debug: debug,
81 resolver: resolver,
82 }
83 client.serverState.Store(StateStarting)
84
85 if err := client.createPowernapClient(); err != nil {
86 return nil, err
87 }
88
89 return client, nil
90}
91
92// Initialize initializes the LSP client and returns the server capabilities.
93func (c *Client) Initialize(ctx context.Context, workspaceDir string) (*protocol.InitializeResult, error) {
94 if err := c.client.Initialize(ctx, false); err != nil {
95 return nil, fmt.Errorf("failed to initialize the lsp client: %w", err)
96 }
97
98 // Convert powernap capabilities to protocol capabilities
99 caps := c.client.GetCapabilities()
100 protocolCaps := protocol.ServerCapabilities{
101 TextDocumentSync: caps.TextDocumentSync,
102 CompletionProvider: func() *protocol.CompletionOptions {
103 if caps.CompletionProvider != nil {
104 return &protocol.CompletionOptions{
105 TriggerCharacters: caps.CompletionProvider.TriggerCharacters,
106 AllCommitCharacters: caps.CompletionProvider.AllCommitCharacters,
107 ResolveProvider: caps.CompletionProvider.ResolveProvider,
108 }
109 }
110 return nil
111 }(),
112 }
113
114 result := &protocol.InitializeResult{
115 Capabilities: protocolCaps,
116 }
117
118 c.registerHandlers()
119
120 return result, nil
121}
122
123// Close closes the LSP client.
124func (c *Client) Close(ctx context.Context) error {
125 c.CloseAllFiles(ctx)
126
127 // Shutdown and exit the client
128 if err := c.client.Shutdown(ctx); err != nil {
129 slog.Warn("Failed to shutdown LSP client", "error", err)
130 }
131
132 return c.client.Exit()
133}
134
135// createPowernapClient creates a new powernap client with the current configuration.
136func (c *Client) createPowernapClient() error {
137 workDir, err := os.Getwd()
138 if err != nil {
139 return fmt.Errorf("failed to get working directory: %w", err)
140 }
141
142 rootURI := string(protocol.URIFromPath(workDir))
143 c.workDir = workDir
144
145 command, err := c.resolver.ResolveValue(c.config.Command)
146 if err != nil {
147 return fmt.Errorf("invalid lsp command: %w", err)
148 }
149
150 clientConfig := powernap.ClientConfig{
151 Command: home.Long(command),
152 Args: c.config.Args,
153 RootURI: rootURI,
154 Environment: maps.Clone(c.config.Env),
155 Settings: c.config.Options,
156 InitOptions: c.config.InitOptions,
157 WorkspaceFolders: []protocol.WorkspaceFolder{
158 {
159 URI: rootURI,
160 Name: filepath.Base(workDir),
161 },
162 },
163 }
164
165 powernapClient, err := powernap.NewClient(clientConfig)
166 if err != nil {
167 return fmt.Errorf("failed to create lsp client: %w", err)
168 }
169
170 c.client = powernapClient
171 return nil
172}
173
174// registerHandlers registers the standard LSP notification and request handlers.
175func (c *Client) registerHandlers() {
176 c.RegisterServerRequestHandler("workspace/applyEdit", HandleApplyEdit)
177 c.RegisterServerRequestHandler("workspace/configuration", HandleWorkspaceConfiguration)
178 c.RegisterServerRequestHandler("client/registerCapability", HandleRegisterCapability)
179 c.RegisterNotificationHandler("window/showMessage", func(ctx context.Context, method string, params json.RawMessage) {
180 if c.debug {
181 HandleServerMessage(ctx, method, params)
182 }
183 })
184 c.RegisterNotificationHandler("textDocument/publishDiagnostics", func(_ context.Context, _ string, params json.RawMessage) {
185 HandleDiagnostics(c, params)
186 })
187}
188
189// Restart closes the current LSP client and creates a new one with the same configuration.
190func (c *Client) Restart() error {
191 var openFiles []string
192 for uri := range c.openFiles.Seq2() {
193 openFiles = append(openFiles, string(uri))
194 }
195
196 closeCtx, cancel := context.WithTimeout(c.ctx, 10*time.Second)
197 defer cancel()
198
199 if err := c.Close(closeCtx); err != nil {
200 slog.Warn("Error closing client during restart", "name", c.name, "error", err)
201 }
202
203 c.diagCountsCache = DiagnosticCounts{}
204 c.diagCountsVersion = 0
205
206 if err := c.createPowernapClient(); err != nil {
207 return err
208 }
209
210 initCtx, cancel := context.WithTimeout(c.ctx, 30*time.Second)
211 defer cancel()
212
213 c.SetServerState(StateStarting)
214
215 if err := c.client.Initialize(initCtx, false); err != nil {
216 c.SetServerState(StateError)
217 return fmt.Errorf("failed to initialize lsp client: %w", err)
218 }
219
220 c.registerHandlers()
221
222 if err := c.WaitForServerReady(initCtx); err != nil {
223 slog.Error("Server failed to become ready after restart", "name", c.name, "error", err)
224 c.SetServerState(StateError)
225 return err
226 }
227
228 for _, uri := range openFiles {
229 if err := c.OpenFile(initCtx, uri); err != nil {
230 slog.Warn("Failed to reopen file after restart", "file", uri, "error", err)
231 }
232 }
233 return nil
234}
235
236// ServerState represents the state of an LSP server
237type ServerState int
238
239const (
240 StateStarting ServerState = iota
241 StateReady
242 StateError
243 StateDisabled
244)
245
246// GetServerState returns the current state of the LSP server
247func (c *Client) GetServerState() ServerState {
248 if val := c.serverState.Load(); val != nil {
249 return val.(ServerState)
250 }
251 return StateStarting
252}
253
254// SetServerState sets the current state of the LSP server
255func (c *Client) SetServerState(state ServerState) {
256 c.serverState.Store(state)
257}
258
259// GetName returns the name of the LSP client
260func (c *Client) GetName() string {
261 return c.name
262}
263
264// SetDiagnosticsCallback sets the callback function for diagnostic changes
265func (c *Client) SetDiagnosticsCallback(callback func(name string, count int)) {
266 c.onDiagnosticsChanged = callback
267}
268
269// WaitForServerReady waits for the server to be ready
270func (c *Client) WaitForServerReady(ctx context.Context) error {
271 // Set initial state
272 c.SetServerState(StateStarting)
273
274 // Create a context with timeout
275 ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
276 defer cancel()
277
278 // Try to ping the server with a simple request
279 ticker := time.NewTicker(500 * time.Millisecond)
280 defer ticker.Stop()
281
282 if c.debug {
283 slog.Debug("Waiting for LSP server to be ready...")
284 }
285
286 c.openKeyConfigFiles(ctx)
287
288 for {
289 select {
290 case <-ctx.Done():
291 c.SetServerState(StateError)
292 return fmt.Errorf("timeout waiting for LSP server to be ready")
293 case <-ticker.C:
294 // Check if client is running
295 if !c.client.IsRunning() {
296 if c.debug {
297 slog.Debug("LSP server not ready yet", "server", c.name)
298 }
299 continue
300 }
301
302 // Server is ready
303 c.SetServerState(StateReady)
304 if c.debug {
305 slog.Debug("LSP server is ready")
306 }
307 return nil
308 }
309 }
310}
311
312// OpenFileInfo contains information about an open file
313type OpenFileInfo struct {
314 Version int32
315 URI protocol.DocumentURI
316}
317
318// HandlesFile checks if this LSP client handles the given file based on its
319// extension and whether it's within the working directory.
320func (c *Client) HandlesFile(path string) bool {
321 // Check if file is within working directory.
322 absPath, err := filepath.Abs(path)
323 if err != nil {
324 slog.Debug("Cannot resolve path", "name", c.name, "file", path, "error", err)
325 return false
326 }
327 relPath, err := filepath.Rel(c.workDir, absPath)
328 if err != nil || strings.HasPrefix(relPath, "..") {
329 slog.Debug("File outside workspace", "name", c.name, "file", path, "workDir", c.workDir)
330 return false
331 }
332
333 // If no file types are specified, handle all files (backward compatibility).
334 if len(c.fileTypes) == 0 {
335 return true
336 }
337
338 kind := powernap.DetectLanguage(path)
339 name := strings.ToLower(filepath.Base(path))
340 for _, filetype := range c.fileTypes {
341 suffix := strings.ToLower(filetype)
342 if !strings.HasPrefix(suffix, ".") {
343 suffix = "." + suffix
344 }
345 if strings.HasSuffix(name, suffix) || filetype == string(kind) {
346 slog.Debug("Handles file", "name", c.name, "file", name, "filetype", filetype, "kind", kind)
347 return true
348 }
349 }
350 slog.Debug("Doesn't handle file", "name", c.name, "file", name)
351 return false
352}
353
354// OpenFile opens a file in the LSP server.
355func (c *Client) OpenFile(ctx context.Context, filepath string) error {
356 if !c.HandlesFile(filepath) {
357 return nil
358 }
359
360 uri := string(protocol.URIFromPath(filepath))
361
362 if _, exists := c.openFiles.Get(uri); exists {
363 return nil // Already open
364 }
365
366 // Skip files that do not exist or cannot be read
367 content, err := os.ReadFile(filepath)
368 if err != nil {
369 return fmt.Errorf("error reading file: %w", err)
370 }
371
372 // Notify the server about the opened document
373 if err = c.client.NotifyDidOpenTextDocument(ctx, uri, string(powernap.DetectLanguage(filepath)), 1, string(content)); err != nil {
374 return err
375 }
376
377 c.openFiles.Set(uri, &OpenFileInfo{
378 Version: 1,
379 URI: protocol.DocumentURI(uri),
380 })
381
382 return nil
383}
384
385// NotifyChange notifies the server about a file change.
386func (c *Client) NotifyChange(ctx context.Context, filepath string) error {
387 uri := string(protocol.URIFromPath(filepath))
388
389 content, err := os.ReadFile(filepath)
390 if err != nil {
391 return fmt.Errorf("error reading file: %w", err)
392 }
393
394 fileInfo, isOpen := c.openFiles.Get(uri)
395 if !isOpen {
396 return fmt.Errorf("cannot notify change for unopened file: %s", filepath)
397 }
398
399 // Increment version
400 fileInfo.Version++
401
402 // Create change event
403 changes := []protocol.TextDocumentContentChangeEvent{
404 {
405 Value: protocol.TextDocumentContentChangeWholeDocument{
406 Text: string(content),
407 },
408 },
409 }
410
411 return c.client.NotifyDidChangeTextDocument(ctx, uri, int(fileInfo.Version), changes)
412}
413
414// IsFileOpen checks if a file is currently open.
415func (c *Client) IsFileOpen(filepath string) bool {
416 uri := string(protocol.URIFromPath(filepath))
417 _, exists := c.openFiles.Get(uri)
418 return exists
419}
420
421// CloseAllFiles closes all currently open files.
422func (c *Client) CloseAllFiles(ctx context.Context) {
423 for uri := range c.openFiles.Seq2() {
424 if c.debug {
425 slog.Debug("Closing file", "file", uri)
426 }
427 if err := c.client.NotifyDidCloseTextDocument(ctx, uri); err != nil {
428 slog.Warn("Error closing file", "uri", uri, "error", err)
429 continue
430 }
431 c.openFiles.Del(uri)
432 }
433}
434
435// GetFileDiagnostics returns diagnostics for a specific file.
436func (c *Client) GetFileDiagnostics(uri protocol.DocumentURI) []protocol.Diagnostic {
437 diags, _ := c.diagnostics.Get(uri)
438 return diags
439}
440
441// GetDiagnostics returns all diagnostics for all files.
442func (c *Client) GetDiagnostics() map[protocol.DocumentURI][]protocol.Diagnostic {
443 return c.diagnostics.Copy()
444}
445
446// GetDiagnosticCounts returns cached diagnostic counts by severity.
447// Uses the VersionedMap version to avoid recomputing on every call.
448func (c *Client) GetDiagnosticCounts() DiagnosticCounts {
449 currentVersion := c.diagnostics.Version()
450
451 c.diagCountsMu.Lock()
452 defer c.diagCountsMu.Unlock()
453
454 if currentVersion == c.diagCountsVersion {
455 return c.diagCountsCache
456 }
457
458 // Recompute counts.
459 counts := DiagnosticCounts{}
460 for _, diags := range c.diagnostics.Seq2() {
461 for _, diag := range diags {
462 switch diag.Severity {
463 case protocol.SeverityError:
464 counts.Error++
465 case protocol.SeverityWarning:
466 counts.Warning++
467 case protocol.SeverityInformation:
468 counts.Information++
469 case protocol.SeverityHint:
470 counts.Hint++
471 }
472 }
473 }
474
475 c.diagCountsCache = counts
476 c.diagCountsVersion = currentVersion
477 return counts
478}
479
480// OpenFileOnDemand opens a file only if it's not already open.
481func (c *Client) OpenFileOnDemand(ctx context.Context, filepath string) error {
482 // Check if the file is already open
483 if c.IsFileOpen(filepath) {
484 return nil
485 }
486
487 // Open the file
488 return c.OpenFile(ctx, filepath)
489}
490
491// GetDiagnosticsForFile ensures a file is open and returns its diagnostics.
492func (c *Client) GetDiagnosticsForFile(ctx context.Context, filepath string) ([]protocol.Diagnostic, error) {
493 documentURI := protocol.URIFromPath(filepath)
494
495 // Make sure the file is open
496 if !c.IsFileOpen(filepath) {
497 if err := c.OpenFile(ctx, filepath); err != nil {
498 return nil, fmt.Errorf("failed to open file for diagnostics: %w", err)
499 }
500
501 // Give the LSP server a moment to process the file
502 time.Sleep(100 * time.Millisecond)
503 }
504
505 // Get diagnostics
506 diagnostics, _ := c.diagnostics.Get(documentURI)
507
508 return diagnostics, nil
509}
510
511// ClearDiagnosticsForURI removes diagnostics for a specific URI from the cache.
512func (c *Client) ClearDiagnosticsForURI(uri protocol.DocumentURI) {
513 c.diagnostics.Del(uri)
514}
515
516// RegisterNotificationHandler registers a notification handler.
517func (c *Client) RegisterNotificationHandler(method string, handler transport.NotificationHandler) {
518 c.client.RegisterNotificationHandler(method, handler)
519}
520
521// RegisterServerRequestHandler handles server requests.
522func (c *Client) RegisterServerRequestHandler(method string, handler transport.Handler) {
523 c.client.RegisterHandler(method, handler)
524}
525
526// DidChangeWatchedFiles sends a workspace/didChangeWatchedFiles notification to the server.
527func (c *Client) DidChangeWatchedFiles(ctx context.Context, params protocol.DidChangeWatchedFilesParams) error {
528 return c.client.NotifyDidChangeWatchedFiles(ctx, params.Changes)
529}
530
531// openKeyConfigFiles opens important configuration files that help initialize the server.
532func (c *Client) openKeyConfigFiles(ctx context.Context) {
533 wd, err := os.Getwd()
534 if err != nil {
535 return
536 }
537
538 // Try to open each file, ignoring errors if they don't exist
539 for _, file := range c.config.RootMarkers {
540 file = filepath.Join(wd, file)
541 if _, err := os.Stat(file); err == nil {
542 // File exists, try to open it
543 if err := c.OpenFile(ctx, file); err != nil {
544 slog.Error("Failed to open key config file", "file", file, "error", err)
545 } else {
546 slog.Debug("Opened key config file for initialization", "file", file)
547 }
548 }
549 }
550}
551
552// WaitForDiagnostics waits until diagnostics change or the timeout is reached.
553func (c *Client) WaitForDiagnostics(ctx context.Context, d time.Duration) {
554 ticker := time.NewTicker(200 * time.Millisecond)
555 defer ticker.Stop()
556 timeout := time.After(d)
557 pv := c.diagnostics.Version()
558 for {
559 select {
560 case <-ctx.Done():
561 return
562 case <-timeout:
563 return
564 case <-ticker.C:
565 if pv != c.diagnostics.Version() {
566 return
567 }
568 }
569 }
570}
571
572// FindReferences finds all references to the symbol at the given position.
573func (c *Client) FindReferences(ctx context.Context, filepath string, line, character int, includeDeclaration bool) ([]protocol.Location, error) {
574 if err := c.OpenFileOnDemand(ctx, filepath); err != nil {
575 return nil, err
576 }
577 // NOTE: line and character should be 0-based.
578 // See: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position
579 return c.client.FindReferences(ctx, filepath, line-1, character-1, includeDeclaration)
580}
581
582// FilterMatching gets a list of configs and only returns the ones with
583// matching root markers.
584func FilterMatching(dir string, servers map[string]*powernapconfig.ServerConfig) map[string]*powernapconfig.ServerConfig {
585 result := map[string]*powernapconfig.ServerConfig{}
586 if len(servers) == 0 {
587 return result
588 }
589
590 type serverPatterns struct {
591 server *powernapconfig.ServerConfig
592 patterns []string
593 }
594 normalized := make(map[string]serverPatterns, len(servers))
595 for name, server := range servers {
596 var patterns []string
597 for _, p := range server.RootMarkers {
598 if p == ".git" {
599 // ignore .git for discovery
600 continue
601 }
602 patterns = append(patterns, filepath.ToSlash(p))
603 }
604 if len(patterns) == 0 {
605 slog.Debug("ignoring lsp with no root markers", "name", name)
606 continue
607 }
608 normalized[name] = serverPatterns{server: server, patterns: patterns}
609 }
610
611 walker := fsext.NewFastGlobWalker(dir)
612 _ = filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
613 if err != nil {
614 return nil
615 }
616
617 if walker.ShouldSkip(path) {
618 if d.IsDir() {
619 return filepath.SkipDir
620 }
621 return nil
622 }
623
624 relPath, err := filepath.Rel(dir, path)
625 if err != nil {
626 return nil
627 }
628 relPath = filepath.ToSlash(relPath)
629
630 for name, sp := range normalized {
631 for _, pattern := range sp.patterns {
632 matched, err := doublestar.Match(pattern, relPath)
633 if err != nil || !matched {
634 continue
635 }
636 result[name] = sp.server
637 delete(normalized, name)
638 break
639 }
640 }
641
642 if len(normalized) == 0 {
643 return filepath.SkipAll
644 }
645 return nil
646 })
647
648 return result
649}