1use anyhow::{anyhow, Result};
2use assistant_slash_command::{
3 ArgumentCompletion, SlashCommand, SlashCommandOutput, SlashCommandOutputSection,
4 SlashCommandResult,
5};
6use fuzzy::{PathMatch, StringMatchCandidate};
7use gpui::{AppContext, Model, Task, View, WeakView};
8use language::{
9 Anchor, BufferSnapshot, DiagnosticEntry, DiagnosticSeverity, LspAdapterDelegate,
10 OffsetRangeExt, ToOffset,
11};
12use project::{DiagnosticSummary, PathMatchCandidateSet, Project};
13use rope::Point;
14use std::{
15 fmt::Write,
16 path::{Path, PathBuf},
17 sync::{atomic::AtomicBool, Arc},
18};
19use ui::prelude::*;
20use util::paths::PathMatcher;
21use util::ResultExt;
22use workspace::Workspace;
23
24use crate::slash_command::create_label_for_command;
25
26pub(crate) struct DiagnosticsSlashCommand;
27
28impl DiagnosticsSlashCommand {
29 fn search_paths(
30 &self,
31 query: String,
32 cancellation_flag: Arc<AtomicBool>,
33 workspace: &View<Workspace>,
34 cx: &mut AppContext,
35 ) -> Task<Vec<PathMatch>> {
36 if query.is_empty() {
37 let workspace = workspace.read(cx);
38 let entries = workspace.recent_navigation_history(Some(10), cx);
39 let path_prefix: Arc<str> = Arc::default();
40 Task::ready(
41 entries
42 .into_iter()
43 .map(|(entry, _)| PathMatch {
44 score: 0.,
45 positions: Vec::new(),
46 worktree_id: entry.worktree_id.to_usize(),
47 path: entry.path.clone(),
48 path_prefix: path_prefix.clone(),
49 is_dir: false, // Diagnostics can't be produced for directories
50 distance_to_relative_ancestor: 0,
51 })
52 .collect(),
53 )
54 } else {
55 let worktrees = workspace.read(cx).visible_worktrees(cx).collect::<Vec<_>>();
56 let candidate_sets = worktrees
57 .into_iter()
58 .map(|worktree| {
59 let worktree = worktree.read(cx);
60 PathMatchCandidateSet {
61 snapshot: worktree.snapshot(),
62 include_ignored: worktree
63 .root_entry()
64 .map_or(false, |entry| entry.is_ignored),
65 include_root_name: true,
66 candidates: project::Candidates::Entries,
67 }
68 })
69 .collect::<Vec<_>>();
70
71 let executor = cx.background_executor().clone();
72 cx.foreground_executor().spawn(async move {
73 fuzzy::match_path_sets(
74 candidate_sets.as_slice(),
75 query.as_str(),
76 None,
77 false,
78 100,
79 &cancellation_flag,
80 executor,
81 )
82 .await
83 })
84 }
85 }
86}
87
88impl SlashCommand for DiagnosticsSlashCommand {
89 fn name(&self) -> String {
90 "diagnostics".into()
91 }
92
93 fn label(&self, cx: &AppContext) -> language::CodeLabel {
94 create_label_for_command("diagnostics", &[INCLUDE_WARNINGS_ARGUMENT], cx)
95 }
96
97 fn description(&self) -> String {
98 "Insert diagnostics".into()
99 }
100
101 fn icon(&self) -> IconName {
102 IconName::XCircle
103 }
104
105 fn menu_text(&self) -> String {
106 self.description()
107 }
108
109 fn requires_argument(&self) -> bool {
110 false
111 }
112
113 fn accepts_arguments(&self) -> bool {
114 true
115 }
116
117 fn complete_argument(
118 self: Arc<Self>,
119 arguments: &[String],
120 cancellation_flag: Arc<AtomicBool>,
121 workspace: Option<WeakView<Workspace>>,
122 cx: &mut WindowContext,
123 ) -> Task<Result<Vec<ArgumentCompletion>>> {
124 let Some(workspace) = workspace.and_then(|workspace| workspace.upgrade()) else {
125 return Task::ready(Err(anyhow!("workspace was dropped")));
126 };
127 let query = arguments.last().cloned().unwrap_or_default();
128
129 let paths = self.search_paths(query.clone(), cancellation_flag.clone(), &workspace, cx);
130 let executor = cx.background_executor().clone();
131 cx.background_executor().spawn(async move {
132 let mut matches: Vec<String> = paths
133 .await
134 .into_iter()
135 .map(|path_match| {
136 format!(
137 "{}{}",
138 path_match.path_prefix,
139 path_match.path.to_string_lossy()
140 )
141 })
142 .collect();
143
144 matches.extend(
145 fuzzy::match_strings(
146 &Options::match_candidates_for_args(),
147 &query,
148 false,
149 10,
150 &cancellation_flag,
151 executor,
152 )
153 .await
154 .into_iter()
155 .map(|candidate| candidate.string),
156 );
157
158 Ok(matches
159 .into_iter()
160 .map(|completion| ArgumentCompletion {
161 label: completion.clone().into(),
162 new_text: completion,
163 after_completion: assistant_slash_command::AfterCompletion::Run,
164 replace_previous_arguments: false,
165 })
166 .collect())
167 })
168 }
169
170 fn run(
171 self: Arc<Self>,
172 arguments: &[String],
173 _context_slash_command_output_sections: &[SlashCommandOutputSection<language::Anchor>],
174 _context_buffer: BufferSnapshot,
175 workspace: WeakView<Workspace>,
176 _delegate: Option<Arc<dyn LspAdapterDelegate>>,
177 cx: &mut WindowContext,
178 ) -> Task<SlashCommandResult> {
179 let Some(workspace) = workspace.upgrade() else {
180 return Task::ready(Err(anyhow!("workspace was dropped")));
181 };
182
183 let options = Options::parse(arguments);
184
185 let task = collect_diagnostics(workspace.read(cx).project().clone(), options, cx);
186
187 cx.spawn(move |_| async move {
188 task.await?
189 .map(|output| output.to_event_stream())
190 .ok_or_else(|| anyhow!("No diagnostics found"))
191 })
192 }
193}
194
195#[derive(Default)]
196struct Options {
197 include_warnings: bool,
198 path_matcher: Option<PathMatcher>,
199}
200
201const INCLUDE_WARNINGS_ARGUMENT: &str = "--include-warnings";
202
203impl Options {
204 fn parse(arguments: &[String]) -> Self {
205 let mut include_warnings = false;
206 let mut path_matcher = None;
207 for arg in arguments {
208 if arg == INCLUDE_WARNINGS_ARGUMENT {
209 include_warnings = true;
210 } else {
211 path_matcher = PathMatcher::new(&[arg.to_owned()]).log_err();
212 }
213 }
214 Self {
215 include_warnings,
216 path_matcher,
217 }
218 }
219
220 fn match_candidates_for_args() -> [StringMatchCandidate; 1] {
221 [StringMatchCandidate::new(0, INCLUDE_WARNINGS_ARGUMENT)]
222 }
223}
224
225fn collect_diagnostics(
226 project: Model<Project>,
227 options: Options,
228 cx: &mut AppContext,
229) -> Task<Result<Option<SlashCommandOutput>>> {
230 let error_source = if let Some(path_matcher) = &options.path_matcher {
231 debug_assert_eq!(path_matcher.sources().len(), 1);
232 Some(path_matcher.sources().first().cloned().unwrap_or_default())
233 } else {
234 None
235 };
236
237 let glob_is_exact_file_match = if let Some(path) = options
238 .path_matcher
239 .as_ref()
240 .and_then(|pm| pm.sources().first())
241 {
242 PathBuf::try_from(path)
243 .ok()
244 .and_then(|path| {
245 project.read(cx).worktrees(cx).find_map(|worktree| {
246 let worktree = worktree.read(cx);
247 let worktree_root_path = Path::new(worktree.root_name());
248 let relative_path = path.strip_prefix(worktree_root_path).ok()?;
249 worktree.absolutize(&relative_path).ok()
250 })
251 })
252 .is_some()
253 } else {
254 false
255 };
256
257 let project_handle = project.downgrade();
258 let diagnostic_summaries: Vec<_> = project
259 .read(cx)
260 .diagnostic_summaries(false, cx)
261 .flat_map(|(path, _, summary)| {
262 let worktree = project.read(cx).worktree_for_id(path.worktree_id, cx)?;
263 let mut path_buf = PathBuf::from(worktree.read(cx).root_name());
264 path_buf.push(&path.path);
265 Some((path, path_buf, summary))
266 })
267 .collect();
268
269 cx.spawn(|mut cx| async move {
270 let mut output = SlashCommandOutput::default();
271
272 if let Some(error_source) = error_source.as_ref() {
273 writeln!(output.text, "diagnostics: {}", error_source).unwrap();
274 } else {
275 writeln!(output.text, "diagnostics").unwrap();
276 }
277
278 let mut project_summary = DiagnosticSummary::default();
279 for (project_path, path, summary) in diagnostic_summaries {
280 if let Some(path_matcher) = &options.path_matcher {
281 if !path_matcher.is_match(&path) {
282 continue;
283 }
284 }
285
286 project_summary.error_count += summary.error_count;
287 if options.include_warnings {
288 project_summary.warning_count += summary.warning_count;
289 } else if summary.error_count == 0 {
290 continue;
291 }
292
293 let last_end = output.text.len();
294 let file_path = path.to_string_lossy().to_string();
295 if !glob_is_exact_file_match {
296 writeln!(&mut output.text, "{file_path}").unwrap();
297 }
298
299 if let Some(buffer) = project_handle
300 .update(&mut cx, |project, cx| project.open_buffer(project_path, cx))?
301 .await
302 .log_err()
303 {
304 let snapshot = cx.read_model(&buffer, |buffer, _| buffer.snapshot())?;
305 collect_buffer_diagnostics(&mut output, &snapshot, options.include_warnings);
306 }
307
308 if !glob_is_exact_file_match {
309 output.sections.push(SlashCommandOutputSection {
310 range: last_end..output.text.len().saturating_sub(1),
311 icon: IconName::File,
312 label: file_path.into(),
313 metadata: None,
314 });
315 }
316 }
317
318 // No diagnostics found
319 if output.sections.is_empty() {
320 return Ok(None);
321 }
322
323 let mut label = String::new();
324 label.push_str("Diagnostics");
325 if let Some(source) = error_source {
326 write!(label, " ({})", source).unwrap();
327 }
328
329 if project_summary.error_count > 0 || project_summary.warning_count > 0 {
330 label.push(':');
331
332 if project_summary.error_count > 0 {
333 write!(label, " {} errors", project_summary.error_count).unwrap();
334 if project_summary.warning_count > 0 {
335 label.push_str(",");
336 }
337 }
338
339 if project_summary.warning_count > 0 {
340 write!(label, " {} warnings", project_summary.warning_count).unwrap();
341 }
342 }
343
344 output.sections.insert(
345 0,
346 SlashCommandOutputSection {
347 range: 0..output.text.len(),
348 icon: IconName::Warning,
349 label: label.into(),
350 metadata: None,
351 },
352 );
353
354 Ok(Some(output))
355 })
356}
357
358pub fn collect_buffer_diagnostics(
359 output: &mut SlashCommandOutput,
360 snapshot: &BufferSnapshot,
361 include_warnings: bool,
362) {
363 for (_, group) in snapshot.diagnostic_groups(None) {
364 let entry = &group.entries[group.primary_ix];
365 collect_diagnostic(output, entry, &snapshot, include_warnings)
366 }
367}
368
369fn collect_diagnostic(
370 output: &mut SlashCommandOutput,
371 entry: &DiagnosticEntry<Anchor>,
372 snapshot: &BufferSnapshot,
373 include_warnings: bool,
374) {
375 const EXCERPT_EXPANSION_SIZE: u32 = 2;
376 const MAX_MESSAGE_LENGTH: usize = 2000;
377
378 let (ty, icon) = match entry.diagnostic.severity {
379 DiagnosticSeverity::WARNING => {
380 if !include_warnings {
381 return;
382 }
383 ("warning", IconName::Warning)
384 }
385 DiagnosticSeverity::ERROR => ("error", IconName::XCircle),
386 _ => return,
387 };
388 let prev_len = output.text.len();
389
390 let range = entry.range.to_point(snapshot);
391 let diagnostic_row_number = range.start.row + 1;
392
393 let start_row = range.start.row.saturating_sub(EXCERPT_EXPANSION_SIZE);
394 let end_row = (range.end.row + EXCERPT_EXPANSION_SIZE).min(snapshot.max_point().row) + 1;
395 let excerpt_range =
396 Point::new(start_row, 0).to_offset(&snapshot)..Point::new(end_row, 0).to_offset(&snapshot);
397
398 output.text.push_str("```");
399 if let Some(language_name) = snapshot.language().map(|l| l.code_fence_block_name()) {
400 output.text.push_str(&language_name);
401 }
402 output.text.push('\n');
403
404 let mut buffer_text = String::new();
405 for chunk in snapshot.text_for_range(excerpt_range) {
406 buffer_text.push_str(chunk);
407 }
408
409 for (i, line) in buffer_text.lines().enumerate() {
410 let line_number = start_row + i as u32 + 1;
411 writeln!(output.text, "{}", line).unwrap();
412
413 if line_number == diagnostic_row_number {
414 output.text.push_str("//");
415 let prev_len = output.text.len();
416 write!(output.text, " {}: ", ty).unwrap();
417 let padding = output.text.len() - prev_len;
418
419 let message = util::truncate(&entry.diagnostic.message, MAX_MESSAGE_LENGTH)
420 .replace('\n', format!("\n//{:padding$}", "").as_str());
421
422 writeln!(output.text, "{message}").unwrap();
423 }
424 }
425
426 writeln!(output.text, "```").unwrap();
427 output.sections.push(SlashCommandOutputSection {
428 range: prev_len..output.text.len().saturating_sub(1),
429 icon,
430 label: entry.diagnostic.message.clone().into(),
431 metadata: None,
432 });
433}