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