diagnostics.go

  1package tools
  2
  3import (
  4	"context"
  5	"encoding/json"
  6	"fmt"
  7	"maps"
  8	"sort"
  9	"strings"
 10	"time"
 11
 12	"github.com/opencode-ai/opencode/internal/lsp"
 13	"github.com/opencode-ai/opencode/internal/lsp/protocol"
 14)
 15
 16type DiagnosticsParams struct {
 17	FilePath string `json:"file_path"`
 18}
 19type diagnosticsTool struct {
 20	lspClients map[string]*lsp.Client
 21}
 22
 23const (
 24	DiagnosticsToolName    = "diagnostics"
 25	diagnosticsDescription = `Get diagnostics for a file and/or project.
 26WHEN TO USE THIS TOOL:
 27- Use when you need to check for errors or warnings in your code
 28- Helpful for debugging and ensuring code quality
 29- Good for getting a quick overview of issues in a file or project
 30HOW TO USE:
 31- Provide a path to a file to get diagnostics for that file
 32- Leave the path empty to get diagnostics for the entire project
 33- Results are displayed in a structured format with severity levels
 34FEATURES:
 35- Displays errors, warnings, and hints
 36- Groups diagnostics by severity
 37- Provides detailed information about each diagnostic
 38LIMITATIONS:
 39- Results are limited to the diagnostics provided by the LSP clients
 40- May not cover all possible issues in the code
 41- Does not provide suggestions for fixing issues
 42TIPS:
 43- Use in conjunction with other tools for a comprehensive code review
 44- Combine with the LSP client for real-time diagnostics
 45`
 46)
 47
 48func NewDiagnosticsTool(lspClients map[string]*lsp.Client) BaseTool {
 49	return &diagnosticsTool{
 50		lspClients,
 51	}
 52}
 53
 54func (b *diagnosticsTool) Info() ToolInfo {
 55	return ToolInfo{
 56		Name:        DiagnosticsToolName,
 57		Description: diagnosticsDescription,
 58		Parameters: map[string]any{
 59			"file_path": map[string]any{
 60				"type":        "string",
 61				"description": "The path to the file to get diagnostics for (leave w empty for project diagnostics)",
 62			},
 63		},
 64		Required: []string{},
 65	}
 66}
 67
 68func (b *diagnosticsTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
 69	var params DiagnosticsParams
 70	if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
 71		return NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
 72	}
 73
 74	lsps := b.lspClients
 75
 76	if len(lsps) == 0 {
 77		return NewTextErrorResponse("no LSP clients available"), nil
 78	}
 79
 80	if params.FilePath != "" {
 81		notifyLspOpenFile(ctx, params.FilePath, lsps)
 82		waitForLspDiagnostics(ctx, params.FilePath, lsps)
 83	}
 84
 85	output := getDiagnostics(params.FilePath, lsps)
 86
 87	return NewTextResponse(output), nil
 88}
 89
 90func notifyLspOpenFile(ctx context.Context, filePath string, lsps map[string]*lsp.Client) {
 91	for _, client := range lsps {
 92		err := client.OpenFile(ctx, filePath)
 93		if err != nil {
 94			continue
 95		}
 96	}
 97}
 98
 99func waitForLspDiagnostics(ctx context.Context, filePath string, lsps map[string]*lsp.Client) {
100	if len(lsps) == 0 {
101		return
102	}
103
104	diagChan := make(chan struct{}, 1)
105
106	for _, client := range lsps {
107		originalDiags := make(map[protocol.DocumentUri][]protocol.Diagnostic)
108		maps.Copy(originalDiags, client.GetDiagnostics())
109
110		handler := func(params json.RawMessage) {
111			lsp.HandleDiagnostics(client, params)
112			var diagParams protocol.PublishDiagnosticsParams
113			if err := json.Unmarshal(params, &diagParams); err != nil {
114				return
115			}
116
117			if diagParams.URI.Path() == filePath || hasDiagnosticsChanged(client.GetDiagnostics(), originalDiags) {
118				select {
119				case diagChan <- struct{}{}:
120				default:
121				}
122			}
123		}
124
125		client.RegisterNotificationHandler("textDocument/publishDiagnostics", handler)
126
127		if client.IsFileOpen(filePath) {
128			err := client.NotifyChange(ctx, filePath)
129			if err != nil {
130				continue
131			}
132		} else {
133			err := client.OpenFile(ctx, filePath)
134			if err != nil {
135				continue
136			}
137		}
138	}
139
140	select {
141	case <-diagChan:
142	case <-time.After(5 * time.Second):
143	case <-ctx.Done():
144	}
145}
146
147func hasDiagnosticsChanged(current, original map[protocol.DocumentUri][]protocol.Diagnostic) bool {
148	for uri, diags := range current {
149		origDiags, exists := original[uri]
150		if !exists || len(diags) != len(origDiags) {
151			return true
152		}
153	}
154	return false
155}
156
157func getDiagnostics(filePath string, lsps map[string]*lsp.Client) string {
158	fileDiagnostics := []string{}
159	projectDiagnostics := []string{}
160
161	formatDiagnostic := func(pth string, diagnostic protocol.Diagnostic, source string) string {
162		severity := "Info"
163		switch diagnostic.Severity {
164		case protocol.SeverityError:
165			severity = "Error"
166		case protocol.SeverityWarning:
167			severity = "Warn"
168		case protocol.SeverityHint:
169			severity = "Hint"
170		}
171
172		location := fmt.Sprintf("%s:%d:%d", pth, diagnostic.Range.Start.Line+1, diagnostic.Range.Start.Character+1)
173
174		sourceInfo := ""
175		if diagnostic.Source != "" {
176			sourceInfo = diagnostic.Source
177		} else if source != "" {
178			sourceInfo = source
179		}
180
181		codeInfo := ""
182		if diagnostic.Code != nil {
183			codeInfo = fmt.Sprintf("[%v]", diagnostic.Code)
184		}
185
186		tagsInfo := ""
187		if len(diagnostic.Tags) > 0 {
188			tags := []string{}
189			for _, tag := range diagnostic.Tags {
190				switch tag {
191				case protocol.Unnecessary:
192					tags = append(tags, "unnecessary")
193				case protocol.Deprecated:
194					tags = append(tags, "deprecated")
195				}
196			}
197			if len(tags) > 0 {
198				tagsInfo = fmt.Sprintf(" (%s)", strings.Join(tags, ", "))
199			}
200		}
201
202		return fmt.Sprintf("%s: %s [%s]%s%s %s",
203			severity,
204			location,
205			sourceInfo,
206			codeInfo,
207			tagsInfo,
208			diagnostic.Message)
209	}
210
211	for lspName, client := range lsps {
212		diagnostics := client.GetDiagnostics()
213		if len(diagnostics) > 0 {
214			for location, diags := range diagnostics {
215				isCurrentFile := location.Path() == filePath
216
217				for _, diag := range diags {
218					formattedDiag := formatDiagnostic(location.Path(), diag, lspName)
219
220					if isCurrentFile {
221						fileDiagnostics = append(fileDiagnostics, formattedDiag)
222					} else {
223						projectDiagnostics = append(projectDiagnostics, formattedDiag)
224					}
225				}
226			}
227		}
228	}
229
230	sort.Slice(fileDiagnostics, func(i, j int) bool {
231		iIsError := strings.HasPrefix(fileDiagnostics[i], "Error")
232		jIsError := strings.HasPrefix(fileDiagnostics[j], "Error")
233		if iIsError != jIsError {
234			return iIsError // Errors come first
235		}
236		return fileDiagnostics[i] < fileDiagnostics[j] // Then alphabetically
237	})
238
239	sort.Slice(projectDiagnostics, func(i, j int) bool {
240		iIsError := strings.HasPrefix(projectDiagnostics[i], "Error")
241		jIsError := strings.HasPrefix(projectDiagnostics[j], "Error")
242		if iIsError != jIsError {
243			return iIsError
244		}
245		return projectDiagnostics[i] < projectDiagnostics[j]
246	})
247
248	output := ""
249
250	if len(fileDiagnostics) > 0 {
251		output += "\n<file_diagnostics>\n"
252		if len(fileDiagnostics) > 10 {
253			output += strings.Join(fileDiagnostics[:10], "\n")
254			output += fmt.Sprintf("\n... and %d more diagnostics", len(fileDiagnostics)-10)
255		} else {
256			output += strings.Join(fileDiagnostics, "\n")
257		}
258		output += "\n</file_diagnostics>\n"
259	}
260
261	if len(projectDiagnostics) > 0 {
262		output += "\n<project_diagnostics>\n"
263		if len(projectDiagnostics) > 10 {
264			output += strings.Join(projectDiagnostics[:10], "\n")
265			output += fmt.Sprintf("\n... and %d more diagnostics", len(projectDiagnostics)-10)
266		} else {
267			output += strings.Join(projectDiagnostics, "\n")
268		}
269		output += "\n</project_diagnostics>\n"
270	}
271
272	if len(fileDiagnostics) > 0 || len(projectDiagnostics) > 0 {
273		fileErrors := countSeverity(fileDiagnostics, "Error")
274		fileWarnings := countSeverity(fileDiagnostics, "Warn")
275		projectErrors := countSeverity(projectDiagnostics, "Error")
276		projectWarnings := countSeverity(projectDiagnostics, "Warn")
277
278		output += "\n<diagnostic_summary>\n"
279		output += fmt.Sprintf("Current file: %d errors, %d warnings\n", fileErrors, fileWarnings)
280		output += fmt.Sprintf("Project: %d errors, %d warnings\n", projectErrors, projectWarnings)
281		output += "</diagnostic_summary>\n"
282	}
283
284	return output
285}
286
287func countSeverity(diagnostics []string, severity string) int {
288	count := 0
289	for _, diag := range diagnostics {
290		if strings.HasPrefix(diag, severity) {
291			count++
292		}
293	}
294	return count
295}