diagnostics.rs

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