on main help sign in
perf(tools/view): pause briefly for LSP diagnostics when viewing a file
3c5fcffChristian Rocha
internal/agent/tools/diagnostics.go+21
@@ -59,6 +59,27 @@ func openInLSPs(
59 }
59 }
60}
60}
61
61
62+// waitForLSPDiagnostics waits briefly for diagnostics publication after a file
63+// has been opened. Intended for read-only situations where viewing up-to-date
64+// files matters but latency should remain low (i.e. when using the view tool).
65+func waitForLSPDiagnostics(
66+ ctx context.Context,
67+ manager *lsp.Manager,
68+ filepath string,
69+ timeout time.Duration,
70+) {
71+ if filepath == "" || manager == nil || timeout <= 0 {
72+ return
73+ }
74+
75+ for client := range manager.Clients().Seq() {
76+ if !client.HandlesFile(filepath) {
77+ continue
78+ }
79+ client.WaitForDiagnostics(ctx, timeout)
80+ }
81+}
82+
62// notifyLSPs notifies LSP servers that a file has changed and waits for
83// notifyLSPs notifies LSP servers that a file has changed and waits for
63// updated diagnostics. Use this after edit/multiedit operations.
84// updated diagnostics. Use this after edit/multiedit operations.
64func notifyLSPs(
85func notifyLSPs(
internal/agent/tools/view.go+2
@@ -10,6 +10,7 @@ import (
10 "os"
10 "os"
11 "path/filepath"
11 "path/filepath"
12 "strings"
12 "strings"
13+ "time"
13 "unicode/utf8"
14 "unicode/utf8"
14
15
15 "charm.land/fantasy"
16 "charm.land/fantasy"
@@ -194,6 +195,7 @@ func NewViewTool(
194 }
195 }
195
196
196 openInLSPs(ctx, lspManager, filePath)
197 openInLSPs(ctx, lspManager, filePath)
198+ waitForLSPDiagnostics(ctx, lspManager, filePath, 300*time.Millisecond)
197 output := "<file>\n"
199 output := "<file>\n"
198 output += addLineNumbers(content, params.Offset+1)
200 output += addLineNumbers(content, params.Offset+1)
199
201