diagnostics.rs

   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, AnyView, 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, ToolbarItemLocation, Workspace,
  49    item::{BreadcrumbText, 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);
 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.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 is_dirty = self
 326                .multibuffer
 327                .read(cx)
 328                .buffer(buffer_id)
 329                .is_none_or(|buffer| buffer.read(cx).is_dirty());
 330            if is_dirty {
 331                continue;
 332            }
 333            self.multibuffer.update(cx, |b, cx| {
 334                b.remove_excerpts_for_buffer(buffer_id, cx);
 335            });
 336        }
 337    }
 338
 339    fn update_stale_excerpts(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 340        if self.update_excerpts_task.is_some() {
 341            return;
 342        }
 343
 344        let project_handle = self.project.clone();
 345        self.update_excerpts_task = Some(cx.spawn_in(window, async move |this, cx| {
 346            cx.background_executor()
 347                .timer(DIAGNOSTICS_UPDATE_DEBOUNCE)
 348                .await;
 349            loop {
 350                let Some(path) = this.update(cx, |this, cx| {
 351                    let Some(path) = this.paths_to_update.pop_first() else {
 352                        this.update_excerpts_task = None;
 353                        cx.notify();
 354                        return None;
 355                    };
 356                    Some(path)
 357                })?
 358                else {
 359                    break;
 360                };
 361
 362                if let Some(buffer) = project_handle
 363                    .update(cx, |project, cx| project.open_buffer(path.clone(), cx))?
 364                    .await
 365                    .log_err()
 366                {
 367                    this.update_in(cx, |this, window, cx| {
 368                        let focused = this.editor.focus_handle(cx).contains_focused(window, cx)
 369                            || this.focus_handle.contains_focused(window, cx);
 370                        let retain_excerpts = if focused {
 371                            RetainExcerpts::All
 372                        } else {
 373                            RetainExcerpts::Dirty
 374                        };
 375                        this.update_excerpts(buffer, retain_excerpts, window, cx)
 376                    })?
 377                    .await?;
 378                }
 379            }
 380            Ok(())
 381        }));
 382    }
 383
 384    fn deploy(
 385        workspace: &mut Workspace,
 386        _: &Deploy,
 387        window: &mut Window,
 388        cx: &mut Context<Workspace>,
 389    ) {
 390        if let Some(existing) = workspace.item_of_type::<ProjectDiagnosticsEditor>(cx) {
 391            let is_active = workspace
 392                .active_item(cx)
 393                .is_some_and(|item| item.item_id() == existing.item_id());
 394
 395            workspace.activate_item(&existing, true, !is_active, window, cx);
 396        } else {
 397            let workspace_handle = cx.entity().downgrade();
 398
 399            let include_warnings = match cx.try_global::<IncludeWarnings>() {
 400                Some(include_warnings) => include_warnings.0,
 401                None => ProjectSettings::get_global(cx).diagnostics.include_warnings,
 402            };
 403
 404            let diagnostics = cx.new(|cx| {
 405                ProjectDiagnosticsEditor::new(
 406                    include_warnings,
 407                    workspace.project().clone(),
 408                    workspace_handle,
 409                    window,
 410                    cx,
 411                )
 412            });
 413            workspace.add_item_to_active_pane(Box::new(diagnostics), None, true, window, cx);
 414        }
 415    }
 416
 417    fn toggle_warnings(&mut self, _: &ToggleWarnings, _: &mut Window, cx: &mut Context<Self>) {
 418        cx.set_global(IncludeWarnings(!self.include_warnings));
 419    }
 420
 421    fn toggle_diagnostics_refresh(
 422        &mut self,
 423        _: &ToggleDiagnosticsRefresh,
 424        window: &mut Window,
 425        cx: &mut Context<Self>,
 426    ) {
 427        if self.update_excerpts_task.is_some() {
 428            self.update_excerpts_task = None;
 429        } else {
 430            self.refresh(window, cx);
 431        }
 432        cx.notify();
 433    }
 434
 435    fn focus_in(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 436        if self.focus_handle.is_focused(window) && !self.multibuffer.read(cx).is_empty() {
 437            self.editor.focus_handle(cx).focus(window)
 438        }
 439    }
 440
 441    fn focus_out(&mut self, _: FocusOutEvent, window: &mut Window, cx: &mut Context<Self>) {
 442        if !self.focus_handle.is_focused(window) && !self.editor.focus_handle(cx).is_focused(window)
 443        {
 444            self.close_diagnosticless_buffers(cx, false);
 445        }
 446    }
 447
 448    /// Clears all diagnostics in this view, and refetches them from the project.
 449    fn refresh(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 450        self.diagnostics.clear();
 451        self.editor.update(cx, |editor, cx| {
 452            for (_, block_ids) in self.blocks.drain() {
 453                editor.display_map.update(cx, |display_map, cx| {
 454                    display_map.remove_blocks(block_ids.into_iter().collect(), cx)
 455                });
 456            }
 457        });
 458        self.close_diagnosticless_buffers(cx, false);
 459        self.project.update(cx, |project, cx| {
 460            self.paths_to_update = project
 461                .diagnostic_summaries(false, cx)
 462                .map(|(project_path, _, _)| project_path)
 463                .collect::<BTreeSet<_>>();
 464        });
 465
 466        self.update_stale_excerpts(window, cx);
 467    }
 468
 469    fn diagnostics_are_unchanged(
 470        &self,
 471        existing: &[DiagnosticEntry<text::Anchor>],
 472        new: &[DiagnosticEntryRef<'_, text::Anchor>],
 473        snapshot: &BufferSnapshot,
 474    ) -> bool {
 475        if existing.len() != new.len() {
 476            return false;
 477        }
 478        existing.iter().zip(new.iter()).all(|(existing, new)| {
 479            existing.diagnostic.message == new.diagnostic.message
 480                && existing.diagnostic.severity == new.diagnostic.severity
 481                && existing.diagnostic.is_primary == new.diagnostic.is_primary
 482                && existing.range.to_offset(snapshot) == new.range.to_offset(snapshot)
 483        })
 484    }
 485
 486    fn update_excerpts(
 487        &mut self,
 488        buffer: Entity<Buffer>,
 489        retain_excerpts: RetainExcerpts,
 490        window: &mut Window,
 491        cx: &mut Context<Self>,
 492    ) -> Task<Result<()>> {
 493        let was_empty = self.multibuffer.read(cx).is_empty();
 494        let mut buffer_snapshot = buffer.read(cx).snapshot();
 495        let buffer_id = buffer_snapshot.remote_id();
 496
 497        let max_severity = if self.include_warnings {
 498            lsp::DiagnosticSeverity::WARNING
 499        } else {
 500            lsp::DiagnosticSeverity::ERROR
 501        };
 502
 503        cx.spawn_in(window, async move |this, cx| {
 504            let diagnostics = buffer_snapshot
 505                .diagnostics_in_range::<_, text::Anchor>(
 506                    Point::zero()..buffer_snapshot.max_point(),
 507                    false,
 508                )
 509                .collect::<Vec<_>>();
 510
 511            let unchanged = this.update(cx, |this, _| {
 512                if this.diagnostics.get(&buffer_id).is_some_and(|existing| {
 513                    this.diagnostics_are_unchanged(existing, &diagnostics, &buffer_snapshot)
 514                }) {
 515                    return true;
 516                }
 517                this.diagnostics.insert(
 518                    buffer_id,
 519                    diagnostics
 520                        .iter()
 521                        .map(DiagnosticEntryRef::to_owned)
 522                        .collect(),
 523                );
 524                false
 525            })?;
 526            if unchanged {
 527                return Ok(());
 528            }
 529
 530            let mut grouped: HashMap<usize, Vec<_>> = HashMap::default();
 531            for entry in diagnostics {
 532                grouped
 533                    .entry(entry.diagnostic.group_id)
 534                    .or_default()
 535                    .push(DiagnosticEntryRef {
 536                        range: entry.range.to_point(&buffer_snapshot),
 537                        diagnostic: entry.diagnostic,
 538                    })
 539            }
 540            let mut blocks: Vec<DiagnosticBlock> = Vec::new();
 541
 542            let diagnostics_toolbar_editor = Arc::new(this.clone());
 543            for (_, group) in grouped {
 544                let group_severity = group.iter().map(|d| d.diagnostic.severity).min();
 545                if group_severity.is_none_or(|s| s > max_severity) {
 546                    continue;
 547                }
 548                let languages = this
 549                    .read_with(cx, |t, cx| t.project.read(cx).languages().clone())
 550                    .ok();
 551                let more = cx.update(|_, cx| {
 552                    crate::diagnostic_renderer::DiagnosticRenderer::diagnostic_blocks_for_group(
 553                        group,
 554                        buffer_snapshot.remote_id(),
 555                        Some(diagnostics_toolbar_editor.clone()),
 556                        languages,
 557                        cx,
 558                    )
 559                })?;
 560
 561                blocks.extend(more);
 562            }
 563
 564            let cmp_excerpts = |buffer_snapshot: &BufferSnapshot,
 565                                a: &ExcerptRange<text::Anchor>,
 566                                b: &ExcerptRange<text::Anchor>| {
 567                let context_start = || a.context.start.cmp(&b.context.start, buffer_snapshot);
 568                let context_end = || a.context.end.cmp(&b.context.end, buffer_snapshot);
 569                let primary_start = || a.primary.start.cmp(&b.primary.start, buffer_snapshot);
 570                let primary_end = || a.primary.end.cmp(&b.primary.end, buffer_snapshot);
 571                context_start()
 572                    .then_with(context_end)
 573                    .then_with(primary_start)
 574                    .then_with(primary_end)
 575                    .then(cmp::Ordering::Greater)
 576            };
 577
 578            let mut excerpt_ranges: Vec<ExcerptRange<_>> = this.update(cx, |this, cx| {
 579                this.multibuffer.update(cx, |multi_buffer, cx| {
 580                    let is_dirty = multi_buffer
 581                        .buffer(buffer_id)
 582                        .is_none_or(|buffer| buffer.read(cx).is_dirty());
 583                    match retain_excerpts {
 584                        RetainExcerpts::Dirty if !is_dirty => Vec::new(),
 585                        RetainExcerpts::All | RetainExcerpts::Dirty => multi_buffer
 586                            .excerpts_for_buffer(buffer_id, cx)
 587                            .into_iter()
 588                            .map(|(_, range)| range)
 589                            .sorted_by(|a, b| cmp_excerpts(&buffer_snapshot, a, b))
 590                            .collect(),
 591                    }
 592                })
 593            })?;
 594
 595            let mut result_blocks = vec![None; excerpt_ranges.len()];
 596            let context_lines = cx.update(|_, cx| multibuffer_context_lines(cx))?;
 597            for b in blocks {
 598                let excerpt_range = context_range_for_entry(
 599                    b.initial_range.clone(),
 600                    context_lines,
 601                    buffer_snapshot.clone(),
 602                    cx,
 603                )
 604                .await;
 605                buffer_snapshot = cx.update(|_, cx| buffer.read(cx).snapshot())?;
 606                let initial_range = buffer_snapshot.anchor_after(b.initial_range.start)
 607                    ..buffer_snapshot.anchor_before(b.initial_range.end);
 608                let excerpt_range = ExcerptRange {
 609                    context: excerpt_range,
 610                    primary: initial_range,
 611                };
 612                let i = excerpt_ranges
 613                    .binary_search_by(|probe| cmp_excerpts(&buffer_snapshot, probe, &excerpt_range))
 614                    .unwrap_or_else(|i| i);
 615                excerpt_ranges.insert(i, excerpt_range);
 616                result_blocks.insert(i, Some(b));
 617            }
 618
 619            this.update_in(cx, |this, window, cx| {
 620                if let Some(block_ids) = this.blocks.remove(&buffer_id) {
 621                    this.editor.update(cx, |editor, cx| {
 622                        editor.display_map.update(cx, |display_map, cx| {
 623                            display_map.remove_blocks(block_ids.into_iter().collect(), cx)
 624                        });
 625                    })
 626                }
 627                let (anchor_ranges, _) = this.multibuffer.update(cx, |multi_buffer, cx| {
 628                    let excerpt_ranges = excerpt_ranges
 629                        .into_iter()
 630                        .map(|range| ExcerptRange {
 631                            context: range.context.to_point(&buffer_snapshot),
 632                            primary: range.primary.to_point(&buffer_snapshot),
 633                        })
 634                        .collect();
 635                    multi_buffer.set_excerpt_ranges_for_path(
 636                        PathKey::for_buffer(&buffer, cx),
 637                        buffer.clone(),
 638                        &buffer_snapshot,
 639                        excerpt_ranges,
 640                        cx,
 641                    )
 642                });
 643                #[cfg(test)]
 644                let cloned_blocks = result_blocks.clone();
 645
 646                if was_empty && let Some(anchor_range) = anchor_ranges.first() {
 647                    let range_to_select = anchor_range.start..anchor_range.start;
 648                    this.editor.update(cx, |editor, cx| {
 649                        editor.change_selections(Default::default(), window, cx, |s| {
 650                            s.select_anchor_ranges([range_to_select]);
 651                        })
 652                    });
 653                    if this.focus_handle.is_focused(window) {
 654                        this.editor.read(cx).focus_handle(cx).focus(window);
 655                    }
 656                }
 657
 658                let editor_blocks = anchor_ranges
 659                    .into_iter()
 660                    .zip_eq(result_blocks.into_iter())
 661                    .filter_map(|(anchor, block)| {
 662                        let block = block?;
 663                        let editor = this.editor.downgrade();
 664                        Some(BlockProperties {
 665                            placement: BlockPlacement::Near(anchor.start),
 666                            height: Some(1),
 667                            style: BlockStyle::Flex,
 668                            render: Arc::new(move |bcx| block.render_block(editor.clone(), bcx)),
 669                            priority: 1,
 670                        })
 671                    });
 672
 673                let block_ids = this.editor.update(cx, |editor, cx| {
 674                    editor.display_map.update(cx, |display_map, cx| {
 675                        display_map.insert_blocks(editor_blocks, cx)
 676                    })
 677                });
 678
 679                #[cfg(test)]
 680                {
 681                    for (block_id, block) in
 682                        block_ids.iter().zip(cloned_blocks.into_iter().flatten())
 683                    {
 684                        let markdown = block.markdown.clone();
 685                        editor::test::set_block_content_for_tests(
 686                            &this.editor,
 687                            *block_id,
 688                            cx,
 689                            move |cx| {
 690                                markdown::MarkdownElement::rendered_text(
 691                                    markdown.clone(),
 692                                    cx,
 693                                    editor::hover_popover::diagnostics_markdown_style,
 694                                )
 695                            },
 696                        );
 697                    }
 698                }
 699
 700                this.blocks.insert(buffer_id, block_ids);
 701                cx.notify()
 702            })
 703        })
 704    }
 705
 706    fn update_diagnostic_summary(&mut self, cx: &mut Context<Self>) {
 707        self.summary = self.project.read(cx).diagnostic_summary(false, cx);
 708        cx.emit(EditorEvent::TitleChanged);
 709    }
 710}
 711
 712impl Focusable for ProjectDiagnosticsEditor {
 713    fn focus_handle(&self, _: &App) -> FocusHandle {
 714        self.focus_handle.clone()
 715    }
 716}
 717
 718impl Item for ProjectDiagnosticsEditor {
 719    type Event = EditorEvent;
 720
 721    fn to_item_events(event: &EditorEvent, f: impl FnMut(ItemEvent)) {
 722        Editor::to_item_events(event, f)
 723    }
 724
 725    fn deactivated(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 726        self.editor
 727            .update(cx, |editor, cx| editor.deactivated(window, cx));
 728    }
 729
 730    fn navigate(
 731        &mut self,
 732        data: Box<dyn Any>,
 733        window: &mut Window,
 734        cx: &mut Context<Self>,
 735    ) -> bool {
 736        self.editor
 737            .update(cx, |editor, cx| editor.navigate(data, window, cx))
 738    }
 739
 740    fn tab_tooltip_text(&self, _: &App) -> Option<SharedString> {
 741        Some("Project Diagnostics".into())
 742    }
 743
 744    fn tab_content_text(&self, _detail: usize, _: &App) -> SharedString {
 745        "Diagnostics".into()
 746    }
 747
 748    fn tab_content(&self, params: TabContentParams, _window: &Window, _: &App) -> AnyElement {
 749        h_flex()
 750            .gap_1()
 751            .when(
 752                self.summary.error_count == 0 && self.summary.warning_count == 0,
 753                |then| {
 754                    then.child(
 755                        h_flex()
 756                            .gap_1()
 757                            .child(Icon::new(IconName::Check).color(Color::Success))
 758                            .child(Label::new("No problems").color(params.text_color())),
 759                    )
 760                },
 761            )
 762            .when(self.summary.error_count > 0, |then| {
 763                then.child(
 764                    h_flex()
 765                        .gap_1()
 766                        .child(Icon::new(IconName::XCircle).color(Color::Error))
 767                        .child(
 768                            Label::new(self.summary.error_count.to_string())
 769                                .color(params.text_color()),
 770                        ),
 771                )
 772            })
 773            .when(self.summary.warning_count > 0, |then| {
 774                then.child(
 775                    h_flex()
 776                        .gap_1()
 777                        .child(Icon::new(IconName::Warning).color(Color::Warning))
 778                        .child(
 779                            Label::new(self.summary.warning_count.to_string())
 780                                .color(params.text_color()),
 781                        ),
 782                )
 783            })
 784            .into_any_element()
 785    }
 786
 787    fn telemetry_event_text(&self) -> Option<&'static str> {
 788        Some("Project Diagnostics Opened")
 789    }
 790
 791    fn for_each_project_item(
 792        &self,
 793        cx: &App,
 794        f: &mut dyn FnMut(gpui::EntityId, &dyn project::ProjectItem),
 795    ) {
 796        self.editor.for_each_project_item(cx, f)
 797    }
 798
 799    fn set_nav_history(
 800        &mut self,
 801        nav_history: ItemNavHistory,
 802        _: &mut Window,
 803        cx: &mut Context<Self>,
 804    ) {
 805        self.editor.update(cx, |editor, _| {
 806            editor.set_nav_history(Some(nav_history));
 807        });
 808    }
 809
 810    fn can_split(&self) -> bool {
 811        true
 812    }
 813
 814    fn clone_on_split(
 815        &self,
 816        _workspace_id: Option<workspace::WorkspaceId>,
 817        window: &mut Window,
 818        cx: &mut Context<Self>,
 819    ) -> Task<Option<Entity<Self>>>
 820    where
 821        Self: Sized,
 822    {
 823        Task::ready(Some(cx.new(|cx| {
 824            ProjectDiagnosticsEditor::new(
 825                self.include_warnings,
 826                self.project.clone(),
 827                self.workspace.clone(),
 828                window,
 829                cx,
 830            )
 831        })))
 832    }
 833
 834    fn is_dirty(&self, cx: &App) -> bool {
 835        self.multibuffer.read(cx).is_dirty(cx)
 836    }
 837
 838    fn has_deleted_file(&self, cx: &App) -> bool {
 839        self.multibuffer.read(cx).has_deleted_file(cx)
 840    }
 841
 842    fn has_conflict(&self, cx: &App) -> bool {
 843        self.multibuffer.read(cx).has_conflict(cx)
 844    }
 845
 846    fn can_save(&self, _: &App) -> bool {
 847        true
 848    }
 849
 850    fn save(
 851        &mut self,
 852        options: SaveOptions,
 853        project: Entity<Project>,
 854        window: &mut Window,
 855        cx: &mut Context<Self>,
 856    ) -> Task<Result<()>> {
 857        self.editor.save(options, project, window, cx)
 858    }
 859
 860    fn save_as(
 861        &mut self,
 862        _: Entity<Project>,
 863        _: ProjectPath,
 864        _window: &mut Window,
 865        _: &mut Context<Self>,
 866    ) -> Task<Result<()>> {
 867        unreachable!()
 868    }
 869
 870    fn reload(
 871        &mut self,
 872        project: Entity<Project>,
 873        window: &mut Window,
 874        cx: &mut Context<Self>,
 875    ) -> Task<Result<()>> {
 876        self.editor.reload(project, window, cx)
 877    }
 878
 879    fn act_as_type<'a>(
 880        &'a self,
 881        type_id: TypeId,
 882        self_handle: &'a Entity<Self>,
 883        _: &'a App,
 884    ) -> Option<AnyView> {
 885        if type_id == TypeId::of::<Self>() {
 886            Some(self_handle.to_any())
 887        } else if type_id == TypeId::of::<Editor>() {
 888            Some(self.editor.to_any())
 889        } else {
 890            None
 891        }
 892    }
 893
 894    fn as_searchable(&self, _: &Entity<Self>) -> Option<Box<dyn SearchableItemHandle>> {
 895        Some(Box::new(self.editor.clone()))
 896    }
 897
 898    fn breadcrumb_location(&self, _: &App) -> ToolbarItemLocation {
 899        ToolbarItemLocation::PrimaryLeft
 900    }
 901
 902    fn breadcrumbs(&self, theme: &theme::Theme, cx: &App) -> Option<Vec<BreadcrumbText>> {
 903        self.editor.breadcrumbs(theme, cx)
 904    }
 905
 906    fn added_to_workspace(
 907        &mut self,
 908        workspace: &mut Workspace,
 909        window: &mut Window,
 910        cx: &mut Context<Self>,
 911    ) {
 912        self.editor.update(cx, |editor, cx| {
 913            editor.added_to_workspace(workspace, window, cx)
 914        });
 915    }
 916}
 917
 918impl DiagnosticsToolbarEditor for WeakEntity<ProjectDiagnosticsEditor> {
 919    fn include_warnings(&self, cx: &App) -> bool {
 920        self.read_with(cx, |project_diagnostics_editor, _cx| {
 921            project_diagnostics_editor.include_warnings
 922        })
 923        .unwrap_or(false)
 924    }
 925
 926    fn is_updating(&self, cx: &App) -> bool {
 927        self.read_with(cx, |project_diagnostics_editor, cx| {
 928            project_diagnostics_editor.update_excerpts_task.is_some()
 929                || project_diagnostics_editor
 930                    .project
 931                    .read(cx)
 932                    .language_servers_running_disk_based_diagnostics(cx)
 933                    .next()
 934                    .is_some()
 935        })
 936        .unwrap_or(false)
 937    }
 938
 939    fn stop_updating(&self, cx: &mut App) {
 940        let _ = self.update(cx, |project_diagnostics_editor, cx| {
 941            project_diagnostics_editor.update_excerpts_task = None;
 942            cx.notify();
 943        });
 944    }
 945
 946    fn refresh_diagnostics(&self, window: &mut Window, cx: &mut App) {
 947        let _ = self.update(cx, |project_diagnostics_editor, cx| {
 948            project_diagnostics_editor.refresh(window, cx);
 949        });
 950    }
 951
 952    fn toggle_warnings(&self, window: &mut Window, cx: &mut App) {
 953        let _ = self.update(cx, |project_diagnostics_editor, cx| {
 954            project_diagnostics_editor.toggle_warnings(&Default::default(), window, cx);
 955        });
 956    }
 957
 958    fn get_diagnostics_for_buffer(
 959        &self,
 960        buffer_id: text::BufferId,
 961        cx: &App,
 962    ) -> Vec<language::DiagnosticEntry<text::Anchor>> {
 963        self.read_with(cx, |project_diagnostics_editor, _cx| {
 964            project_diagnostics_editor
 965                .diagnostics
 966                .get(&buffer_id)
 967                .cloned()
 968                .unwrap_or_default()
 969        })
 970        .unwrap_or_default()
 971    }
 972}
 973const DIAGNOSTIC_EXPANSION_ROW_LIMIT: u32 = 32;
 974
 975async fn context_range_for_entry(
 976    range: Range<Point>,
 977    context: u32,
 978    snapshot: BufferSnapshot,
 979    cx: &mut AsyncApp,
 980) -> Range<text::Anchor> {
 981    let range = if let Some(rows) = heuristic_syntactic_expand(
 982        range.clone(),
 983        DIAGNOSTIC_EXPANSION_ROW_LIMIT,
 984        snapshot.clone(),
 985        cx,
 986    )
 987    .await
 988    {
 989        Range {
 990            start: Point::new(*rows.start(), 0),
 991            end: snapshot.clip_point(Point::new(*rows.end(), u32::MAX), Bias::Left),
 992        }
 993    } else {
 994        Range {
 995            start: Point::new(range.start.row.saturating_sub(context), 0),
 996            end: snapshot.clip_point(Point::new(range.end.row + context, u32::MAX), Bias::Left),
 997        }
 998    };
 999    snapshot.anchor_after(range.start)..snapshot.anchor_before(range.end)
1000}
1001
1002/// Expands the input range using syntax information from TreeSitter. This expansion will be limited
1003/// to the specified `max_row_count`.
1004///
1005/// If there is a containing outline item that is less than `max_row_count`, it will be returned.
1006/// Otherwise fairly arbitrary heuristics are applied to attempt to return a logical block of code.
1007async fn heuristic_syntactic_expand(
1008    input_range: Range<Point>,
1009    max_row_count: u32,
1010    snapshot: BufferSnapshot,
1011    cx: &mut AsyncApp,
1012) -> Option<RangeInclusive<BufferRow>> {
1013    let input_row_count = input_range.end.row - input_range.start.row;
1014    if input_row_count > max_row_count {
1015        return None;
1016    }
1017
1018    // If the outline node contains the diagnostic and is small enough, just use that.
1019    let outline_range = snapshot.outline_range_containing(input_range.clone());
1020    if let Some(outline_range) = outline_range.clone() {
1021        // Remove blank lines from start and end
1022        if let Some(start_row) = (outline_range.start.row..outline_range.end.row)
1023            .find(|row| !snapshot.line_indent_for_row(*row).is_line_blank())
1024            && let Some(end_row) = (outline_range.start.row..outline_range.end.row + 1)
1025                .rev()
1026                .find(|row| !snapshot.line_indent_for_row(*row).is_line_blank())
1027        {
1028            let row_count = end_row.saturating_sub(start_row);
1029            if row_count <= max_row_count {
1030                return Some(RangeInclusive::new(
1031                    outline_range.start.row,
1032                    outline_range.end.row,
1033                ));
1034            }
1035        }
1036    }
1037
1038    let mut node = snapshot.syntax_ancestor(input_range.clone())?;
1039
1040    loop {
1041        let node_start = Point::from_ts_point(node.start_position());
1042        let node_end = Point::from_ts_point(node.end_position());
1043        let node_range = node_start..node_end;
1044        let row_count = node_end.row - node_start.row + 1;
1045        let mut ancestor_range = None;
1046        let reached_outline_node = cx.background_executor().scoped({
1047            let node_range = node_range.clone();
1048            let outline_range = outline_range.clone();
1049            let ancestor_range = &mut ancestor_range;
1050            |scope| {
1051                scope.spawn(async move {
1052                    // Stop if we've exceeded the row count or reached an outline node. Then, find the interval
1053                    // of node children which contains the query range. For example, this allows just returning
1054                    // the header of a declaration rather than the entire declaration.
1055                    if row_count > max_row_count || outline_range == Some(node_range.clone()) {
1056                        let mut cursor = node.walk();
1057                        let mut included_child_start = None;
1058                        let mut included_child_end = None;
1059                        let mut previous_end = node_start;
1060                        if cursor.goto_first_child() {
1061                            loop {
1062                                let child_node = cursor.node();
1063                                let child_range =
1064                                    previous_end..Point::from_ts_point(child_node.end_position());
1065                                if included_child_start.is_none()
1066                                    && child_range.contains(&input_range.start)
1067                                {
1068                                    included_child_start = Some(child_range.start);
1069                                }
1070                                if child_range.contains(&input_range.end) {
1071                                    included_child_end = Some(child_range.end);
1072                                }
1073                                previous_end = child_range.end;
1074                                if !cursor.goto_next_sibling() {
1075                                    break;
1076                                }
1077                            }
1078                        }
1079                        let end = included_child_end.unwrap_or(node_range.end);
1080                        if let Some(start) = included_child_start {
1081                            let row_count = end.row - start.row;
1082                            if row_count < max_row_count {
1083                                *ancestor_range =
1084                                    Some(Some(RangeInclusive::new(start.row, end.row)));
1085                                return;
1086                            }
1087                        }
1088                        *ancestor_range = Some(None);
1089                    }
1090                })
1091            }
1092        });
1093        reached_outline_node.await;
1094        if let Some(node) = ancestor_range {
1095            return node;
1096        }
1097
1098        let node_name = node.grammar_name();
1099        let node_row_range = RangeInclusive::new(node_range.start.row, node_range.end.row);
1100        if node_name.ends_with("block") {
1101            return Some(node_row_range);
1102        } else if node_name.ends_with("statement") || node_name.ends_with("declaration") {
1103            // Expand to the nearest dedent or blank line for statements and declarations.
1104            let tab_size = cx
1105                .update(|cx| snapshot.settings_at(node_range.start, cx).tab_size.get())
1106                .ok()?;
1107            let indent_level = snapshot
1108                .line_indent_for_row(node_range.start.row)
1109                .len(tab_size);
1110            let rows_remaining = max_row_count.saturating_sub(row_count);
1111            let Some(start_row) = (node_range.start.row.saturating_sub(rows_remaining)
1112                ..node_range.start.row)
1113                .rev()
1114                .find(|row| {
1115                    is_line_blank_or_indented_less(indent_level, *row, tab_size, &snapshot.clone())
1116                })
1117            else {
1118                return Some(node_row_range);
1119            };
1120            let rows_remaining = max_row_count.saturating_sub(node_range.end.row - start_row);
1121            let Some(end_row) = (node_range.end.row + 1
1122                ..cmp::min(
1123                    node_range.end.row + rows_remaining + 1,
1124                    snapshot.row_count(),
1125                ))
1126                .find(|row| {
1127                    is_line_blank_or_indented_less(indent_level, *row, tab_size, &snapshot.clone())
1128                })
1129            else {
1130                return Some(node_row_range);
1131            };
1132            return Some(RangeInclusive::new(start_row, end_row));
1133        }
1134
1135        // TODO: doing this instead of walking a cursor as that doesn't work - why?
1136        let Some(parent) = node.parent() else {
1137            log::info!(
1138                "Expanding to ancestor reached the top node, so using default context line count.",
1139            );
1140            return None;
1141        };
1142        node = parent;
1143    }
1144}
1145
1146fn is_line_blank_or_indented_less(
1147    indent_level: u32,
1148    row: u32,
1149    tab_size: u32,
1150    snapshot: &BufferSnapshot,
1151) -> bool {
1152    let line_indent = snapshot.line_indent_for_row(row);
1153    line_indent.is_line_blank() || line_indent.len(tab_size) < indent_level
1154}