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