1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
|
package lsp
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"log/slog"
"github.com/sst/opencode/internal/config"
"github.com/sst/opencode/internal/logging"
"github.com/sst/opencode/internal/lsp/protocol"
"github.com/sst/opencode/internal/status"
)
type Client struct {
Cmd *exec.Cmd
stdin io.WriteCloser
stdout *bufio.Reader
stderr io.ReadCloser
// Request ID counter
nextID atomic.Int32
// Response handlers
handlers map[int32]chan *Message
handlersMu sync.RWMutex
// Server request handlers
serverRequestHandlers map[string]ServerRequestHandler
serverHandlersMu sync.RWMutex
// Notification handlers
notificationHandlers map[string]NotificationHandler
notificationMu sync.RWMutex
// Diagnostic cache
diagnostics map[protocol.DocumentUri][]protocol.Diagnostic
diagnosticsMu sync.RWMutex
// Files are currently opened by the LSP
openFiles map[string]*OpenFileInfo
openFilesMu sync.RWMutex
// Server state
serverState atomic.Value
}
func NewClient(ctx context.Context, command string, args ...string) (*Client, error) {
cmd := exec.CommandContext(ctx, command, args...)
// Copy env
cmd.Env = os.Environ()
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("failed to create stdin pipe: %w", err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("failed to create stdout pipe: %w", err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, fmt.Errorf("failed to create stderr pipe: %w", err)
}
client := &Client{
Cmd: cmd,
stdin: stdin,
stdout: bufio.NewReader(stdout),
stderr: stderr,
handlers: make(map[int32]chan *Message),
notificationHandlers: make(map[string]NotificationHandler),
serverRequestHandlers: make(map[string]ServerRequestHandler),
diagnostics: make(map[protocol.DocumentUri][]protocol.Diagnostic),
openFiles: make(map[string]*OpenFileInfo),
}
// Initialize server state
client.serverState.Store(StateStarting)
// Start the LSP server process
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed to start LSP server: %w", err)
}
// Handle stderr in a separate goroutine
go func() {
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
slog.Info("LSP Server", "message", scanner.Text())
}
if err := scanner.Err(); err != nil {
slog.Error("Error reading LSP stderr", "error", err)
}
}()
// Start message handling loop
go func() {
defer logging.RecoverPanic("LSP-message-handler", func() {
status.Error("LSP message handler crashed, LSP functionality may be impaired")
})
client.handleMessages()
}()
return client, nil
}
func (c *Client) RegisterNotificationHandler(method string, handler NotificationHandler) {
c.notificationMu.Lock()
defer c.notificationMu.Unlock()
c.notificationHandlers[method] = handler
}
func (c *Client) RegisterServerRequestHandler(method string, handler ServerRequestHandler) {
c.serverHandlersMu.Lock()
defer c.serverHandlersMu.Unlock()
c.serverRequestHandlers[method] = handler
}
func (c *Client) InitializeLSPClient(ctx context.Context, workspaceDir string) (*protocol.InitializeResult, error) {
initParams := &protocol.InitializeParams{
WorkspaceFoldersInitializeParams: protocol.WorkspaceFoldersInitializeParams{
WorkspaceFolders: []protocol.WorkspaceFolder{
{
URI: protocol.URI("file://" + workspaceDir),
Name: workspaceDir,
},
},
},
XInitializeParams: protocol.XInitializeParams{
ProcessID: int32(os.Getpid()),
ClientInfo: &protocol.ClientInfo{
Name: "mcp-language-server",
Version: "0.1.0",
},
RootPath: workspaceDir,
RootURI: protocol.DocumentUri("file://" + workspaceDir),
Capabilities: protocol.ClientCapabilities{
Workspace: protocol.WorkspaceClientCapabilities{
Configuration: true,
DidChangeConfiguration: protocol.DidChangeConfigurationClientCapabilities{
DynamicRegistration: true,
},
DidChangeWatchedFiles: protocol.DidChangeWatchedFilesClientCapabilities{
DynamicRegistration: true,
RelativePatternSupport: true,
},
},
TextDocument: protocol.TextDocumentClientCapabilities{
Synchronization: &protocol.TextDocumentSyncClientCapabilities{
DynamicRegistration: true,
DidSave: true,
},
Completion: protocol.CompletionClientCapabilities{
CompletionItem: protocol.ClientCompletionItemOptions{},
},
CodeLens: &protocol.CodeLensClientCapabilities{
DynamicRegistration: true,
},
DocumentSymbol: protocol.DocumentSymbolClientCapabilities{},
CodeAction: protocol.CodeActionClientCapabilities{
CodeActionLiteralSupport: protocol.ClientCodeActionLiteralOptions{
CodeActionKind: protocol.ClientCodeActionKindOptions{
ValueSet: []protocol.CodeActionKind{},
},
},
},
PublishDiagnostics: protocol.PublishDiagnosticsClientCapabilities{
VersionSupport: true,
},
SemanticTokens: protocol.SemanticTokensClientCapabilities{
Requests: protocol.ClientSemanticTokensRequestOptions{
Range: &protocol.Or_ClientSemanticTokensRequestOptions_range{},
Full: &protocol.Or_ClientSemanticTokensRequestOptions_full{},
},
TokenTypes: []string{},
TokenModifiers: []string{},
Formats: []protocol.TokenFormat{},
},
},
Window: protocol.WindowClientCapabilities{},
},
InitializationOptions: map[string]any{
"codelenses": map[string]bool{
"generate": true,
"regenerate_cgo": true,
"test": true,
"tidy": true,
"upgrade_dependency": true,
"vendor": true,
"vulncheck": false,
},
},
},
}
var result protocol.InitializeResult
if err := c.Call(ctx, "initialize", initParams, &result); err != nil {
return nil, fmt.Errorf("initialize failed: %w", err)
}
if err := c.Notify(ctx, "initialized", struct{}{}); err != nil {
return nil, fmt.Errorf("initialized notification failed: %w", err)
}
// Register handlers
c.RegisterServerRequestHandler("workspace/applyEdit", HandleApplyEdit)
c.RegisterServerRequestHandler("workspace/configuration", HandleWorkspaceConfiguration)
c.RegisterServerRequestHandler("client/registerCapability", HandleRegisterCapability)
c.RegisterNotificationHandler("window/showMessage", HandleServerMessage)
c.RegisterNotificationHandler("textDocument/publishDiagnostics",
func(params json.RawMessage) { HandleDiagnostics(c, params) })
// Notify the LSP server
err := c.Initialized(ctx, protocol.InitializedParams{})
if err != nil {
return nil, fmt.Errorf("initialization failed: %w", err)
}
return &result, nil
}
func (c *Client) Close() error {
// Try to close all open files first
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Attempt to close files but continue shutdown regardless
c.CloseAllFiles(ctx)
// Close stdin to signal the server
if err := c.stdin.Close(); err != nil {
return fmt.Errorf("failed to close stdin: %w", err)
}
// Use a channel to handle the Wait with timeout
done := make(chan error, 1)
go func() {
done <- c.Cmd.Wait()
}()
// Wait for process to exit with timeout
select {
case err := <-done:
return err
case <-time.After(2 * time.Second):
// If we timeout, try to kill the process
if err := c.Cmd.Process.Kill(); err != nil {
return fmt.Errorf("failed to kill process: %w", err)
}
return fmt.Errorf("process killed after timeout")
}
}
type ServerState int
const (
StateStarting ServerState = iota
StateReady
StateError
)
// GetServerState returns the current state of the LSP server
func (c *Client) GetServerState() ServerState {
if val := c.serverState.Load(); val != nil {
return val.(ServerState)
}
return StateStarting
}
// SetServerState sets the current state of the LSP server
func (c *Client) SetServerState(state ServerState) {
c.serverState.Store(state)
}
// WaitForServerReady waits for the server to be ready by polling the server
// with a simple request until it responds successfully or times out
func (c *Client) WaitForServerReady(ctx context.Context) error {
cnf := config.Get()
// Set initial state
c.SetServerState(StateStarting)
// Create a context with timeout
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
// Try to ping the server with a simple request
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
if cnf.DebugLSP {
slog.Debug("Waiting for LSP server to be ready...")
}
// Determine server type for specialized initialization
serverType := c.detectServerType()
// For TypeScript-like servers, we need to open some key files first
if serverType == ServerTypeTypeScript {
if cnf.DebugLSP {
slog.Debug("TypeScript-like server detected, opening key configuration files")
}
c.openKeyConfigFiles(ctx)
}
for {
select {
case <-ctx.Done():
c.SetServerState(StateError)
return fmt.Errorf("timeout waiting for LSP server to be ready")
case <-ticker.C:
// Try a ping method appropriate for this server type
err := c.pingServerByType(ctx, serverType)
if err == nil {
// Server responded successfully
c.SetServerState(StateReady)
if cnf.DebugLSP {
slog.Debug("LSP server is ready")
}
return nil
} else {
slog.Debug("LSP server not ready yet", "error", err, "serverType", serverType)
}
if cnf.DebugLSP {
slog.Debug("LSP server not ready yet", "error", err, "serverType", serverType)
}
}
}
}
// ServerType represents the type of LSP server
type ServerType int
const (
ServerTypeUnknown ServerType = iota
ServerTypeGo
ServerTypeTypeScript
ServerTypeRust
ServerTypePython
ServerTypeGeneric
)
// detectServerType tries to determine what type of LSP server we're dealing with
func (c *Client) detectServerType() ServerType {
if c.Cmd == nil {
return ServerTypeUnknown
}
cmdPath := strings.ToLower(c.Cmd.Path)
switch {
case strings.Contains(cmdPath, "gopls"):
return ServerTypeGo
case strings.Contains(cmdPath, "typescript") || strings.Contains(cmdPath, "vtsls") || strings.Contains(cmdPath, "tsserver"):
return ServerTypeTypeScript
case strings.Contains(cmdPath, "rust-analyzer"):
return ServerTypeRust
case strings.Contains(cmdPath, "pyright") || strings.Contains(cmdPath, "pylsp") || strings.Contains(cmdPath, "python"):
return ServerTypePython
default:
return ServerTypeGeneric
}
}
// openKeyConfigFiles opens important configuration files that help initialize the server
func (c *Client) openKeyConfigFiles(ctx context.Context) {
workDir := config.WorkingDirectory()
serverType := c.detectServerType()
var filesToOpen []string
switch serverType {
case ServerTypeTypeScript:
// TypeScript servers need these config files to properly initialize
filesToOpen = []string{
filepath.Join(workDir, "tsconfig.json"),
filepath.Join(workDir, "package.json"),
filepath.Join(workDir, "jsconfig.json"),
}
// Also find and open a few TypeScript files to help the server initialize
c.openTypeScriptFiles(ctx, workDir)
case ServerTypeGo:
filesToOpen = []string{
filepath.Join(workDir, "go.mod"),
filepath.Join(workDir, "go.sum"),
}
case ServerTypeRust:
filesToOpen = []string{
filepath.Join(workDir, "Cargo.toml"),
filepath.Join(workDir, "Cargo.lock"),
}
}
// Try to open each file, ignoring errors if they don't exist
for _, file := range filesToOpen {
if _, err := os.Stat(file); err == nil {
// File exists, try to open it
if err := c.OpenFile(ctx, file); err != nil {
slog.Debug("Failed to open key config file", "file", file, "error", err)
} else {
slog.Debug("Opened key config file for initialization", "file", file)
}
}
}
}
// pingServerByType sends a ping request appropriate for the server type
func (c *Client) pingServerByType(ctx context.Context, serverType ServerType) error {
switch serverType {
case ServerTypeTypeScript:
// For TypeScript, try a document symbol request on an open file
return c.pingTypeScriptServer(ctx)
case ServerTypeGo:
// For Go, workspace/symbol works well
return c.pingWithWorkspaceSymbol(ctx)
case ServerTypeRust:
// For Rust, workspace/symbol works well
return c.pingWithWorkspaceSymbol(ctx)
default:
// Default ping method
return c.pingWithWorkspaceSymbol(ctx)
}
}
// pingTypeScriptServer tries to ping a TypeScript server with appropriate methods
func (c *Client) pingTypeScriptServer(ctx context.Context) error {
// First try workspace/symbol which works for many servers
if err := c.pingWithWorkspaceSymbol(ctx); err == nil {
return nil
}
// If that fails, try to find an open file and request document symbols
c.openFilesMu.RLock()
defer c.openFilesMu.RUnlock()
// If we have any open files, try to get document symbols for one
for uri := range c.openFiles {
filePath := strings.TrimPrefix(uri, "file://")
if strings.HasSuffix(filePath, ".ts") || strings.HasSuffix(filePath, ".js") ||
strings.HasSuffix(filePath, ".tsx") || strings.HasSuffix(filePath, ".jsx") {
var symbols []protocol.DocumentSymbol
err := c.Call(ctx, "textDocument/documentSymbol", protocol.DocumentSymbolParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: protocol.DocumentUri(uri),
},
}, &symbols)
if err == nil {
return nil
}
}
}
// If we have no open TypeScript files, try to find and open one
workDir := config.WorkingDirectory()
err := filepath.WalkDir(workDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
// Skip directories and non-TypeScript files
if d.IsDir() {
return nil
}
ext := filepath.Ext(path)
if ext == ".ts" || ext == ".js" || ext == ".tsx" || ext == ".jsx" {
// Found a TypeScript file, try to open it
if err := c.OpenFile(ctx, path); err == nil {
// Successfully opened, stop walking
return filepath.SkipAll
}
}
return nil
})
if err != nil {
slog.Debug("Error walking directory for TypeScript files", "error", err)
}
// Final fallback - just try a generic capability
return c.pingWithServerCapabilities(ctx)
}
// openTypeScriptFiles finds and opens TypeScript files to help initialize the server
func (c *Client) openTypeScriptFiles(ctx context.Context, workDir string) {
cnf := config.Get()
filesOpened := 0
maxFilesToOpen := 5 // Limit to a reasonable number of files
// Find and open TypeScript files
err := filepath.WalkDir(workDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
// Skip directories and non-TypeScript files
if d.IsDir() {
// Skip common directories to avoid wasting time
if shouldSkipDir(path) {
return filepath.SkipDir
}
return nil
}
// Check if we've opened enough files
if filesOpened >= maxFilesToOpen {
return filepath.SkipAll
}
// Check file extension
ext := filepath.Ext(path)
if ext == ".ts" || ext == ".tsx" || ext == ".js" || ext == ".jsx" {
// Try to open the file
if err := c.OpenFile(ctx, path); err == nil {
filesOpened++
if cnf.DebugLSP {
slog.Debug("Opened TypeScript file for initialization", "file", path)
}
}
}
return nil
})
if err != nil && cnf.DebugLSP {
slog.Debug("Error walking directory for TypeScript files", "error", err)
}
if cnf.DebugLSP {
slog.Debug("Opened TypeScript files for initialization", "count", filesOpened)
}
}
// shouldSkipDir returns true if the directory should be skipped during file search
func shouldSkipDir(path string) bool {
dirName := filepath.Base(path)
// Skip hidden directories
if strings.HasPrefix(dirName, ".") {
return true
}
// Skip common directories that won't contain relevant source files
skipDirs := map[string]bool{
"node_modules": true,
"dist": true,
"build": true,
"coverage": true,
"vendor": true,
"target": true,
}
return skipDirs[dirName]
}
// pingWithWorkspaceSymbol tries a workspace/symbol request
func (c *Client) pingWithWorkspaceSymbol(ctx context.Context) error {
var result []protocol.SymbolInformation
return c.Call(ctx, "workspace/symbol", protocol.WorkspaceSymbolParams{
Query: "",
}, &result)
}
// pingWithServerCapabilities tries to get server capabilities
func (c *Client) pingWithServerCapabilities(ctx context.Context) error {
// This is a very lightweight request that should work for most servers
return c.Notify(ctx, "$/cancelRequest", struct{ ID int }{ID: -1})
}
type OpenFileInfo struct {
Version int32
URI protocol.DocumentUri
}
func (c *Client) OpenFile(ctx context.Context, filepath string) error {
uri := fmt.Sprintf("file://%s", filepath)
c.openFilesMu.Lock()
if _, exists := c.openFiles[uri]; exists {
c.openFilesMu.Unlock()
return nil // Already open
}
c.openFilesMu.Unlock()
// Skip files that do not exist or cannot be read
content, err := os.ReadFile(filepath)
if err != nil {
return fmt.Errorf("error reading file: %w", err)
}
params := protocol.DidOpenTextDocumentParams{
TextDocument: protocol.TextDocumentItem{
URI: protocol.DocumentUri(uri),
LanguageID: DetectLanguageID(uri),
Version: 1,
Text: string(content),
},
}
if err := c.Notify(ctx, "textDocument/didOpen", params); err != nil {
return err
}
c.openFilesMu.Lock()
c.openFiles[uri] = &OpenFileInfo{
Version: 1,
URI: protocol.DocumentUri(uri),
}
c.openFilesMu.Unlock()
return nil
}
func (c *Client) NotifyChange(ctx context.Context, filepath string) error {
uri := fmt.Sprintf("file://%s", filepath)
// Verify file exists before attempting to read it
if _, err := os.Stat(filepath); err != nil {
if os.IsNotExist(err) {
// File was deleted - close it in the LSP client instead of notifying change
return c.CloseFile(ctx, filepath)
}
return fmt.Errorf("error checking file: %w", err)
}
content, err := os.ReadFile(filepath)
if err != nil {
return fmt.Errorf("error reading file: %w", err)
}
c.openFilesMu.Lock()
fileInfo, isOpen := c.openFiles[uri]
if !isOpen {
c.openFilesMu.Unlock()
return fmt.Errorf("cannot notify change for unopened file: %s", filepath)
}
// Increment version
fileInfo.Version++
version := fileInfo.Version
c.openFilesMu.Unlock()
params := protocol.DidChangeTextDocumentParams{
TextDocument: protocol.VersionedTextDocumentIdentifier{
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
URI: protocol.DocumentUri(uri),
},
Version: version,
},
ContentChanges: []protocol.TextDocumentContentChangeEvent{
{
Value: protocol.TextDocumentContentChangeWholeDocument{
Text: string(content),
},
},
},
}
return c.Notify(ctx, "textDocument/didChange", params)
}
func (c *Client) CloseFile(ctx context.Context, filepath string) error {
cnf := config.Get()
uri := fmt.Sprintf("file://%s", filepath)
c.openFilesMu.Lock()
if _, exists := c.openFiles[uri]; !exists {
c.openFilesMu.Unlock()
return nil // Already closed
}
c.openFilesMu.Unlock()
params := protocol.DidCloseTextDocumentParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: protocol.DocumentUri(uri),
},
}
if cnf.DebugLSP {
slog.Debug("Closing file", "file", filepath)
}
if err := c.Notify(ctx, "textDocument/didClose", params); err != nil {
return err
}
c.openFilesMu.Lock()
delete(c.openFiles, uri)
c.openFilesMu.Unlock()
return nil
}
func (c *Client) IsFileOpen(filepath string) bool {
uri := fmt.Sprintf("file://%s", filepath)
c.openFilesMu.RLock()
defer c.openFilesMu.RUnlock()
_, exists := c.openFiles[uri]
return exists
}
// CloseAllFiles closes all currently open files
func (c *Client) CloseAllFiles(ctx context.Context) {
cnf := config.Get()
c.openFilesMu.Lock()
filesToClose := make([]string, 0, len(c.openFiles))
// First collect all URIs that need to be closed
for uri := range c.openFiles {
// Convert URI back to file path by trimming "file://" prefix
filePath := strings.TrimPrefix(uri, "file://")
filesToClose = append(filesToClose, filePath)
}
c.openFilesMu.Unlock()
// Then close them all
for _, filePath := range filesToClose {
err := c.CloseFile(ctx, filePath)
if err != nil && cnf.DebugLSP {
slog.Warn("Error closing file", "file", filePath, "error", err)
}
}
if cnf.DebugLSP {
slog.Debug("Closed all files", "files", filesToClose)
}
}
func (c *Client) GetFileDiagnostics(uri protocol.DocumentUri) []protocol.Diagnostic {
c.diagnosticsMu.RLock()
defer c.diagnosticsMu.RUnlock()
return c.diagnostics[uri]
}
// GetDiagnostics returns all diagnostics for all files
func (c *Client) GetDiagnostics() map[protocol.DocumentUri][]protocol.Diagnostic {
return c.diagnostics
}
// OpenFileOnDemand opens a file only if it's not already open
// This is used for lazy-loading files when they're actually needed
func (c *Client) OpenFileOnDemand(ctx context.Context, filepath string) error {
// Check if the file is already open
if c.IsFileOpen(filepath) {
return nil
}
// Open the file
return c.OpenFile(ctx, filepath)
}
// GetDiagnosticsForFile ensures a file is open and returns its diagnostics
// This is useful for on-demand diagnostics when using lazy loading
func (c *Client) GetDiagnosticsForFile(ctx context.Context, filepath string) ([]protocol.Diagnostic, error) {
uri := fmt.Sprintf("file://%s", filepath)
documentUri := protocol.DocumentUri(uri)
// Make sure the file is open
if !c.IsFileOpen(filepath) {
if err := c.OpenFile(ctx, filepath); err != nil {
return nil, fmt.Errorf("failed to open file for diagnostics: %w", err)
}
// Give the LSP server a moment to process the file
time.Sleep(100 * time.Millisecond)
}
// Get diagnostics
c.diagnosticsMu.RLock()
diagnostics := c.diagnostics[documentUri]
c.diagnosticsMu.RUnlock()
return diagnostics, nil
}
// ClearDiagnosticsForURI removes diagnostics for a specific URI from the cache
func (c *Client) ClearDiagnosticsForURI(uri protocol.DocumentUri) {
c.diagnosticsMu.Lock()
defer c.diagnosticsMu.Unlock()
delete(c.diagnostics, uri)
}
|