1use anyhow::{anyhow, Result};
2use assistant_tool::{ActionLog, Tool};
3use gpui::{App, Entity, Task};
4use language::{DiagnosticSeverity, OffsetRangeExt};
5use language_model::LanguageModelRequestMessage;
6use project::Project;
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9use std::{
10 fmt::Write,
11 path::{Path, PathBuf},
12 sync::Arc,
13};
14use ui::IconName;
15
16#[derive(Debug, Serialize, Deserialize, JsonSchema)]
17pub struct DiagnosticsToolInput {
18 /// The path to get diagnostics for. If not provided, returns a project-wide summary.
19 ///
20 /// This path should never be absolute, and the first component
21 /// of the path should always be a root directory in a project.
22 ///
23 /// <example>
24 /// If the project has the following root directories:
25 ///
26 /// - lorem
27 /// - ipsum
28 ///
29 /// If you wanna access diagnostics for `dolor.txt` in `ipsum`, you should use the path `ipsum/dolor.txt`.
30 /// </example>
31 pub path: Option<PathBuf>,
32}
33
34pub struct DiagnosticsTool;
35
36impl Tool for DiagnosticsTool {
37 fn name(&self) -> String {
38 "diagnostics".into()
39 }
40
41 fn needs_confirmation(&self) -> bool {
42 false
43 }
44
45 fn description(&self) -> String {
46 include_str!("./diagnostics_tool/description.md").into()
47 }
48
49 fn icon(&self) -> IconName {
50 IconName::Warning
51 }
52
53 fn input_schema(&self) -> serde_json::Value {
54 let schema = schemars::schema_for!(DiagnosticsToolInput);
55 serde_json::to_value(&schema).unwrap()
56 }
57
58 fn ui_text(&self, input: &serde_json::Value) -> String {
59 if let Some(path) = serde_json::from_value::<DiagnosticsToolInput>(input.clone())
60 .ok()
61 .and_then(|input| input.path)
62 {
63 format!("Check diagnostics for “`{}`”", path.display())
64 } else {
65 "Check project diagnostics".to_string()
66 }
67 }
68
69 fn run(
70 self: Arc<Self>,
71 input: serde_json::Value,
72 _messages: &[LanguageModelRequestMessage],
73 project: Entity<Project>,
74 _action_log: Entity<ActionLog>,
75 cx: &mut App,
76 ) -> Task<Result<String>> {
77 if let Some(path) = serde_json::from_value::<DiagnosticsToolInput>(input)
78 .ok()
79 .and_then(|input| input.path)
80 {
81 let Some(project_path) = project.read(cx).find_project_path(&path, cx) else {
82 return Task::ready(Err(anyhow!(
83 "Could not find path {} in project",
84 path.display()
85 )));
86 };
87 let buffer = project.update(cx, |project, cx| project.open_buffer(project_path, cx));
88
89 cx.spawn(async move |cx| {
90 let mut output = String::new();
91 let buffer = buffer.await?;
92 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot())?;
93
94 for (_, group) in snapshot.diagnostic_groups(None) {
95 let entry = &group.entries[group.primary_ix];
96 let range = entry.range.to_point(&snapshot);
97 let severity = match entry.diagnostic.severity {
98 DiagnosticSeverity::ERROR => "error",
99 DiagnosticSeverity::WARNING => "warning",
100 _ => continue,
101 };
102
103 writeln!(
104 output,
105 "{} at line {}: {}",
106 severity,
107 range.start.row + 1,
108 entry.diagnostic.message
109 )?;
110 }
111
112 if output.is_empty() {
113 Ok("File doesn't have errors or warnings!".to_string())
114 } else {
115 Ok(output)
116 }
117 })
118 } else {
119 let project = project.read(cx);
120 let mut output = String::new();
121 let mut has_diagnostics = false;
122
123 for (project_path, _, summary) in project.diagnostic_summaries(true, cx) {
124 if summary.error_count > 0 || summary.warning_count > 0 {
125 let Some(worktree) = project.worktree_for_id(project_path.worktree_id, cx)
126 else {
127 continue;
128 };
129
130 has_diagnostics = true;
131 output.push_str(&format!(
132 "{}: {} error(s), {} warning(s)\n",
133 Path::new(worktree.read(cx).root_name())
134 .join(project_path.path)
135 .display(),
136 summary.error_count,
137 summary.warning_count
138 ));
139 }
140 }
141
142 if has_diagnostics {
143 Task::ready(Ok(output))
144 } else {
145 Task::ready(Ok("No errors or warnings found in the project.".to_string()))
146 }
147 }
148 }
149}