diagnostics.go

  1package tools
  2
  3import (
  4	"context"
  5	_ "embed"
  6	"fmt"
  7	"log/slog"
  8	"sort"
  9	"strings"
 10	"sync"
 11	"time"
 12
 13	"charm.land/fantasy"
 14	"github.com/charmbracelet/crush/internal/lsp"
 15	"github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
 16)
 17
 18type DiagnosticsParams struct {
 19	FilePath string `json:"file_path,omitempty" description:"The path to the file to get diagnostics for (leave empty for project diagnostics)"`
 20}
 21
 22const DiagnosticsToolName = "lsp_diagnostics"
 23
 24//go:embed diagnostics.md
 25var diagnosticsDescription []byte
 26
 27func NewDiagnosticsTool(lspManager *lsp.Manager) fantasy.AgentTool {
 28	return fantasy.NewAgentTool(
 29		DiagnosticsToolName,
 30		FirstLineDescription(diagnosticsDescription),
 31		func(ctx context.Context, params DiagnosticsParams, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
 32			if lspManager.Clients().Len() == 0 {
 33				return fantasy.NewTextErrorResponse("no LSP clients available"), nil
 34			}
 35			notifyLSPs(ctx, lspManager, params.FilePath)
 36			output := getDiagnostics(params.FilePath, lspManager)
 37			return fantasy.NewTextResponse(output), nil
 38		})
 39}
 40
 41// openInLSPs ensures LSP servers are running and aware of the file, but does
 42// not notify changes or wait for fresh diagnostics. Use this for read-only
 43// operations like view where the file content hasn't changed.
 44func openInLSPs(
 45	ctx context.Context,
 46	manager *lsp.Manager,
 47	filepath string,
 48) {
 49	if filepath == "" || manager == nil {
 50		return
 51	}
 52
 53	manager.Start(ctx, filepath)
 54
 55	for client := range manager.Clients().Seq() {
 56		if !client.HandlesFile(filepath) {
 57			continue
 58		}
 59		_ = client.OpenFileOnDemand(ctx, filepath)
 60	}
 61}
 62
 63// waitForLSPDiagnostics waits briefly for diagnostics publication after a file
 64// has been opened. Intended for read-only situations where viewing up-to-date
 65// files matters but latency should remain low (i.e. when using the view tool).
 66func waitForLSPDiagnostics(
 67	ctx context.Context,
 68	manager *lsp.Manager,
 69	filepath string,
 70	timeout time.Duration,
 71) {
 72	if filepath == "" || manager == nil || timeout <= 0 {
 73		return
 74	}
 75
 76	var wg sync.WaitGroup
 77	for client := range manager.Clients().Seq() {
 78		if !client.HandlesFile(filepath) {
 79			continue
 80		}
 81		wg.Go(func() {
 82			client.WaitForDiagnostics(ctx, timeout)
 83		})
 84	}
 85	wg.Wait()
 86}
 87
 88// notifyLSPs notifies LSP servers that a file has changed and waits for
 89// updated diagnostics. Use this after edit/multiedit operations.
 90// When filepath is empty, refreshes all open files across all LSP clients
 91// and sends a workspace-level change notification for full re-analysis.
 92func notifyLSPs(
 93	ctx context.Context,
 94	manager *lsp.Manager,
 95	filepath string,
 96) {
 97	if manager == nil {
 98		return
 99	}
100	if filepath == "" {
101		// No specific file — refresh all open files for all clients.
102		var wg sync.WaitGroup
103		for client := range manager.Clients().Seq() {
104			wg.Go(func() {
105				client.RefreshOpenFiles(ctx)
106				if err := client.NotifyWorkspaceChange(ctx); err != nil {
107					slog.WarnContext(ctx, "Failed to notify workspace change", "error", err)
108				}
109				client.WaitForDiagnostics(ctx, 5*time.Second)
110			})
111		}
112		wg.Wait()
113		return
114	}
115
116	manager.Start(ctx, filepath)
117
118	var wg sync.WaitGroup
119	for client := range manager.Clients().Seq() {
120		if !client.HandlesFile(filepath) {
121			continue
122		}
123		_ = client.OpenFileOnDemand(ctx, filepath)
124		_ = client.NotifyChange(ctx, filepath)
125		wg.Go(func() {
126			client.WaitForDiagnostics(ctx, 5*time.Second)
127		})
128	}
129	wg.Wait()
130}
131
132func getDiagnostics(filePath string, manager *lsp.Manager) string {
133	if manager == nil {
134		return ""
135	}
136
137	var fileDiagnostics []string
138	var projectDiagnostics []string
139
140	for lspName, client := range manager.Clients().Seq2() {
141		for location, diags := range client.GetDiagnostics() {
142			path, err := location.Path()
143			if err != nil {
144				slog.Error("Failed to convert diagnostic location URI to path", "uri", location, "error", err)
145				continue
146			}
147			isCurrentFile := path == filePath
148			for _, diag := range diags {
149				formattedDiag := formatDiagnostic(path, diag, lspName)
150				if isCurrentFile {
151					fileDiagnostics = append(fileDiagnostics, formattedDiag)
152				} else {
153					projectDiagnostics = append(projectDiagnostics, formattedDiag)
154				}
155			}
156		}
157	}
158
159	sortDiagnostics(fileDiagnostics)
160	sortDiagnostics(projectDiagnostics)
161
162	var output strings.Builder
163	writeDiagnostics(&output, "file_diagnostics", fileDiagnostics)
164	writeDiagnostics(&output, "project_diagnostics", projectDiagnostics)
165
166	if len(fileDiagnostics) > 0 || len(projectDiagnostics) > 0 {
167		fileErrors := countSeverity(fileDiagnostics, "Error")
168		fileWarnings := countSeverity(fileDiagnostics, "Warn")
169		projectErrors := countSeverity(projectDiagnostics, "Error")
170		projectWarnings := countSeverity(projectDiagnostics, "Warn")
171		output.WriteString("\n<diagnostic_summary>\n")
172		fmt.Fprintf(&output, "Current file: %d errors, %d warnings\n", fileErrors, fileWarnings)
173		fmt.Fprintf(&output, "Project: %d errors, %d warnings\n", projectErrors, projectWarnings)
174		output.WriteString("</diagnostic_summary>\n")
175	}
176
177	out := output.String()
178	slog.Debug("Diagnostics", "output", out)
179	return out
180}
181
182func writeDiagnostics(output *strings.Builder, tag string, in []string) {
183	if len(in) == 0 {
184		return
185	}
186	output.WriteString("\n<" + tag + ">\n")
187	if len(in) > 10 {
188		output.WriteString(strings.Join(in[:10], "\n"))
189		fmt.Fprintf(output, "\n... and %d more diagnostics", len(in)-10)
190	} else {
191		output.WriteString(strings.Join(in, "\n"))
192	}
193	output.WriteString("\n</" + tag + ">\n")
194}
195
196func sortDiagnostics(in []string) []string {
197	sort.Slice(in, func(i, j int) bool {
198		iIsError := strings.HasPrefix(in[i], "Error")
199		jIsError := strings.HasPrefix(in[j], "Error")
200		if iIsError != jIsError {
201			return iIsError // Errors come first
202		}
203		return in[i] < in[j] // Then alphabetically
204	})
205	return in
206}
207
208func formatDiagnostic(pth string, diagnostic protocol.Diagnostic, source string) string {
209	severity := "Info"
210	switch diagnostic.Severity {
211	case protocol.SeverityError:
212		severity = "Error"
213	case protocol.SeverityWarning:
214		severity = "Warn"
215	case protocol.SeverityHint:
216		severity = "Hint"
217	}
218
219	location := fmt.Sprintf("%s:%d:%d", pth, diagnostic.Range.Start.Line+1, diagnostic.Range.Start.Character+1)
220
221	sourceInfo := source
222	if diagnostic.Source != "" {
223		sourceInfo += " " + diagnostic.Source
224	}
225
226	codeInfo := ""
227	if diagnostic.Code != nil {
228		codeInfo = fmt.Sprintf("[%v]", diagnostic.Code)
229	}
230
231	tagsInfo := ""
232	if len(diagnostic.Tags) > 0 {
233		var tags []string
234		for _, tag := range diagnostic.Tags {
235			switch tag {
236			case protocol.Unnecessary:
237				tags = append(tags, "unnecessary")
238			case protocol.Deprecated:
239				tags = append(tags, "deprecated")
240			}
241		}
242		if len(tags) > 0 {
243			tagsInfo = fmt.Sprintf(" (%s)", strings.Join(tags, ", "))
244		}
245	}
246
247	return fmt.Sprintf("%s: %s [%s]%s%s %s",
248		severity,
249		location,
250		sourceInfo,
251		codeInfo,
252		tagsInfo,
253		diagnostic.Message)
254}
255
256func countSeverity(diagnostics []string, severity string) int {
257	count := 0
258	for _, diag := range diagnostics {
259		if strings.HasPrefix(diag, severity) {
260			count++
261		}
262	}
263	return count
264}