diagnostics.rs

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