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