1pub mod items;
2mod toolbar_controls;
3
4mod diagnostic_renderer;
5
6#[cfg(test)]
7mod diagnostics_tests;
8
9use anyhow::Result;
10use collections::{BTreeSet, HashMap};
11use diagnostic_renderer::DiagnosticBlock;
12use editor::{
13 DEFAULT_MULTIBUFFER_CONTEXT, Editor, EditorEvent, ExcerptRange, MultiBuffer, PathKey,
14 display_map::{BlockPlacement, BlockProperties, BlockStyle, CustomBlockId},
15};
16use futures::future::join_all;
17use gpui::{
18 AnyElement, AnyView, App, AsyncApp, Context, Entity, EventEmitter, FocusHandle, Focusable,
19 Global, InteractiveElement, IntoElement, ParentElement, Render, SharedString, Styled,
20 Subscription, Task, WeakEntity, Window, actions, div,
21};
22use language::{
23 Bias, Buffer, BufferRow, BufferSnapshot, DiagnosticEntry, Point, ToTreeSitterPoint,
24};
25use project::{
26 DiagnosticSummary, Project, ProjectPath,
27 lsp_store::rust_analyzer_ext::{cancel_flycheck, run_flycheck},
28 project_settings::{DiagnosticSeverity, ProjectSettings},
29};
30use settings::Settings;
31use std::{
32 any::{Any, TypeId},
33 cmp::{self, Ordering},
34 ops::{Range, RangeInclusive},
35 sync::Arc,
36 time::Duration,
37};
38use text::{BufferId, OffsetRangeExt};
39use theme::ActiveTheme;
40pub use toolbar_controls::ToolbarControls;
41use ui::{Icon, IconName, Label, h_flex, prelude::*};
42use util::ResultExt;
43use workspace::{
44 ItemNavHistory, ToolbarItemLocation, Workspace,
45 item::{BreadcrumbText, Item, ItemEvent, ItemHandle, SaveOptions, TabContentParams},
46 searchable::SearchableItemHandle,
47};
48
49actions!(
50 diagnostics,
51 [
52 /// Opens the project diagnostics view.
53 Deploy,
54 /// Toggles the display of warning-level diagnostics.
55 ToggleWarnings,
56 /// Toggles automatic refresh of diagnostics.
57 ToggleDiagnosticsRefresh
58 ]
59);
60
61#[derive(Default)]
62pub(crate) struct IncludeWarnings(bool);
63impl Global for IncludeWarnings {}
64
65pub fn init(cx: &mut App) {
66 editor::set_diagnostic_renderer(diagnostic_renderer::DiagnosticRenderer {}, cx);
67 cx.observe_new(ProjectDiagnosticsEditor::register).detach();
68}
69
70pub(crate) struct ProjectDiagnosticsEditor {
71 project: Entity<Project>,
72 workspace: WeakEntity<Workspace>,
73 focus_handle: FocusHandle,
74 editor: Entity<Editor>,
75 diagnostics: HashMap<BufferId, Vec<DiagnosticEntry<text::Anchor>>>,
76 blocks: HashMap<BufferId, Vec<CustomBlockId>>,
77 summary: DiagnosticSummary,
78 multibuffer: Entity<MultiBuffer>,
79 paths_to_update: BTreeSet<ProjectPath>,
80 include_warnings: bool,
81 update_excerpts_task: Option<Task<Result<()>>>,
82 cargo_diagnostics_fetch: CargoDiagnosticsFetchState,
83 diagnostic_summary_update: Task<()>,
84 _subscription: Subscription,
85}
86
87struct CargoDiagnosticsFetchState {
88 fetch_task: Option<Task<()>>,
89 cancel_task: Option<Task<()>>,
90 diagnostic_sources: Arc<Vec<ProjectPath>>,
91}
92
93impl EventEmitter<EditorEvent> for ProjectDiagnosticsEditor {}
94
95const DIAGNOSTICS_UPDATE_DELAY: Duration = Duration::from_millis(50);
96
97impl Render for ProjectDiagnosticsEditor {
98 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
99 let warning_count = if self.include_warnings {
100 self.summary.warning_count
101 } else {
102 0
103 };
104
105 let child = if warning_count + self.summary.error_count == 0 {
106 let label = if self.summary.warning_count == 0 {
107 SharedString::new_static("No problems in workspace")
108 } else {
109 SharedString::new_static("No errors in workspace")
110 };
111 v_flex()
112 .key_context("EmptyPane")
113 .size_full()
114 .gap_1()
115 .justify_center()
116 .items_center()
117 .text_center()
118 .bg(cx.theme().colors().editor_background)
119 .child(Label::new(label).color(Color::Muted))
120 .when(self.summary.warning_count > 0, |this| {
121 let plural_suffix = if self.summary.warning_count > 1 {
122 "s"
123 } else {
124 ""
125 };
126 let label = format!(
127 "Show {} warning{}",
128 self.summary.warning_count, plural_suffix
129 );
130 this.child(
131 Button::new("diagnostics-show-warning-label", label).on_click(cx.listener(
132 |this, _, window, cx| {
133 this.toggle_warnings(&Default::default(), window, cx);
134 cx.notify();
135 },
136 )),
137 )
138 })
139 } else {
140 div().size_full().child(self.editor.clone())
141 };
142
143 div()
144 .key_context("Diagnostics")
145 .track_focus(&self.focus_handle(cx))
146 .size_full()
147 .on_action(cx.listener(Self::toggle_warnings))
148 .on_action(cx.listener(Self::toggle_diagnostics_refresh))
149 .child(child)
150 }
151}
152
153impl ProjectDiagnosticsEditor {
154 fn register(
155 workspace: &mut Workspace,
156 _window: Option<&mut Window>,
157 _: &mut Context<Workspace>,
158 ) {
159 workspace.register_action(Self::deploy);
160 }
161
162 fn new(
163 include_warnings: bool,
164 project_handle: Entity<Project>,
165 workspace: WeakEntity<Workspace>,
166 window: &mut Window,
167 cx: &mut Context<Self>,
168 ) -> Self {
169 let project_event_subscription =
170 cx.subscribe_in(&project_handle, window, |this, project, event, window, cx| match event {
171 project::Event::DiskBasedDiagnosticsStarted { .. } => {
172 cx.notify();
173 }
174 project::Event::DiskBasedDiagnosticsFinished { language_server_id } => {
175 log::debug!("disk based diagnostics finished for server {language_server_id}");
176 this.update_stale_excerpts(window, cx);
177 }
178 project::Event::DiagnosticsUpdated {
179 language_server_id,
180 paths,
181 } => {
182 this.paths_to_update.extend(paths.clone());
183 let project = project.clone();
184 this.diagnostic_summary_update = cx.spawn(async move |this, cx| {
185 cx.background_executor()
186 .timer(Duration::from_millis(30))
187 .await;
188 this.update(cx, |this, cx| {
189 this.summary = project.read(cx).diagnostic_summary(false, cx);
190 })
191 .log_err();
192 });
193 cx.emit(EditorEvent::TitleChanged);
194
195 log::debug!("diagnostics updated for server {language_server_id}, paths {paths:?}. updating excerpts");
196 this.update_stale_excerpts(window, cx);
197 }
198 _ => {}
199 });
200
201 let focus_handle = cx.focus_handle();
202 cx.on_focus_in(&focus_handle, window, |this, window, cx| {
203 this.focus_in(window, cx)
204 })
205 .detach();
206 cx.on_focus_out(&focus_handle, window, |this, _event, window, cx| {
207 this.focus_out(window, cx)
208 })
209 .detach();
210
211 let excerpts = cx.new(|cx| MultiBuffer::new(project_handle.read(cx).capability()));
212 let editor = cx.new(|cx| {
213 let mut editor =
214 Editor::for_multibuffer(excerpts.clone(), Some(project_handle.clone()), window, cx);
215 editor.set_vertical_scroll_margin(5, cx);
216 editor.disable_inline_diagnostics();
217 editor.set_max_diagnostics_severity(
218 if include_warnings {
219 DiagnosticSeverity::Warning
220 } else {
221 DiagnosticSeverity::Error
222 },
223 cx,
224 );
225 editor.set_all_diagnostics_active(cx);
226 editor
227 });
228 cx.subscribe_in(
229 &editor,
230 window,
231 |this, _editor, event: &EditorEvent, window, cx| {
232 cx.emit(event.clone());
233 match event {
234 EditorEvent::Focused => {
235 if this.multibuffer.read(cx).is_empty() {
236 window.focus(&this.focus_handle);
237 }
238 }
239 EditorEvent::Blurred => this.update_stale_excerpts(window, cx),
240 EditorEvent::Saved => this.update_all_excerpts(window, cx),
241 _ => {}
242 }
243 },
244 )
245 .detach();
246 cx.observe_global_in::<IncludeWarnings>(window, |this, window, cx| {
247 let include_warnings = cx.global::<IncludeWarnings>().0;
248 this.include_warnings = include_warnings;
249 this.editor.update(cx, |editor, cx| {
250 editor.set_max_diagnostics_severity(
251 if include_warnings {
252 DiagnosticSeverity::Warning
253 } else {
254 DiagnosticSeverity::Error
255 },
256 cx,
257 )
258 });
259 this.diagnostics.clear();
260 this.update_all_diagnostics(false, window, cx);
261 })
262 .detach();
263 cx.observe_release(&cx.entity(), |editor, _, cx| {
264 editor.stop_cargo_diagnostics_fetch(cx);
265 })
266 .detach();
267
268 let project = project_handle.read(cx);
269 let mut this = Self {
270 project: project_handle.clone(),
271 summary: project.diagnostic_summary(false, cx),
272 diagnostics: Default::default(),
273 blocks: Default::default(),
274 include_warnings,
275 workspace,
276 multibuffer: excerpts,
277 focus_handle,
278 editor,
279 paths_to_update: Default::default(),
280 update_excerpts_task: None,
281 cargo_diagnostics_fetch: CargoDiagnosticsFetchState {
282 fetch_task: None,
283 cancel_task: None,
284 diagnostic_sources: Arc::new(Vec::new()),
285 },
286 diagnostic_summary_update: Task::ready(()),
287 _subscription: project_event_subscription,
288 };
289 this.update_all_diagnostics(true, window, cx);
290 this
291 }
292
293 fn update_stale_excerpts(&mut self, window: &mut Window, cx: &mut Context<Self>) {
294 if self.update_excerpts_task.is_some() {
295 return;
296 }
297
298 let project_handle = self.project.clone();
299 self.update_excerpts_task = Some(cx.spawn_in(window, async move |this, cx| {
300 cx.background_executor()
301 .timer(DIAGNOSTICS_UPDATE_DELAY)
302 .await;
303 loop {
304 let Some(path) = this.update(cx, |this, cx| {
305 let Some(path) = this.paths_to_update.pop_first() else {
306 this.update_excerpts_task = None;
307 cx.notify();
308 return None;
309 };
310 Some(path)
311 })?
312 else {
313 break;
314 };
315
316 if let Some(buffer) = project_handle
317 .update(cx, |project, cx| project.open_buffer(path.clone(), cx))?
318 .await
319 .log_err()
320 {
321 this.update_in(cx, |this, window, cx| {
322 this.update_excerpts(buffer, window, cx)
323 })?
324 .await?;
325 }
326 }
327 Ok(())
328 }));
329 }
330
331 fn deploy(
332 workspace: &mut Workspace,
333 _: &Deploy,
334 window: &mut Window,
335 cx: &mut Context<Workspace>,
336 ) {
337 if let Some(existing) = workspace.item_of_type::<ProjectDiagnosticsEditor>(cx) {
338 let is_active = workspace
339 .active_item(cx)
340 .is_some_and(|item| item.item_id() == existing.item_id());
341 workspace.activate_item(&existing, true, !is_active, window, cx);
342 } else {
343 let workspace_handle = cx.entity().downgrade();
344
345 let include_warnings = match cx.try_global::<IncludeWarnings>() {
346 Some(include_warnings) => include_warnings.0,
347 None => ProjectSettings::get_global(cx).diagnostics.include_warnings,
348 };
349
350 let diagnostics = cx.new(|cx| {
351 ProjectDiagnosticsEditor::new(
352 include_warnings,
353 workspace.project().clone(),
354 workspace_handle,
355 window,
356 cx,
357 )
358 });
359 workspace.add_item_to_active_pane(Box::new(diagnostics), None, true, window, cx);
360 }
361 }
362
363 fn toggle_warnings(&mut self, _: &ToggleWarnings, _: &mut Window, cx: &mut Context<Self>) {
364 cx.set_global(IncludeWarnings(!self.include_warnings));
365 }
366
367 fn toggle_diagnostics_refresh(
368 &mut self,
369 _: &ToggleDiagnosticsRefresh,
370 window: &mut Window,
371 cx: &mut Context<Self>,
372 ) {
373 let fetch_cargo_diagnostics = ProjectSettings::get_global(cx)
374 .diagnostics
375 .fetch_cargo_diagnostics();
376
377 if fetch_cargo_diagnostics {
378 if self.cargo_diagnostics_fetch.fetch_task.is_some() {
379 self.stop_cargo_diagnostics_fetch(cx);
380 } else {
381 self.update_all_diagnostics(false, window, cx);
382 }
383 } else {
384 if self.update_excerpts_task.is_some() {
385 self.update_excerpts_task = None;
386 } else {
387 self.update_all_diagnostics(false, window, cx);
388 }
389 }
390 cx.notify();
391 }
392
393 fn focus_in(&mut self, window: &mut Window, cx: &mut Context<Self>) {
394 if self.focus_handle.is_focused(window) && !self.multibuffer.read(cx).is_empty() {
395 self.editor.focus_handle(cx).focus(window)
396 }
397 }
398
399 fn focus_out(&mut self, window: &mut Window, cx: &mut Context<Self>) {
400 if !self.focus_handle.is_focused(window) && !self.editor.focus_handle(cx).is_focused(window)
401 {
402 self.update_stale_excerpts(window, cx);
403 }
404 }
405
406 fn update_all_diagnostics(
407 &mut self,
408 first_launch: bool,
409 window: &mut Window,
410 cx: &mut Context<Self>,
411 ) {
412 let cargo_diagnostics_sources = self.cargo_diagnostics_sources(cx);
413 if cargo_diagnostics_sources.is_empty() {
414 self.update_all_excerpts(window, cx);
415 } else if first_launch && !self.summary.is_empty() {
416 self.update_all_excerpts(window, cx);
417 } else {
418 self.fetch_cargo_diagnostics(Arc::new(cargo_diagnostics_sources), cx);
419 }
420 }
421
422 fn fetch_cargo_diagnostics(
423 &mut self,
424 diagnostics_sources: Arc<Vec<ProjectPath>>,
425 cx: &mut Context<Self>,
426 ) {
427 let project = self.project.clone();
428 self.cargo_diagnostics_fetch.cancel_task = None;
429 self.cargo_diagnostics_fetch.fetch_task = None;
430 self.cargo_diagnostics_fetch.diagnostic_sources = diagnostics_sources.clone();
431 if self.cargo_diagnostics_fetch.diagnostic_sources.is_empty() {
432 return;
433 }
434
435 self.cargo_diagnostics_fetch.fetch_task = Some(cx.spawn(async move |editor, cx| {
436 let mut fetch_tasks = Vec::new();
437 for buffer_path in diagnostics_sources.iter().cloned() {
438 if cx
439 .update(|cx| {
440 fetch_tasks.push(run_flycheck(project.clone(), buffer_path, cx));
441 })
442 .is_err()
443 {
444 break;
445 }
446 }
447
448 let _ = join_all(fetch_tasks).await;
449 editor
450 .update(cx, |editor, _| {
451 editor.cargo_diagnostics_fetch.fetch_task = None;
452 })
453 .ok();
454 }));
455 }
456
457 fn stop_cargo_diagnostics_fetch(&mut self, cx: &mut App) {
458 self.cargo_diagnostics_fetch.fetch_task = None;
459 let mut cancel_gasks = Vec::new();
460 for buffer_path in std::mem::take(&mut self.cargo_diagnostics_fetch.diagnostic_sources)
461 .iter()
462 .cloned()
463 {
464 cancel_gasks.push(cancel_flycheck(self.project.clone(), buffer_path, cx));
465 }
466
467 self.cargo_diagnostics_fetch.cancel_task = Some(cx.background_spawn(async move {
468 let _ = join_all(cancel_gasks).await;
469 log::info!("Finished fetching cargo diagnostics");
470 }));
471 }
472
473 /// Enqueue an update of all excerpts. Updates all paths that either
474 /// currently have diagnostics or are currently present in this view.
475 fn update_all_excerpts(&mut self, window: &mut Window, cx: &mut Context<Self>) {
476 self.project.update(cx, |project, cx| {
477 let mut paths = project
478 .diagnostic_summaries(false, cx)
479 .map(|(path, _, _)| path)
480 .collect::<BTreeSet<_>>();
481 self.multibuffer.update(cx, |multibuffer, cx| {
482 for buffer in multibuffer.all_buffers() {
483 if let Some(file) = buffer.read(cx).file() {
484 paths.insert(ProjectPath {
485 path: file.path().clone(),
486 worktree_id: file.worktree_id(cx),
487 });
488 }
489 }
490 });
491 self.paths_to_update = paths;
492 });
493 self.update_stale_excerpts(window, cx);
494 }
495
496 fn diagnostics_are_unchanged(
497 &self,
498 existing: &Vec<DiagnosticEntry<text::Anchor>>,
499 new: &Vec<DiagnosticEntry<text::Anchor>>,
500 snapshot: &BufferSnapshot,
501 ) -> bool {
502 if existing.len() != new.len() {
503 return false;
504 }
505 existing.iter().zip(new.iter()).all(|(existing, new)| {
506 existing.diagnostic.message == new.diagnostic.message
507 && existing.diagnostic.severity == new.diagnostic.severity
508 && existing.diagnostic.is_primary == new.diagnostic.is_primary
509 && existing.range.to_offset(snapshot) == new.range.to_offset(snapshot)
510 })
511 }
512
513 fn update_excerpts(
514 &mut self,
515 buffer: Entity<Buffer>,
516 window: &mut Window,
517 cx: &mut Context<Self>,
518 ) -> Task<Result<()>> {
519 let was_empty = self.multibuffer.read(cx).is_empty();
520 let buffer_ = buffer.read(cx);
521 let is_dirty = buffer_.is_dirty();
522 let buffer_snapshot = buffer_.snapshot();
523
524 let buffer_id = buffer_snapshot.remote_id();
525 let max_severity = if self.include_warnings {
526 lsp::DiagnosticSeverity::WARNING
527 } else {
528 lsp::DiagnosticSeverity::ERROR
529 };
530
531 cx.spawn_in(window, async move |this, cx| {
532 let diagnostics = buffer_snapshot
533 .diagnostics_in_range::<_, text::Anchor>(
534 Point::zero()..buffer_snapshot.max_point(),
535 false,
536 )
537 .collect::<Vec<_>>();
538 let unchanged = this.update(cx, |this, _| {
539 if this.diagnostics.get(&buffer_id).is_some_and(|existing| {
540 this.diagnostics_are_unchanged(existing, &diagnostics, &buffer_snapshot)
541 }) {
542 return true;
543 }
544 this.diagnostics.insert(buffer_id, diagnostics.clone());
545 return false;
546 })?;
547 if unchanged {
548 return Ok(());
549 }
550
551 let mut grouped: HashMap<usize, Vec<_>> = HashMap::default();
552 for entry in diagnostics {
553 grouped
554 .entry(entry.diagnostic.group_id)
555 .or_default()
556 .push(DiagnosticEntry {
557 range: entry.range.to_point(&buffer_snapshot),
558 diagnostic: entry.diagnostic,
559 })
560 }
561 let mut blocks: Vec<DiagnosticBlock> = Vec::new();
562
563 for (_, group) in grouped {
564 let group_severity = group.iter().map(|d| d.diagnostic.severity).min();
565 if group_severity.is_none_or(|s| s > max_severity) {
566 continue;
567 }
568 let more = cx.update(|_, cx| {
569 crate::diagnostic_renderer::DiagnosticRenderer::diagnostic_blocks_for_group(
570 group,
571 buffer_snapshot.remote_id(),
572 Some(this.clone()),
573 cx,
574 )
575 })?;
576
577 for item in more {
578 let i = blocks
579 .binary_search_by(|probe| {
580 probe
581 .initial_range
582 .start
583 .cmp(&item.initial_range.start)
584 .then(probe.initial_range.end.cmp(&item.initial_range.end))
585 .then(Ordering::Greater)
586 })
587 .unwrap_or_else(|i| i);
588 blocks.insert(i, item);
589 }
590 }
591
592 let mut excerpt_ranges: Vec<ExcerptRange<Point>> = Vec::new();
593 for b in blocks.iter() {
594 let excerpt_range = context_range_for_entry(
595 b.initial_range.clone(),
596 DEFAULT_MULTIBUFFER_CONTEXT,
597 buffer_snapshot.clone(),
598 cx,
599 )
600 .await;
601 let i = excerpt_ranges
602 .binary_search_by(|probe| {
603 probe
604 .context
605 .start
606 .cmp(&excerpt_range.start)
607 .then(probe.context.end.cmp(&excerpt_range.end))
608 .then(probe.primary.start.cmp(&b.initial_range.start))
609 .then(probe.primary.end.cmp(&b.initial_range.end))
610 .then(cmp::Ordering::Greater)
611 })
612 .unwrap_or_else(|i| i);
613 excerpt_ranges.insert(
614 i,
615 ExcerptRange {
616 context: excerpt_range,
617 primary: b.initial_range.clone(),
618 },
619 )
620 }
621
622 this.update_in(cx, |this, window, cx| {
623 if let Some(block_ids) = this.blocks.remove(&buffer_id) {
624 this.editor.update(cx, |editor, cx| {
625 editor.display_map.update(cx, |display_map, cx| {
626 display_map.remove_blocks(block_ids.into_iter().collect(), cx)
627 });
628 })
629 }
630 let (anchor_ranges, _) = this.multibuffer.update(cx, |multi_buffer, cx| {
631 multi_buffer.set_excerpt_ranges_for_path(
632 PathKey::for_buffer(&buffer, cx),
633 buffer.clone(),
634 &buffer_snapshot,
635 excerpt_ranges,
636 is_dirty,
637 cx,
638 )
639 });
640 #[cfg(test)]
641 let cloned_blocks = blocks.clone();
642
643 if was_empty {
644 if let Some(anchor_range) = anchor_ranges.first() {
645 let range_to_select = anchor_range.start..anchor_range.start;
646 this.editor.update(cx, |editor, cx| {
647 editor.change_selections(Default::default(), window, cx, |s| {
648 s.select_anchor_ranges([range_to_select]);
649 })
650 });
651 if this.focus_handle.is_focused(window) {
652 this.editor.read(cx).focus_handle(cx).focus(window);
653 }
654 }
655 }
656
657 let editor_blocks =
658 anchor_ranges
659 .into_iter()
660 .zip(blocks.into_iter())
661 .map(|(anchor, block)| {
662 let editor = this.editor.downgrade();
663 BlockProperties {
664 placement: BlockPlacement::Near(anchor.start),
665 height: Some(1),
666 style: BlockStyle::Flex,
667 render: Arc::new(move |bcx| {
668 block.render_block(editor.clone(), bcx)
669 }),
670 priority: 1,
671 }
672 });
673 let block_ids = this.editor.update(cx, |editor, cx| {
674 editor.display_map.update(cx, |display_map, cx| {
675 display_map.insert_blocks(editor_blocks, cx)
676 })
677 });
678
679 #[cfg(test)]
680 {
681 for (block_id, block) in block_ids.iter().zip(cloned_blocks.iter()) {
682 let markdown = block.markdown.clone();
683 editor::test::set_block_content_for_tests(
684 &this.editor,
685 *block_id,
686 cx,
687 move |cx| {
688 markdown::MarkdownElement::rendered_text(
689 markdown.clone(),
690 cx,
691 editor::hover_popover::diagnostics_markdown_style,
692 )
693 },
694 );
695 }
696 }
697
698 this.blocks.insert(buffer_id, block_ids);
699 cx.notify()
700 })
701 })
702 }
703
704 pub fn cargo_diagnostics_sources(&self, cx: &App) -> Vec<ProjectPath> {
705 let fetch_cargo_diagnostics = ProjectSettings::get_global(cx)
706 .diagnostics
707 .fetch_cargo_diagnostics();
708 if !fetch_cargo_diagnostics {
709 return Vec::new();
710 }
711 self.project
712 .read(cx)
713 .worktrees(cx)
714 .filter_map(|worktree| {
715 let _cargo_toml_entry = worktree.read(cx).entry_for_path("Cargo.toml")?;
716 let rust_file_entry = worktree.read(cx).entries(false, 0).find(|entry| {
717 entry
718 .path
719 .extension()
720 .and_then(|extension| extension.to_str())
721 == Some("rs")
722 })?;
723 self.project.read(cx).path_for_entry(rust_file_entry.id, cx)
724 })
725 .collect()
726 }
727}
728
729impl Focusable for ProjectDiagnosticsEditor {
730 fn focus_handle(&self, _: &App) -> FocusHandle {
731 self.focus_handle.clone()
732 }
733}
734
735impl Item for ProjectDiagnosticsEditor {
736 type Event = EditorEvent;
737
738 fn to_item_events(event: &EditorEvent, f: impl FnMut(ItemEvent)) {
739 Editor::to_item_events(event, f)
740 }
741
742 fn deactivated(&mut self, window: &mut Window, cx: &mut Context<Self>) {
743 self.editor
744 .update(cx, |editor, cx| editor.deactivated(window, cx));
745 }
746
747 fn navigate(
748 &mut self,
749 data: Box<dyn Any>,
750 window: &mut Window,
751 cx: &mut Context<Self>,
752 ) -> bool {
753 self.editor
754 .update(cx, |editor, cx| editor.navigate(data, window, cx))
755 }
756
757 fn tab_tooltip_text(&self, _: &App) -> Option<SharedString> {
758 Some("Project Diagnostics".into())
759 }
760
761 fn tab_content_text(&self, _detail: usize, _: &App) -> SharedString {
762 "Diagnostics".into()
763 }
764
765 fn tab_content(&self, params: TabContentParams, _window: &Window, _: &App) -> AnyElement {
766 h_flex()
767 .gap_1()
768 .when(
769 self.summary.error_count == 0 && self.summary.warning_count == 0,
770 |then| {
771 then.child(
772 h_flex()
773 .gap_1()
774 .child(Icon::new(IconName::Check).color(Color::Success))
775 .child(Label::new("No problems").color(params.text_color())),
776 )
777 },
778 )
779 .when(self.summary.error_count > 0, |then| {
780 then.child(
781 h_flex()
782 .gap_1()
783 .child(Icon::new(IconName::XCircle).color(Color::Error))
784 .child(
785 Label::new(self.summary.error_count.to_string())
786 .color(params.text_color()),
787 ),
788 )
789 })
790 .when(self.summary.warning_count > 0, |then| {
791 then.child(
792 h_flex()
793 .gap_1()
794 .child(Icon::new(IconName::Warning).color(Color::Warning))
795 .child(
796 Label::new(self.summary.warning_count.to_string())
797 .color(params.text_color()),
798 ),
799 )
800 })
801 .into_any_element()
802 }
803
804 fn telemetry_event_text(&self) -> Option<&'static str> {
805 Some("Project Diagnostics Opened")
806 }
807
808 fn for_each_project_item(
809 &self,
810 cx: &App,
811 f: &mut dyn FnMut(gpui::EntityId, &dyn project::ProjectItem),
812 ) {
813 self.editor.for_each_project_item(cx, f)
814 }
815
816 fn is_singleton(&self, _: &App) -> bool {
817 false
818 }
819
820 fn set_nav_history(
821 &mut self,
822 nav_history: ItemNavHistory,
823 _: &mut Window,
824 cx: &mut Context<Self>,
825 ) {
826 self.editor.update(cx, |editor, _| {
827 editor.set_nav_history(Some(nav_history));
828 });
829 }
830
831 fn clone_on_split(
832 &self,
833 _workspace_id: Option<workspace::WorkspaceId>,
834 window: &mut Window,
835 cx: &mut Context<Self>,
836 ) -> Option<Entity<Self>>
837 where
838 Self: Sized,
839 {
840 Some(cx.new(|cx| {
841 ProjectDiagnosticsEditor::new(
842 self.include_warnings,
843 self.project.clone(),
844 self.workspace.clone(),
845 window,
846 cx,
847 )
848 }))
849 }
850
851 fn is_dirty(&self, cx: &App) -> bool {
852 self.multibuffer.read(cx).is_dirty(cx)
853 }
854
855 fn has_deleted_file(&self, cx: &App) -> bool {
856 self.multibuffer.read(cx).has_deleted_file(cx)
857 }
858
859 fn has_conflict(&self, cx: &App) -> bool {
860 self.multibuffer.read(cx).has_conflict(cx)
861 }
862
863 fn can_save(&self, _: &App) -> bool {
864 true
865 }
866
867 fn save(
868 &mut self,
869 options: SaveOptions,
870 project: Entity<Project>,
871 window: &mut Window,
872 cx: &mut Context<Self>,
873 ) -> Task<Result<()>> {
874 self.editor.save(options, project, window, cx)
875 }
876
877 fn save_as(
878 &mut self,
879 _: Entity<Project>,
880 _: ProjectPath,
881 _window: &mut Window,
882 _: &mut Context<Self>,
883 ) -> Task<Result<()>> {
884 unreachable!()
885 }
886
887 fn reload(
888 &mut self,
889 project: Entity<Project>,
890 window: &mut Window,
891 cx: &mut Context<Self>,
892 ) -> Task<Result<()>> {
893 self.editor.reload(project, window, cx)
894 }
895
896 fn act_as_type<'a>(
897 &'a self,
898 type_id: TypeId,
899 self_handle: &'a Entity<Self>,
900 _: &'a App,
901 ) -> Option<AnyView> {
902 if type_id == TypeId::of::<Self>() {
903 Some(self_handle.to_any())
904 } else if type_id == TypeId::of::<Editor>() {
905 Some(self.editor.to_any())
906 } else {
907 None
908 }
909 }
910
911 fn as_searchable(&self, _: &Entity<Self>) -> Option<Box<dyn SearchableItemHandle>> {
912 Some(Box::new(self.editor.clone()))
913 }
914
915 fn breadcrumb_location(&self, _: &App) -> ToolbarItemLocation {
916 ToolbarItemLocation::PrimaryLeft
917 }
918
919 fn breadcrumbs(&self, theme: &theme::Theme, cx: &App) -> Option<Vec<BreadcrumbText>> {
920 self.editor.breadcrumbs(theme, cx)
921 }
922
923 fn added_to_workspace(
924 &mut self,
925 workspace: &mut Workspace,
926 window: &mut Window,
927 cx: &mut Context<Self>,
928 ) {
929 self.editor.update(cx, |editor, cx| {
930 editor.added_to_workspace(workspace, window, cx)
931 });
932 }
933}
934
935const DIAGNOSTIC_EXPANSION_ROW_LIMIT: u32 = 32;
936
937async fn context_range_for_entry(
938 range: Range<Point>,
939 context: u32,
940 snapshot: BufferSnapshot,
941 cx: &mut AsyncApp,
942) -> Range<Point> {
943 if let Some(rows) = heuristic_syntactic_expand(
944 range.clone(),
945 DIAGNOSTIC_EXPANSION_ROW_LIMIT,
946 snapshot.clone(),
947 cx,
948 )
949 .await
950 {
951 return Range {
952 start: Point::new(*rows.start(), 0),
953 end: snapshot.clip_point(Point::new(*rows.end(), u32::MAX), Bias::Left),
954 };
955 }
956 Range {
957 start: Point::new(range.start.row.saturating_sub(context), 0),
958 end: snapshot.clip_point(Point::new(range.end.row + context, u32::MAX), Bias::Left),
959 }
960}
961
962/// Expands the input range using syntax information from TreeSitter. This expansion will be limited
963/// to the specified `max_row_count`.
964///
965/// If there is a containing outline item that is less than `max_row_count`, it will be returned.
966/// Otherwise fairly arbitrary heuristics are applied to attempt to return a logical block of code.
967async fn heuristic_syntactic_expand(
968 input_range: Range<Point>,
969 max_row_count: u32,
970 snapshot: BufferSnapshot,
971 cx: &mut AsyncApp,
972) -> Option<RangeInclusive<BufferRow>> {
973 let input_row_count = input_range.end.row - input_range.start.row;
974 if input_row_count > max_row_count {
975 return None;
976 }
977
978 // If the outline node contains the diagnostic and is small enough, just use that.
979 let outline_range = snapshot.outline_range_containing(input_range.clone());
980 if let Some(outline_range) = outline_range.clone() {
981 // Remove blank lines from start and end
982 if let Some(start_row) = (outline_range.start.row..outline_range.end.row)
983 .find(|row| !snapshot.line_indent_for_row(*row).is_line_blank())
984 {
985 if let Some(end_row) = (outline_range.start.row..outline_range.end.row + 1)
986 .rev()
987 .find(|row| !snapshot.line_indent_for_row(*row).is_line_blank())
988 {
989 let row_count = end_row.saturating_sub(start_row);
990 if row_count <= max_row_count {
991 return Some(RangeInclusive::new(
992 outline_range.start.row,
993 outline_range.end.row,
994 ));
995 }
996 }
997 }
998 }
999
1000 let mut node = snapshot.syntax_ancestor(input_range.clone())?;
1001
1002 loop {
1003 let node_start = Point::from_ts_point(node.start_position());
1004 let node_end = Point::from_ts_point(node.end_position());
1005 let node_range = node_start..node_end;
1006 let row_count = node_end.row - node_start.row + 1;
1007 let mut ancestor_range = None;
1008 let reached_outline_node = cx.background_executor().scoped({
1009 let node_range = node_range.clone();
1010 let outline_range = outline_range.clone();
1011 let ancestor_range = &mut ancestor_range;
1012 |scope| {scope.spawn(async move {
1013 // Stop if we've exceeded the row count or reached an outline node. Then, find the interval
1014 // of node children which contains the query range. For example, this allows just returning
1015 // the header of a declaration rather than the entire declaration.
1016 if row_count > max_row_count || outline_range == Some(node_range.clone()) {
1017 let mut cursor = node.walk();
1018 let mut included_child_start = None;
1019 let mut included_child_end = None;
1020 let mut previous_end = node_start;
1021 if cursor.goto_first_child() {
1022 loop {
1023 let child_node = cursor.node();
1024 let child_range = previous_end..Point::from_ts_point(child_node.end_position());
1025 if included_child_start.is_none() && child_range.contains(&input_range.start) {
1026 included_child_start = Some(child_range.start);
1027 }
1028 if child_range.contains(&input_range.end) {
1029 included_child_end = Some(child_range.end);
1030 }
1031 previous_end = child_range.end;
1032 if !cursor.goto_next_sibling() {
1033 break;
1034 }
1035 }
1036 }
1037 let end = included_child_end.unwrap_or(node_range.end);
1038 if let Some(start) = included_child_start {
1039 let row_count = end.row - start.row;
1040 if row_count < max_row_count {
1041 *ancestor_range = Some(Some(RangeInclusive::new(start.row, end.row)));
1042 return;
1043 }
1044 }
1045
1046 log::info!(
1047 "Expanding to ancestor started on {} node exceeding row limit of {max_row_count}.",
1048 node.grammar_name()
1049 );
1050 *ancestor_range = Some(None);
1051 }
1052 })
1053 }});
1054 reached_outline_node.await;
1055 if let Some(node) = ancestor_range {
1056 return node;
1057 }
1058
1059 let node_name = node.grammar_name();
1060 let node_row_range = RangeInclusive::new(node_range.start.row, node_range.end.row);
1061 if node_name.ends_with("block") {
1062 return Some(node_row_range);
1063 } else if node_name.ends_with("statement") || node_name.ends_with("declaration") {
1064 // Expand to the nearest dedent or blank line for statements and declarations.
1065 let tab_size = cx
1066 .update(|cx| snapshot.settings_at(node_range.start, cx).tab_size.get())
1067 .ok()?;
1068 let indent_level = snapshot
1069 .line_indent_for_row(node_range.start.row)
1070 .len(tab_size);
1071 let rows_remaining = max_row_count.saturating_sub(row_count);
1072 let Some(start_row) = (node_range.start.row.saturating_sub(rows_remaining)
1073 ..node_range.start.row)
1074 .rev()
1075 .find(|row| {
1076 is_line_blank_or_indented_less(indent_level, *row, tab_size, &snapshot.clone())
1077 })
1078 else {
1079 return Some(node_row_range);
1080 };
1081 let rows_remaining = max_row_count.saturating_sub(node_range.end.row - start_row);
1082 let Some(end_row) = (node_range.end.row + 1
1083 ..cmp::min(
1084 node_range.end.row + rows_remaining + 1,
1085 snapshot.row_count(),
1086 ))
1087 .find(|row| {
1088 is_line_blank_or_indented_less(indent_level, *row, tab_size, &snapshot.clone())
1089 })
1090 else {
1091 return Some(node_row_range);
1092 };
1093 return Some(RangeInclusive::new(start_row, end_row));
1094 }
1095
1096 // TODO: doing this instead of walking a cursor as that doesn't work - why?
1097 let Some(parent) = node.parent() else {
1098 log::info!(
1099 "Expanding to ancestor reached the top node, so using default context line count.",
1100 );
1101 return None;
1102 };
1103 node = parent;
1104 }
1105}
1106
1107fn is_line_blank_or_indented_less(
1108 indent_level: u32,
1109 row: u32,
1110 tab_size: u32,
1111 snapshot: &BufferSnapshot,
1112) -> bool {
1113 let line_indent = snapshot.line_indent_for_row(row);
1114 line_indent.is_line_blank() || line_indent.len(tab_size) < indent_level
1115}