hover_popover.rs

   1use crate::{
   2    display_map::{InlayOffset, ToDisplayPoint},
   3    hover_links::{InlayHighlight, RangeInEditor},
   4    Anchor, AnchorRangeExt, DisplayPoint, Editor, EditorSettings, EditorSnapshot, EditorStyle,
   5    ExcerptId, Hover, RangeToAnchorExt,
   6};
   7use futures::FutureExt;
   8use gpui::{
   9    div, px, AnyElement, CursorStyle, Hsla, InteractiveElement, IntoElement, Model, MouseButton,
  10    ParentElement, Pixels, SharedString, Size, StatefulInteractiveElement, Styled, Task,
  11    ViewContext, WeakView,
  12};
  13use language::{markdown, Bias, DiagnosticEntry, Language, LanguageRegistry, ParsedMarkdown};
  14
  15use lsp::DiagnosticSeverity;
  16use project::{HoverBlock, HoverBlockKind, InlayHintLabelPart, Project};
  17use settings::Settings;
  18use std::{ops::Range, sync::Arc, time::Duration};
  19use ui::{prelude::*, Tooltip};
  20use util::TryFutureExt;
  21use workspace::Workspace;
  22
  23pub const HOVER_DELAY_MILLIS: u64 = 350;
  24pub const HOVER_REQUEST_DELAY_MILLIS: u64 = 200;
  25
  26pub const MIN_POPOVER_CHARACTER_WIDTH: f32 = 20.;
  27pub const MIN_POPOVER_LINE_HEIGHT: Pixels = px(4.);
  28pub const HOVER_POPOVER_GAP: Pixels = px(10.);
  29
  30/// Bindable action which uses the most recent selection head to trigger a hover
  31pub fn hover(editor: &mut Editor, _: &Hover, cx: &mut ViewContext<Editor>) {
  32    let head = editor.selections.newest_display(cx).head();
  33    show_hover(editor, head, true, cx);
  34}
  35
  36/// The internal hover action dispatches between `show_hover` or `hide_hover`
  37/// depending on whether a point to hover over is provided.
  38pub fn hover_at(editor: &mut Editor, point: Option<DisplayPoint>, cx: &mut ViewContext<Editor>) {
  39    if EditorSettings::get_global(cx).hover_popover_enabled {
  40        if let Some(point) = point {
  41            show_hover(editor, point, false, cx);
  42        } else {
  43            hide_hover(editor, cx);
  44        }
  45    }
  46}
  47
  48pub struct InlayHover {
  49    pub excerpt: ExcerptId,
  50    pub range: InlayHighlight,
  51    pub tooltip: HoverBlock,
  52}
  53
  54pub fn find_hovered_hint_part(
  55    label_parts: Vec<InlayHintLabelPart>,
  56    hint_start: InlayOffset,
  57    hovered_offset: InlayOffset,
  58) -> Option<(InlayHintLabelPart, Range<InlayOffset>)> {
  59    if hovered_offset >= hint_start {
  60        let mut hovered_character = (hovered_offset - hint_start).0;
  61        let mut part_start = hint_start;
  62        for part in label_parts {
  63            let part_len = part.value.chars().count();
  64            if hovered_character > part_len {
  65                hovered_character -= part_len;
  66                part_start.0 += part_len;
  67            } else {
  68                let part_end = InlayOffset(part_start.0 + part_len);
  69                return Some((part, part_start..part_end));
  70            }
  71        }
  72    }
  73    None
  74}
  75
  76pub fn hover_at_inlay(editor: &mut Editor, inlay_hover: InlayHover, cx: &mut ViewContext<Editor>) {
  77    if EditorSettings::get_global(cx).hover_popover_enabled {
  78        if editor.pending_rename.is_some() {
  79            return;
  80        }
  81
  82        let Some(project) = editor.project.clone() else {
  83            return;
  84        };
  85
  86        if let Some(InfoPopover { symbol_range, .. }) = &editor.hover_state.info_popover {
  87            if let RangeInEditor::Inlay(range) = symbol_range {
  88                if range == &inlay_hover.range {
  89                    // Hover triggered from same location as last time. Don't show again.
  90                    return;
  91                }
  92            }
  93            hide_hover(editor, cx);
  94        }
  95
  96        let task = cx.spawn(|this, mut cx| {
  97            async move {
  98                cx.background_executor()
  99                    .timer(Duration::from_millis(HOVER_DELAY_MILLIS))
 100                    .await;
 101                this.update(&mut cx, |this, _| {
 102                    this.hover_state.diagnostic_popover = None;
 103                })?;
 104
 105                let language_registry = project.update(&mut cx, |p, _| p.languages().clone())?;
 106                let blocks = vec![inlay_hover.tooltip];
 107                let parsed_content = parse_blocks(&blocks, &language_registry, None).await;
 108
 109                let hover_popover = InfoPopover {
 110                    project: project.clone(),
 111                    symbol_range: RangeInEditor::Inlay(inlay_hover.range.clone()),
 112                    blocks,
 113                    parsed_content,
 114                };
 115
 116                this.update(&mut cx, |this, cx| {
 117                    // TODO: no background highlights happen for inlays currently
 118                    this.hover_state.info_popover = Some(hover_popover);
 119                    cx.notify();
 120                })?;
 121
 122                anyhow::Ok(())
 123            }
 124            .log_err()
 125        });
 126
 127        editor.hover_state.info_task = Some(task);
 128    }
 129}
 130
 131/// Hides the type information popup.
 132/// Triggered by the `Hover` action when the cursor is not over a symbol or when the
 133/// selections changed.
 134pub fn hide_hover(editor: &mut Editor, cx: &mut ViewContext<Editor>) -> bool {
 135    let did_hide = editor.hover_state.info_popover.take().is_some()
 136        | editor.hover_state.diagnostic_popover.take().is_some();
 137
 138    editor.hover_state.info_task = None;
 139    editor.hover_state.triggered_from = None;
 140
 141    editor.clear_background_highlights::<HoverState>(cx);
 142
 143    if did_hide {
 144        cx.notify();
 145    }
 146
 147    did_hide
 148}
 149
 150/// Queries the LSP and shows type info and documentation
 151/// about the symbol the mouse is currently hovering over.
 152/// Triggered by the `Hover` action when the cursor may be over a symbol.
 153fn show_hover(
 154    editor: &mut Editor,
 155    point: DisplayPoint,
 156    ignore_timeout: bool,
 157    cx: &mut ViewContext<Editor>,
 158) {
 159    if editor.pending_rename.is_some() {
 160        return;
 161    }
 162
 163    let snapshot = editor.snapshot(cx);
 164    let multibuffer_offset = point.to_offset(&snapshot.display_snapshot, Bias::Left);
 165
 166    let (buffer, buffer_position) = if let Some(output) = editor
 167        .buffer
 168        .read(cx)
 169        .text_anchor_for_position(multibuffer_offset, cx)
 170    {
 171        output
 172    } else {
 173        return;
 174    };
 175
 176    let excerpt_id = if let Some((excerpt_id, _, _)) = editor
 177        .buffer()
 178        .read(cx)
 179        .excerpt_containing(multibuffer_offset, cx)
 180    {
 181        excerpt_id
 182    } else {
 183        return;
 184    };
 185
 186    let project = if let Some(project) = editor.project.clone() {
 187        project
 188    } else {
 189        return;
 190    };
 191
 192    if !ignore_timeout {
 193        if let Some(InfoPopover { symbol_range, .. }) = &editor.hover_state.info_popover {
 194            if symbol_range
 195                .as_text_range()
 196                .map(|range| {
 197                    let hover_range = range.to_offset(&snapshot.buffer_snapshot);
 198                    // LSP returns a hover result for the end index of ranges that should be hovered, so we need to
 199                    // use an inclusive range here to check if we should dismiss the popover
 200                    (hover_range.start..=hover_range.end).contains(&multibuffer_offset)
 201                })
 202                .unwrap_or(false)
 203            {
 204                // Hover triggered from same location as last time. Don't show again.
 205                return;
 206            } else {
 207                hide_hover(editor, cx);
 208            }
 209        }
 210    }
 211
 212    // Get input anchor
 213    let anchor = snapshot
 214        .buffer_snapshot
 215        .anchor_at(multibuffer_offset, Bias::Left);
 216
 217    // Don't request again if the location is the same as the previous request
 218    if let Some(triggered_from) = &editor.hover_state.triggered_from {
 219        if triggered_from
 220            .cmp(&anchor, &snapshot.buffer_snapshot)
 221            .is_eq()
 222        {
 223            return;
 224        }
 225    }
 226
 227    let task = cx.spawn(|this, mut cx| {
 228        async move {
 229            // If we need to delay, delay a set amount initially before making the lsp request
 230            let delay = if !ignore_timeout {
 231                // Construct delay task to wait for later
 232                let total_delay = Some(
 233                    cx.background_executor()
 234                        .timer(Duration::from_millis(HOVER_DELAY_MILLIS)),
 235                );
 236
 237                cx.background_executor()
 238                    .timer(Duration::from_millis(HOVER_REQUEST_DELAY_MILLIS))
 239                    .await;
 240                total_delay
 241            } else {
 242                None
 243            };
 244
 245            // query the LSP for hover info
 246            let hover_request = cx.update(|cx| {
 247                project.update(cx, |project, cx| {
 248                    project.hover(&buffer, buffer_position, cx)
 249                })
 250            })?;
 251
 252            if let Some(delay) = delay {
 253                delay.await;
 254            }
 255
 256            // If there's a diagnostic, assign it on the hover state and notify
 257            let local_diagnostic = snapshot
 258                .buffer_snapshot
 259                .diagnostics_in_range::<_, usize>(multibuffer_offset..multibuffer_offset, false)
 260                // Find the entry with the most specific range
 261                .min_by_key(|entry| entry.range.end - entry.range.start)
 262                .map(|entry| DiagnosticEntry {
 263                    diagnostic: entry.diagnostic,
 264                    range: entry.range.to_anchors(&snapshot.buffer_snapshot),
 265                });
 266
 267            // Pull the primary diagnostic out so we can jump to it if the popover is clicked
 268            let primary_diagnostic = local_diagnostic.as_ref().and_then(|local_diagnostic| {
 269                snapshot
 270                    .buffer_snapshot
 271                    .diagnostic_group::<usize>(local_diagnostic.diagnostic.group_id)
 272                    .find(|diagnostic| diagnostic.diagnostic.is_primary)
 273                    .map(|entry| DiagnosticEntry {
 274                        diagnostic: entry.diagnostic,
 275                        range: entry.range.to_anchors(&snapshot.buffer_snapshot),
 276                    })
 277            });
 278
 279            this.update(&mut cx, |this, _| {
 280                this.hover_state.diagnostic_popover =
 281                    local_diagnostic.map(|local_diagnostic| DiagnosticPopover {
 282                        local_diagnostic,
 283                        primary_diagnostic,
 284                    });
 285            })?;
 286
 287            let hover_result = hover_request.await.ok().flatten();
 288            let snapshot = this.update(&mut cx, |this, cx| this.snapshot(cx))?;
 289            let hover_popover = match hover_result {
 290                Some(hover_result) if !hover_result.is_empty() => {
 291                    // Create symbol range of anchors for highlighting and filtering of future requests.
 292                    let range = hover_result
 293                        .range
 294                        .and_then(|range| {
 295                            let start = snapshot
 296                                .buffer_snapshot
 297                                .anchor_in_excerpt(excerpt_id, range.start)?;
 298                            let end = snapshot
 299                                .buffer_snapshot
 300                                .anchor_in_excerpt(excerpt_id, range.end)?;
 301
 302                            Some(start..end)
 303                        })
 304                        .unwrap_or_else(|| anchor..anchor);
 305
 306                    let language_registry =
 307                        project.update(&mut cx, |p, _| p.languages().clone())?;
 308                    let blocks = hover_result.contents;
 309                    let language = hover_result.language;
 310                    let parsed_content = parse_blocks(&blocks, &language_registry, language).await;
 311
 312                    Some(InfoPopover {
 313                        project: project.clone(),
 314                        symbol_range: RangeInEditor::Text(range),
 315                        blocks,
 316                        parsed_content,
 317                    })
 318                }
 319
 320                _ => None,
 321            };
 322
 323            this.update(&mut cx, |this, cx| {
 324                if let Some(symbol_range) = hover_popover
 325                    .as_ref()
 326                    .and_then(|hover_popover| hover_popover.symbol_range.as_text_range())
 327                {
 328                    // Highlight the selected symbol using a background highlight
 329                    this.highlight_background::<HoverState>(
 330                        vec![symbol_range],
 331                        |theme| theme.element_hover, // todo update theme
 332                        cx,
 333                    );
 334                } else {
 335                    this.clear_background_highlights::<HoverState>(cx);
 336                }
 337
 338                this.hover_state.info_popover = hover_popover;
 339                cx.notify();
 340                cx.refresh();
 341            })?;
 342
 343            Ok::<_, anyhow::Error>(())
 344        }
 345        .log_err()
 346    });
 347
 348    editor.hover_state.info_task = Some(task);
 349}
 350
 351async fn parse_blocks(
 352    blocks: &[HoverBlock],
 353    language_registry: &Arc<LanguageRegistry>,
 354    language: Option<Arc<Language>>,
 355) -> markdown::ParsedMarkdown {
 356    let mut text = String::new();
 357    let mut highlights = Vec::new();
 358    let mut region_ranges = Vec::new();
 359    let mut regions = Vec::new();
 360
 361    for block in blocks {
 362        match &block.kind {
 363            HoverBlockKind::PlainText => {
 364                markdown::new_paragraph(&mut text, &mut Vec::new());
 365                text.push_str(&block.text);
 366            }
 367
 368            HoverBlockKind::Markdown => {
 369                markdown::parse_markdown_block(
 370                    &block.text,
 371                    language_registry,
 372                    language.clone(),
 373                    &mut text,
 374                    &mut highlights,
 375                    &mut region_ranges,
 376                    &mut regions,
 377                )
 378                .await
 379            }
 380
 381            HoverBlockKind::Code { language } => {
 382                if let Some(language) = language_registry
 383                    .language_for_name(language)
 384                    .now_or_never()
 385                    .and_then(Result::ok)
 386                {
 387                    markdown::highlight_code(&mut text, &mut highlights, &block.text, &language);
 388                } else {
 389                    text.push_str(&block.text);
 390                }
 391            }
 392        }
 393    }
 394
 395    let leading_space = text.chars().take_while(|c| c.is_whitespace()).count();
 396    if leading_space > 0 {
 397        highlights = highlights
 398            .into_iter()
 399            .map(|(range, style)| {
 400                (
 401                    range.start.saturating_sub(leading_space)
 402                        ..range.end.saturating_sub(leading_space),
 403                    style,
 404                )
 405            })
 406            .collect();
 407        region_ranges = region_ranges
 408            .into_iter()
 409            .map(|range| {
 410                range.start.saturating_sub(leading_space)..range.end.saturating_sub(leading_space)
 411            })
 412            .collect();
 413    }
 414
 415    ParsedMarkdown {
 416        text: text.trim().to_string(),
 417        highlights,
 418        region_ranges,
 419        regions,
 420    }
 421}
 422
 423#[derive(Default)]
 424pub struct HoverState {
 425    pub info_popover: Option<InfoPopover>,
 426    pub diagnostic_popover: Option<DiagnosticPopover>,
 427    pub triggered_from: Option<Anchor>,
 428    pub info_task: Option<Task<Option<()>>>,
 429}
 430
 431impl HoverState {
 432    pub fn visible(&self) -> bool {
 433        self.info_popover.is_some() || self.diagnostic_popover.is_some()
 434    }
 435
 436    pub fn render(
 437        &mut self,
 438        snapshot: &EditorSnapshot,
 439        style: &EditorStyle,
 440        visible_rows: Range<u32>,
 441        max_size: Size<Pixels>,
 442        workspace: Option<WeakView<Workspace>>,
 443        cx: &mut ViewContext<Editor>,
 444    ) -> Option<(DisplayPoint, Vec<AnyElement>)> {
 445        // If there is a diagnostic, position the popovers based on that.
 446        // Otherwise use the start of the hover range
 447        let anchor = self
 448            .diagnostic_popover
 449            .as_ref()
 450            .map(|diagnostic_popover| &diagnostic_popover.local_diagnostic.range.start)
 451            .or_else(|| {
 452                self.info_popover
 453                    .as_ref()
 454                    .map(|info_popover| match &info_popover.symbol_range {
 455                        RangeInEditor::Text(range) => &range.start,
 456                        RangeInEditor::Inlay(range) => &range.inlay_position,
 457                    })
 458            })?;
 459        let point = anchor.to_display_point(&snapshot.display_snapshot);
 460
 461        // Don't render if the relevant point isn't on screen
 462        if !self.visible() || !visible_rows.contains(&point.row()) {
 463            return None;
 464        }
 465
 466        let mut elements = Vec::new();
 467
 468        if let Some(diagnostic_popover) = self.diagnostic_popover.as_ref() {
 469            elements.push(diagnostic_popover.render(style, max_size, cx));
 470        }
 471        if let Some(info_popover) = self.info_popover.as_mut() {
 472            elements.push(info_popover.render(style, max_size, workspace, cx));
 473        }
 474
 475        Some((point, elements))
 476    }
 477}
 478
 479#[derive(Debug, Clone)]
 480pub struct InfoPopover {
 481    pub project: Model<Project>,
 482    symbol_range: RangeInEditor,
 483    pub blocks: Vec<HoverBlock>,
 484    parsed_content: ParsedMarkdown,
 485}
 486
 487impl InfoPopover {
 488    pub fn render(
 489        &mut self,
 490        style: &EditorStyle,
 491        max_size: Size<Pixels>,
 492        workspace: Option<WeakView<Workspace>>,
 493        cx: &mut ViewContext<Editor>,
 494    ) -> AnyElement {
 495        div()
 496            .id("info_popover")
 497            .elevation_2(cx)
 498            .p_2()
 499            .overflow_y_scroll()
 500            .max_w(max_size.width)
 501            .max_h(max_size.height)
 502            // Prevent a mouse move on the popover from being propagated to the editor,
 503            // because that would dismiss the popover.
 504            .on_mouse_move(|_, cx| cx.stop_propagation())
 505            .child(crate::render_parsed_markdown(
 506                "content",
 507                &self.parsed_content,
 508                style,
 509                workspace,
 510                cx,
 511            ))
 512            .into_any_element()
 513    }
 514}
 515
 516#[derive(Debug, Clone)]
 517pub struct DiagnosticPopover {
 518    local_diagnostic: DiagnosticEntry<Anchor>,
 519    primary_diagnostic: Option<DiagnosticEntry<Anchor>>,
 520}
 521
 522impl DiagnosticPopover {
 523    pub fn render(
 524        &self,
 525        style: &EditorStyle,
 526        max_size: Size<Pixels>,
 527        cx: &mut ViewContext<Editor>,
 528    ) -> AnyElement {
 529        let text = match &self.local_diagnostic.diagnostic.source {
 530            Some(source) => format!("{source}: {}", self.local_diagnostic.diagnostic.message),
 531            None => self.local_diagnostic.diagnostic.message.clone(),
 532        };
 533
 534        let status_colors = cx.theme().status();
 535
 536        struct DiagnosticColors {
 537            pub background: Hsla,
 538            pub border: Hsla,
 539        }
 540
 541        let diagnostic_colors = match self.local_diagnostic.diagnostic.severity {
 542            DiagnosticSeverity::ERROR => DiagnosticColors {
 543                background: status_colors.error_background,
 544                border: status_colors.error_border,
 545            },
 546            DiagnosticSeverity::WARNING => DiagnosticColors {
 547                background: status_colors.warning_background,
 548                border: status_colors.warning_border,
 549            },
 550            DiagnosticSeverity::INFORMATION => DiagnosticColors {
 551                background: status_colors.info_background,
 552                border: status_colors.info_border,
 553            },
 554            DiagnosticSeverity::HINT => DiagnosticColors {
 555                background: status_colors.hint_background,
 556                border: status_colors.hint_border,
 557            },
 558            _ => DiagnosticColors {
 559                background: status_colors.ignored_background,
 560                border: status_colors.ignored_border,
 561            },
 562        };
 563
 564        div()
 565            .id("diagnostic")
 566            .elevation_2(cx)
 567            .overflow_y_scroll()
 568            .px_2()
 569            .py_1()
 570            .bg(diagnostic_colors.background)
 571            .text_color(style.text.color)
 572            .border_1()
 573            .border_color(diagnostic_colors.border)
 574            .rounded_md()
 575            .max_w(max_size.width)
 576            .max_h(max_size.height)
 577            .cursor(CursorStyle::PointingHand)
 578            .tooltip(move |cx| Tooltip::for_action("Go To Diagnostic", &crate::GoToDiagnostic, cx))
 579            // Prevent a mouse move on the popover from being propagated to the editor,
 580            // because that would dismiss the popover.
 581            .on_mouse_move(|_, cx| cx.stop_propagation())
 582            // Prevent a mouse down on the popover from being propagated to the editor,
 583            // because that would move the cursor.
 584            .on_mouse_down(MouseButton::Left, |_, cx| cx.stop_propagation())
 585            .on_click(cx.listener(|editor, _, cx| editor.go_to_diagnostic(&Default::default(), cx)))
 586            .child(SharedString::from(text))
 587            .into_any_element()
 588    }
 589
 590    pub fn activation_info(&self) -> (usize, Anchor) {
 591        let entry = self
 592            .primary_diagnostic
 593            .as_ref()
 594            .unwrap_or(&self.local_diagnostic);
 595
 596        (entry.diagnostic.group_id, entry.range.start)
 597    }
 598}
 599
 600#[cfg(test)]
 601mod tests {
 602    use super::*;
 603    use crate::{
 604        editor_tests::init_test,
 605        element::PointForPosition,
 606        hover_links::update_inlay_link_and_hover_points,
 607        inlay_hint_cache::tests::{cached_hint_labels, visible_hint_labels},
 608        test::editor_lsp_test_context::EditorLspTestContext,
 609        InlayId,
 610    };
 611    use collections::BTreeSet;
 612    use gpui::{FontWeight, HighlightStyle, UnderlineStyle};
 613    use indoc::indoc;
 614    use language::{language_settings::InlayHintSettings, Diagnostic, DiagnosticSet};
 615    use lsp::LanguageServerId;
 616    use project::{HoverBlock, HoverBlockKind};
 617    use smol::stream::StreamExt;
 618    use unindent::Unindent;
 619    use util::test::marked_text_ranges;
 620
 621    #[gpui::test]
 622    async fn test_mouse_hover_info_popover(cx: &mut gpui::TestAppContext) {
 623        init_test(cx, |_| {});
 624
 625        let mut cx = EditorLspTestContext::new_rust(
 626            lsp::ServerCapabilities {
 627                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 628                ..Default::default()
 629            },
 630            cx,
 631        )
 632        .await;
 633
 634        // Basic hover delays and then pops without moving the mouse
 635        cx.set_state(indoc! {"
 636            fn ˇtest() { println!(); }
 637        "});
 638        let hover_point = cx.display_point(indoc! {"
 639            fn test() { printˇln!(); }
 640        "});
 641
 642        cx.update_editor(|editor, cx| hover_at(editor, Some(hover_point), cx));
 643        assert!(!cx.editor(|editor, _| editor.hover_state.visible()));
 644
 645        // After delay, hover should be visible.
 646        let symbol_range = cx.lsp_range(indoc! {"
 647            fn test() { «println!»(); }
 648        "});
 649        let mut requests =
 650            cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 651                Ok(Some(lsp::Hover {
 652                    contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 653                        kind: lsp::MarkupKind::Markdown,
 654                        value: "some basic docs".to_string(),
 655                    }),
 656                    range: Some(symbol_range),
 657                }))
 658            });
 659        cx.background_executor
 660            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
 661        requests.next().await;
 662
 663        cx.editor(|editor, _| {
 664            assert!(editor.hover_state.visible());
 665            assert_eq!(
 666                editor.hover_state.info_popover.clone().unwrap().blocks,
 667                vec![HoverBlock {
 668                    text: "some basic docs".to_string(),
 669                    kind: HoverBlockKind::Markdown,
 670                },]
 671            )
 672        });
 673
 674        // Mouse moved with no hover response dismisses
 675        let hover_point = cx.display_point(indoc! {"
 676            fn teˇst() { println!(); }
 677        "});
 678        let mut request = cx
 679            .lsp
 680            .handle_request::<lsp::request::HoverRequest, _, _>(|_, _| async move { Ok(None) });
 681        cx.update_editor(|editor, cx| hover_at(editor, Some(hover_point), cx));
 682        cx.background_executor
 683            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
 684        request.next().await;
 685        cx.editor(|editor, _| {
 686            assert!(!editor.hover_state.visible());
 687        });
 688    }
 689
 690    #[gpui::test]
 691    async fn test_keyboard_hover_info_popover(cx: &mut gpui::TestAppContext) {
 692        init_test(cx, |_| {});
 693
 694        let mut cx = EditorLspTestContext::new_rust(
 695            lsp::ServerCapabilities {
 696                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 697                ..Default::default()
 698            },
 699            cx,
 700        )
 701        .await;
 702
 703        // Hover with keyboard has no delay
 704        cx.set_state(indoc! {"
 705            fˇn test() { println!(); }
 706        "});
 707        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 708        let symbol_range = cx.lsp_range(indoc! {"
 709            «fn» test() { println!(); }
 710        "});
 711        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 712            Ok(Some(lsp::Hover {
 713                contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 714                    kind: lsp::MarkupKind::Markdown,
 715                    value: "some other basic docs".to_string(),
 716                }),
 717                range: Some(symbol_range),
 718            }))
 719        })
 720        .next()
 721        .await;
 722
 723        cx.condition(|editor, _| editor.hover_state.visible()).await;
 724        cx.editor(|editor, _| {
 725            assert_eq!(
 726                editor.hover_state.info_popover.clone().unwrap().blocks,
 727                vec![HoverBlock {
 728                    text: "some other basic docs".to_string(),
 729                    kind: HoverBlockKind::Markdown,
 730                }]
 731            )
 732        });
 733    }
 734
 735    #[gpui::test]
 736    async fn test_empty_hovers_filtered(cx: &mut gpui::TestAppContext) {
 737        init_test(cx, |_| {});
 738
 739        let mut cx = EditorLspTestContext::new_rust(
 740            lsp::ServerCapabilities {
 741                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 742                ..Default::default()
 743            },
 744            cx,
 745        )
 746        .await;
 747
 748        // Hover with keyboard has no delay
 749        cx.set_state(indoc! {"
 750            fˇn test() { println!(); }
 751        "});
 752        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 753        let symbol_range = cx.lsp_range(indoc! {"
 754            «fn» test() { println!(); }
 755        "});
 756        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 757            Ok(Some(lsp::Hover {
 758                contents: lsp::HoverContents::Array(vec![
 759                    lsp::MarkedString::String("regular text for hover to show".to_string()),
 760                    lsp::MarkedString::String("".to_string()),
 761                    lsp::MarkedString::LanguageString(lsp::LanguageString {
 762                        language: "Rust".to_string(),
 763                        value: "".to_string(),
 764                    }),
 765                ]),
 766                range: Some(symbol_range),
 767            }))
 768        })
 769        .next()
 770        .await;
 771
 772        cx.condition(|editor, _| editor.hover_state.visible()).await;
 773        cx.editor(|editor, _| {
 774            assert_eq!(
 775                editor.hover_state.info_popover.clone().unwrap().blocks,
 776                vec![HoverBlock {
 777                    text: "regular text for hover to show".to_string(),
 778                    kind: HoverBlockKind::Markdown,
 779                }],
 780                "No empty string hovers should be shown"
 781            );
 782        });
 783    }
 784
 785    #[gpui::test]
 786    async fn test_line_ends_trimmed(cx: &mut gpui::TestAppContext) {
 787        init_test(cx, |_| {});
 788
 789        let mut cx = EditorLspTestContext::new_rust(
 790            lsp::ServerCapabilities {
 791                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 792                ..Default::default()
 793            },
 794            cx,
 795        )
 796        .await;
 797
 798        // Hover with keyboard has no delay
 799        cx.set_state(indoc! {"
 800            fˇn test() { println!(); }
 801        "});
 802        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 803        let symbol_range = cx.lsp_range(indoc! {"
 804            «fn» test() { println!(); }
 805        "});
 806
 807        let code_str = "\nlet hovered_point: Vector2F // size = 8, align = 0x4\n";
 808        let markdown_string = format!("\n```rust\n{code_str}```");
 809
 810        let closure_markdown_string = markdown_string.clone();
 811        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| {
 812            let future_markdown_string = closure_markdown_string.clone();
 813            async move {
 814                Ok(Some(lsp::Hover {
 815                    contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 816                        kind: lsp::MarkupKind::Markdown,
 817                        value: future_markdown_string,
 818                    }),
 819                    range: Some(symbol_range),
 820                }))
 821            }
 822        })
 823        .next()
 824        .await;
 825
 826        cx.condition(|editor, _| editor.hover_state.visible()).await;
 827        cx.editor(|editor, _| {
 828            let blocks = editor.hover_state.info_popover.clone().unwrap().blocks;
 829            assert_eq!(
 830                blocks,
 831                vec![HoverBlock {
 832                    text: markdown_string,
 833                    kind: HoverBlockKind::Markdown,
 834                }],
 835            );
 836
 837            let rendered = smol::block_on(parse_blocks(&blocks, &Default::default(), None));
 838            assert_eq!(
 839                rendered.text,
 840                code_str.trim(),
 841                "Should not have extra line breaks at end of rendered hover"
 842            );
 843        });
 844    }
 845
 846    #[gpui::test]
 847    async fn test_hover_diagnostic_and_info_popovers(cx: &mut gpui::TestAppContext) {
 848        init_test(cx, |_| {});
 849
 850        let mut cx = EditorLspTestContext::new_rust(
 851            lsp::ServerCapabilities {
 852                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 853                ..Default::default()
 854            },
 855            cx,
 856        )
 857        .await;
 858
 859        // Hover with just diagnostic, pops DiagnosticPopover immediately and then
 860        // info popover once request completes
 861        cx.set_state(indoc! {"
 862            fn teˇst() { println!(); }
 863        "});
 864
 865        // Send diagnostic to client
 866        let range = cx.text_anchor_range(indoc! {"
 867            fn «test»() { println!(); }
 868        "});
 869        cx.update_buffer(|buffer, cx| {
 870            let snapshot = buffer.text_snapshot();
 871            let set = DiagnosticSet::from_sorted_entries(
 872                vec![DiagnosticEntry {
 873                    range,
 874                    diagnostic: Diagnostic {
 875                        message: "A test diagnostic message.".to_string(),
 876                        ..Default::default()
 877                    },
 878                }],
 879                &snapshot,
 880            );
 881            buffer.update_diagnostics(LanguageServerId(0), set, cx);
 882        });
 883
 884        // Hover pops diagnostic immediately
 885        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 886        cx.background_executor.run_until_parked();
 887
 888        cx.editor(|Editor { hover_state, .. }, _| {
 889            assert!(hover_state.diagnostic_popover.is_some() && hover_state.info_popover.is_none())
 890        });
 891
 892        // Info Popover shows after request responded to
 893        let range = cx.lsp_range(indoc! {"
 894            fn «test»() { println!(); }
 895        "});
 896        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 897            Ok(Some(lsp::Hover {
 898                contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 899                    kind: lsp::MarkupKind::Markdown,
 900                    value: "some new docs".to_string(),
 901                }),
 902                range: Some(range),
 903            }))
 904        });
 905        cx.background_executor
 906            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
 907
 908        cx.background_executor.run_until_parked();
 909        cx.editor(|Editor { hover_state, .. }, _| {
 910            hover_state.diagnostic_popover.is_some() && hover_state.info_task.is_some()
 911        });
 912    }
 913
 914    #[gpui::test]
 915    fn test_render_blocks(cx: &mut gpui::TestAppContext) {
 916        init_test(cx, |_| {});
 917
 918        let editor = cx.add_window(|cx| Editor::single_line(cx));
 919        editor
 920            .update(cx, |editor, _cx| {
 921                let style = editor.style.clone().unwrap();
 922
 923                struct Row {
 924                    blocks: Vec<HoverBlock>,
 925                    expected_marked_text: String,
 926                    expected_styles: Vec<HighlightStyle>,
 927                }
 928
 929                let rows = &[
 930                    // Strong emphasis
 931                    Row {
 932                        blocks: vec![HoverBlock {
 933                            text: "one **two** three".to_string(),
 934                            kind: HoverBlockKind::Markdown,
 935                        }],
 936                        expected_marked_text: "one «two» three".to_string(),
 937                        expected_styles: vec![HighlightStyle {
 938                            font_weight: Some(FontWeight::BOLD),
 939                            ..Default::default()
 940                        }],
 941                    },
 942                    // Links
 943                    Row {
 944                        blocks: vec![HoverBlock {
 945                            text: "one [two](https://the-url) three".to_string(),
 946                            kind: HoverBlockKind::Markdown,
 947                        }],
 948                        expected_marked_text: "one «two» three".to_string(),
 949                        expected_styles: vec![HighlightStyle {
 950                            underline: Some(UnderlineStyle {
 951                                thickness: 1.0.into(),
 952                                ..Default::default()
 953                            }),
 954                            ..Default::default()
 955                        }],
 956                    },
 957                    // Lists
 958                    Row {
 959                        blocks: vec![HoverBlock {
 960                            text: "
 961                            lists:
 962                            * one
 963                                - a
 964                                - b
 965                            * two
 966                                - [c](https://the-url)
 967                                - d"
 968                            .unindent(),
 969                            kind: HoverBlockKind::Markdown,
 970                        }],
 971                        expected_marked_text: "
 972                        lists:
 973                        - one
 974                          - a
 975                          - b
 976                        - two
 977                          - «c»
 978                          - d"
 979                        .unindent(),
 980                        expected_styles: vec![HighlightStyle {
 981                            underline: Some(UnderlineStyle {
 982                                thickness: 1.0.into(),
 983                                ..Default::default()
 984                            }),
 985                            ..Default::default()
 986                        }],
 987                    },
 988                    // Multi-paragraph list items
 989                    Row {
 990                        blocks: vec![HoverBlock {
 991                            text: "
 992                            * one two
 993                              three
 994
 995                            * four five
 996                                * six seven
 997                                  eight
 998
 999                                  nine
1000                                * ten
1001                            * six"
1002                                .unindent(),
1003                            kind: HoverBlockKind::Markdown,
1004                        }],
1005                        expected_marked_text: "
1006                        - one two three
1007                        - four five
1008                          - six seven eight
1009
1010                            nine
1011                          - ten
1012                        - six"
1013                            .unindent(),
1014                        expected_styles: vec![HighlightStyle {
1015                            underline: Some(UnderlineStyle {
1016                                thickness: 1.0.into(),
1017                                ..Default::default()
1018                            }),
1019                            ..Default::default()
1020                        }],
1021                    },
1022                ];
1023
1024                for Row {
1025                    blocks,
1026                    expected_marked_text,
1027                    expected_styles,
1028                } in &rows[0..]
1029                {
1030                    let rendered = smol::block_on(parse_blocks(&blocks, &Default::default(), None));
1031
1032                    let (expected_text, ranges) = marked_text_ranges(expected_marked_text, false);
1033                    let expected_highlights = ranges
1034                        .into_iter()
1035                        .zip(expected_styles.iter().cloned())
1036                        .collect::<Vec<_>>();
1037                    assert_eq!(
1038                        rendered.text, expected_text,
1039                        "wrong text for input {blocks:?}"
1040                    );
1041
1042                    let rendered_highlights: Vec<_> = rendered
1043                        .highlights
1044                        .iter()
1045                        .filter_map(|(range, highlight)| {
1046                            let highlight = highlight.to_highlight_style(&style.syntax)?;
1047                            Some((range.clone(), highlight))
1048                        })
1049                        .collect();
1050
1051                    assert_eq!(
1052                        rendered_highlights, expected_highlights,
1053                        "wrong highlights for input {blocks:?}"
1054                    );
1055                }
1056            })
1057            .unwrap();
1058    }
1059
1060    #[gpui::test]
1061    async fn test_hover_inlay_label_parts(cx: &mut gpui::TestAppContext) {
1062        init_test(cx, |settings| {
1063            settings.defaults.inlay_hints = Some(InlayHintSettings {
1064                enabled: true,
1065                edit_debounce_ms: 0,
1066                scroll_debounce_ms: 0,
1067                show_type_hints: true,
1068                show_parameter_hints: true,
1069                show_other_hints: true,
1070            })
1071        });
1072
1073        let mut cx = EditorLspTestContext::new_rust(
1074            lsp::ServerCapabilities {
1075                inlay_hint_provider: Some(lsp::OneOf::Right(
1076                    lsp::InlayHintServerCapabilities::Options(lsp::InlayHintOptions {
1077                        resolve_provider: Some(true),
1078                        ..Default::default()
1079                    }),
1080                )),
1081                ..Default::default()
1082            },
1083            cx,
1084        )
1085        .await;
1086
1087        cx.set_state(indoc! {"
1088            struct TestStruct;
1089
1090            // ==================
1091
1092            struct TestNewType<T>(T);
1093
1094            fn main() {
1095                let variableˇ = TestNewType(TestStruct);
1096            }
1097        "});
1098
1099        let hint_start_offset = cx.ranges(indoc! {"
1100            struct TestStruct;
1101
1102            // ==================
1103
1104            struct TestNewType<T>(T);
1105
1106            fn main() {
1107                let variableˇ = TestNewType(TestStruct);
1108            }
1109        "})[0]
1110            .start;
1111        let hint_position = cx.to_lsp(hint_start_offset);
1112        let new_type_target_range = cx.lsp_range(indoc! {"
1113            struct TestStruct;
1114
1115            // ==================
1116
1117            struct «TestNewType»<T>(T);
1118
1119            fn main() {
1120                let variable = TestNewType(TestStruct);
1121            }
1122        "});
1123        let struct_target_range = cx.lsp_range(indoc! {"
1124            struct «TestStruct»;
1125
1126            // ==================
1127
1128            struct TestNewType<T>(T);
1129
1130            fn main() {
1131                let variable = TestNewType(TestStruct);
1132            }
1133        "});
1134
1135        let uri = cx.buffer_lsp_url.clone();
1136        let new_type_label = "TestNewType";
1137        let struct_label = "TestStruct";
1138        let entire_hint_label = ": TestNewType<TestStruct>";
1139        let closure_uri = uri.clone();
1140        cx.lsp
1141            .handle_request::<lsp::request::InlayHintRequest, _, _>(move |params, _| {
1142                let task_uri = closure_uri.clone();
1143                async move {
1144                    assert_eq!(params.text_document.uri, task_uri);
1145                    Ok(Some(vec![lsp::InlayHint {
1146                        position: hint_position,
1147                        label: lsp::InlayHintLabel::LabelParts(vec![lsp::InlayHintLabelPart {
1148                            value: entire_hint_label.to_string(),
1149                            ..Default::default()
1150                        }]),
1151                        kind: Some(lsp::InlayHintKind::TYPE),
1152                        text_edits: None,
1153                        tooltip: None,
1154                        padding_left: Some(false),
1155                        padding_right: Some(false),
1156                        data: None,
1157                    }]))
1158                }
1159            })
1160            .next()
1161            .await;
1162        cx.background_executor.run_until_parked();
1163        cx.update_editor(|editor, cx| {
1164            let expected_layers = vec![entire_hint_label.to_string()];
1165            assert_eq!(expected_layers, cached_hint_labels(editor));
1166            assert_eq!(expected_layers, visible_hint_labels(editor, cx));
1167        });
1168
1169        let inlay_range = cx
1170            .ranges(indoc! {"
1171                struct TestStruct;
1172
1173                // ==================
1174
1175                struct TestNewType<T>(T);
1176
1177                fn main() {
1178                    let variable« »= TestNewType(TestStruct);
1179                }
1180        "})
1181            .get(0)
1182            .cloned()
1183            .unwrap();
1184        let new_type_hint_part_hover_position = cx.update_editor(|editor, cx| {
1185            let snapshot = editor.snapshot(cx);
1186            let previous_valid = inlay_range.start.to_display_point(&snapshot);
1187            let next_valid = inlay_range.end.to_display_point(&snapshot);
1188            assert_eq!(previous_valid.row(), next_valid.row());
1189            assert!(previous_valid.column() < next_valid.column());
1190            let exact_unclipped = DisplayPoint::new(
1191                previous_valid.row(),
1192                previous_valid.column()
1193                    + (entire_hint_label.find(new_type_label).unwrap() + new_type_label.len() / 2)
1194                        as u32,
1195            );
1196            PointForPosition {
1197                previous_valid,
1198                next_valid,
1199                exact_unclipped,
1200                column_overshoot_after_line_end: 0,
1201            }
1202        });
1203        cx.update_editor(|editor, cx| {
1204            update_inlay_link_and_hover_points(
1205                &editor.snapshot(cx),
1206                new_type_hint_part_hover_position,
1207                editor,
1208                true,
1209                false,
1210                cx,
1211            );
1212        });
1213
1214        let resolve_closure_uri = uri.clone();
1215        cx.lsp
1216            .handle_request::<lsp::request::InlayHintResolveRequest, _, _>(
1217                move |mut hint_to_resolve, _| {
1218                    let mut resolved_hint_positions = BTreeSet::new();
1219                    let task_uri = resolve_closure_uri.clone();
1220                    async move {
1221                        let inserted = resolved_hint_positions.insert(hint_to_resolve.position);
1222                        assert!(inserted, "Hint {hint_to_resolve:?} was resolved twice");
1223
1224                        // `: TestNewType<TestStruct>`
1225                        hint_to_resolve.label = lsp::InlayHintLabel::LabelParts(vec![
1226                            lsp::InlayHintLabelPart {
1227                                value: ": ".to_string(),
1228                                ..Default::default()
1229                            },
1230                            lsp::InlayHintLabelPart {
1231                                value: new_type_label.to_string(),
1232                                location: Some(lsp::Location {
1233                                    uri: task_uri.clone(),
1234                                    range: new_type_target_range,
1235                                }),
1236                                tooltip: Some(lsp::InlayHintLabelPartTooltip::String(format!(
1237                                    "A tooltip for `{new_type_label}`"
1238                                ))),
1239                                ..Default::default()
1240                            },
1241                            lsp::InlayHintLabelPart {
1242                                value: "<".to_string(),
1243                                ..Default::default()
1244                            },
1245                            lsp::InlayHintLabelPart {
1246                                value: struct_label.to_string(),
1247                                location: Some(lsp::Location {
1248                                    uri: task_uri,
1249                                    range: struct_target_range,
1250                                }),
1251                                tooltip: Some(lsp::InlayHintLabelPartTooltip::MarkupContent(
1252                                    lsp::MarkupContent {
1253                                        kind: lsp::MarkupKind::Markdown,
1254                                        value: format!("A tooltip for `{struct_label}`"),
1255                                    },
1256                                )),
1257                                ..Default::default()
1258                            },
1259                            lsp::InlayHintLabelPart {
1260                                value: ">".to_string(),
1261                                ..Default::default()
1262                            },
1263                        ]);
1264
1265                        Ok(hint_to_resolve)
1266                    }
1267                },
1268            )
1269            .next()
1270            .await;
1271        cx.background_executor.run_until_parked();
1272
1273        cx.update_editor(|editor, cx| {
1274            update_inlay_link_and_hover_points(
1275                &editor.snapshot(cx),
1276                new_type_hint_part_hover_position,
1277                editor,
1278                true,
1279                false,
1280                cx,
1281            );
1282        });
1283        cx.background_executor
1284            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
1285        cx.background_executor.run_until_parked();
1286        cx.update_editor(|editor, cx| {
1287            let hover_state = &editor.hover_state;
1288            assert!(hover_state.diagnostic_popover.is_none() && hover_state.info_popover.is_some());
1289            let popover = hover_state.info_popover.as_ref().unwrap();
1290            let buffer_snapshot = editor.buffer().update(cx, |buffer, cx| buffer.snapshot(cx));
1291            assert_eq!(
1292                popover.symbol_range,
1293                RangeInEditor::Inlay(InlayHighlight {
1294                    inlay: InlayId::Hint(0),
1295                    inlay_position: buffer_snapshot.anchor_at(inlay_range.start, Bias::Right),
1296                    range: ": ".len()..": ".len() + new_type_label.len(),
1297                }),
1298                "Popover range should match the new type label part"
1299            );
1300            assert_eq!(
1301                popover.parsed_content.text,
1302                format!("A tooltip for `{new_type_label}`"),
1303                "Rendered text should not anyhow alter backticks"
1304            );
1305        });
1306
1307        let struct_hint_part_hover_position = cx.update_editor(|editor, cx| {
1308            let snapshot = editor.snapshot(cx);
1309            let previous_valid = inlay_range.start.to_display_point(&snapshot);
1310            let next_valid = inlay_range.end.to_display_point(&snapshot);
1311            assert_eq!(previous_valid.row(), next_valid.row());
1312            assert!(previous_valid.column() < next_valid.column());
1313            let exact_unclipped = DisplayPoint::new(
1314                previous_valid.row(),
1315                previous_valid.column()
1316                    + (entire_hint_label.find(struct_label).unwrap() + struct_label.len() / 2)
1317                        as u32,
1318            );
1319            PointForPosition {
1320                previous_valid,
1321                next_valid,
1322                exact_unclipped,
1323                column_overshoot_after_line_end: 0,
1324            }
1325        });
1326        cx.update_editor(|editor, cx| {
1327            update_inlay_link_and_hover_points(
1328                &editor.snapshot(cx),
1329                struct_hint_part_hover_position,
1330                editor,
1331                true,
1332                false,
1333                cx,
1334            );
1335        });
1336        cx.background_executor
1337            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
1338        cx.background_executor.run_until_parked();
1339        cx.update_editor(|editor, cx| {
1340            let hover_state = &editor.hover_state;
1341            assert!(hover_state.diagnostic_popover.is_none() && hover_state.info_popover.is_some());
1342            let popover = hover_state.info_popover.as_ref().unwrap();
1343            let buffer_snapshot = editor.buffer().update(cx, |buffer, cx| buffer.snapshot(cx));
1344            assert_eq!(
1345                popover.symbol_range,
1346                RangeInEditor::Inlay(InlayHighlight {
1347                    inlay: InlayId::Hint(0),
1348                    inlay_position: buffer_snapshot.anchor_at(inlay_range.start, Bias::Right),
1349                    range: ": ".len() + new_type_label.len() + "<".len()
1350                        ..": ".len() + new_type_label.len() + "<".len() + struct_label.len(),
1351                }),
1352                "Popover range should match the struct label part"
1353            );
1354            assert_eq!(
1355                popover.parsed_content.text,
1356                format!("A tooltip for {struct_label}"),
1357                "Rendered markdown element should remove backticks from text"
1358            );
1359        });
1360    }
1361}