diagnostics.rs

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