diagnostics_tool.rs

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