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