diagnostics.rs

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