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