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