@@ -53,6 +53,7 @@ func (b *diagnosticsTool) Run(ctx context.Context, call ToolCall) (ToolResponse,
5353
5454 if params.FilePath != "" {
5555 notifyLspOpenFile(ctx, params.FilePath, lsps)
56+ waitForLspDiagnostics(ctx, params.FilePath, lsps)
5657 }
5758
5859 output := appendDiagnostics(params.FilePath, lsps)
@@ -61,6 +62,22 @@ func (b *diagnosticsTool) Run(ctx context.Context, call ToolCall) (ToolResponse,
6162}
6263
6364func notifyLspOpenFile(ctx context.Context, filePath string, lsps map[string]*lsp.Client) {
65+ for _, client := range lsps {
66+ // Open the file
67+ err := client.OpenFile(ctx, filePath)
68+ if err != nil {
69+ // If there's an error opening the file, continue to the next client
70+ continue
71+ }
72+ }
73+}
74+
75+// waitForLspDiagnostics opens a file in LSP clients and waits for diagnostics to be published
76+func waitForLspDiagnostics(ctx context.Context, filePath string, lsps map[string]*lsp.Client) {
77+ if len(lsps) == 0 {
78+ return
79+ }
80+
6481 // Create a channel to receive diagnostic notifications
6582 diagChan := make(chan struct{}, 1)
6683
@@ -92,11 +109,18 @@ func notifyLspOpenFile(ctx context.Context, filePath string, lsps map[string]*ls
92109 // Register our temporary handler
93110 client.RegisterNotificationHandler("textDocument/publishDiagnostics", handler)
94111
95โ // Open the file
96โ err := client.OpenFile(ctx, filePath)
97โ if err != nil {
98โ // If there's an error opening the file, continue to the next client
99โ continue
112+ // Notify change if the file is already open
113+ if client.IsFileOpen(filePath) {
114+ err := client.NotifyChange(ctx, filePath)
115+ if err != nil {
116+ continue
117+ }
118+ } else {
119+ // Open the file if it's not already open
120+ err := client.OpenFile(ctx, filePath)
121+ if err != nil {
122+ continue
123+ }
100124 }
101125 }
102126
@@ -104,7 +128,7 @@ func notifyLspOpenFile(ctx context.Context, filePath string, lsps map[string]*ls
104128 select {
105129 case <-diagChan:
106130 // Diagnostics received
107โ case <-time.After(10 * time.Second):
131+ case <-time.After(5 * time.Second):
108132 // Timeout after 5 seconds - this is a fallback in case no diagnostics are published
109133 case <-ctx.Done():
110134 // Context cancelled