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 down/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            .on_mouse_down(MouseButton::Left, |_, cx| cx.stop_propagation())
 506            .child(crate::render_parsed_markdown(
 507                "content",
 508                &self.parsed_content,
 509                style,
 510                workspace,
 511                cx,
 512            ))
 513            .into_any_element()
 514    }
 515}
 516
 517#[derive(Debug, Clone)]
 518pub struct DiagnosticPopover {
 519    local_diagnostic: DiagnosticEntry<Anchor>,
 520    primary_diagnostic: Option<DiagnosticEntry<Anchor>>,
 521}
 522
 523impl DiagnosticPopover {
 524    pub fn render(
 525        &self,
 526        style: &EditorStyle,
 527        max_size: Size<Pixels>,
 528        cx: &mut ViewContext<Editor>,
 529    ) -> AnyElement {
 530        let text = match &self.local_diagnostic.diagnostic.source {
 531            Some(source) => format!("{source}: {}", self.local_diagnostic.diagnostic.message),
 532            None => self.local_diagnostic.diagnostic.message.clone(),
 533        };
 534
 535        let status_colors = cx.theme().status();
 536
 537        struct DiagnosticColors {
 538            pub background: Hsla,
 539            pub border: Hsla,
 540        }
 541
 542        let diagnostic_colors = match self.local_diagnostic.diagnostic.severity {
 543            DiagnosticSeverity::ERROR => DiagnosticColors {
 544                background: status_colors.error_background,
 545                border: status_colors.error_border,
 546            },
 547            DiagnosticSeverity::WARNING => DiagnosticColors {
 548                background: status_colors.warning_background,
 549                border: status_colors.warning_border,
 550            },
 551            DiagnosticSeverity::INFORMATION => DiagnosticColors {
 552                background: status_colors.info_background,
 553                border: status_colors.info_border,
 554            },
 555            DiagnosticSeverity::HINT => DiagnosticColors {
 556                background: status_colors.hint_background,
 557                border: status_colors.hint_border,
 558            },
 559            _ => DiagnosticColors {
 560                background: status_colors.ignored_background,
 561                border: status_colors.ignored_border,
 562            },
 563        };
 564
 565        div()
 566            .id("diagnostic")
 567            .block()
 568            .elevation_2(cx)
 569            .overflow_y_scroll()
 570            .px_2()
 571            .py_1()
 572            .bg(diagnostic_colors.background)
 573            .text_color(style.text.color)
 574            .border_1()
 575            .border_color(diagnostic_colors.border)
 576            .rounded_md()
 577            .max_w(max_size.width)
 578            .max_h(max_size.height)
 579            .cursor(CursorStyle::PointingHand)
 580            .tooltip(move |cx| Tooltip::for_action("Go To Diagnostic", &crate::GoToDiagnostic, cx))
 581            // Prevent a mouse move on the popover from being propagated to the editor,
 582            // because that would dismiss the popover.
 583            .on_mouse_move(|_, cx| cx.stop_propagation())
 584            // Prevent a mouse down on the popover from being propagated to the editor,
 585            // because that would move the cursor.
 586            .on_mouse_down(MouseButton::Left, |_, cx| cx.stop_propagation())
 587            .on_click(cx.listener(|editor, _, cx| editor.go_to_diagnostic(&Default::default(), cx)))
 588            .child(SharedString::from(text))
 589            .into_any_element()
 590    }
 591
 592    pub fn activation_info(&self) -> (usize, Anchor) {
 593        let entry = self
 594            .primary_diagnostic
 595            .as_ref()
 596            .unwrap_or(&self.local_diagnostic);
 597
 598        (entry.diagnostic.group_id, entry.range.start)
 599    }
 600}
 601
 602#[cfg(test)]
 603mod tests {
 604    use super::*;
 605    use crate::{
 606        editor_tests::init_test,
 607        hover_links::update_inlay_link_and_hover_points,
 608        inlay_hint_cache::tests::{cached_hint_labels, visible_hint_labels},
 609        test::editor_lsp_test_context::EditorLspTestContext,
 610        InlayId, PointForPosition,
 611    };
 612    use collections::BTreeSet;
 613    use gpui::{FontWeight, HighlightStyle, UnderlineStyle};
 614    use indoc::indoc;
 615    use language::{language_settings::InlayHintSettings, Diagnostic, DiagnosticSet};
 616    use lsp::LanguageServerId;
 617    use project::{HoverBlock, HoverBlockKind};
 618    use smol::stream::StreamExt;
 619    use unindent::Unindent;
 620    use util::test::marked_text_ranges;
 621
 622    #[gpui::test]
 623    async fn test_mouse_hover_info_popover(cx: &mut gpui::TestAppContext) {
 624        init_test(cx, |_| {});
 625
 626        let mut cx = EditorLspTestContext::new_rust(
 627            lsp::ServerCapabilities {
 628                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 629                ..Default::default()
 630            },
 631            cx,
 632        )
 633        .await;
 634
 635        // Basic hover delays and then pops without moving the mouse
 636        cx.set_state(indoc! {"
 637            fn ˇtest() { println!(); }
 638        "});
 639        let hover_point = cx.display_point(indoc! {"
 640            fn test() { printˇln!(); }
 641        "});
 642
 643        cx.update_editor(|editor, cx| hover_at(editor, Some(hover_point), cx));
 644        assert!(!cx.editor(|editor, _| editor.hover_state.visible()));
 645
 646        // After delay, hover should be visible.
 647        let symbol_range = cx.lsp_range(indoc! {"
 648            fn test() { «println!»(); }
 649        "});
 650        let mut requests =
 651            cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 652                Ok(Some(lsp::Hover {
 653                    contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 654                        kind: lsp::MarkupKind::Markdown,
 655                        value: "some basic docs".to_string(),
 656                    }),
 657                    range: Some(symbol_range),
 658                }))
 659            });
 660        cx.background_executor
 661            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
 662        requests.next().await;
 663
 664        cx.editor(|editor, _| {
 665            assert!(editor.hover_state.visible());
 666            assert_eq!(
 667                editor.hover_state.info_popover.clone().unwrap().blocks,
 668                vec![HoverBlock {
 669                    text: "some basic docs".to_string(),
 670                    kind: HoverBlockKind::Markdown,
 671                },]
 672            )
 673        });
 674
 675        // Mouse moved with no hover response dismisses
 676        let hover_point = cx.display_point(indoc! {"
 677            fn teˇst() { println!(); }
 678        "});
 679        let mut request = cx
 680            .lsp
 681            .handle_request::<lsp::request::HoverRequest, _, _>(|_, _| async move { Ok(None) });
 682        cx.update_editor(|editor, cx| hover_at(editor, Some(hover_point), cx));
 683        cx.background_executor
 684            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
 685        request.next().await;
 686        cx.editor(|editor, _| {
 687            assert!(!editor.hover_state.visible());
 688        });
 689    }
 690
 691    #[gpui::test]
 692    async fn test_keyboard_hover_info_popover(cx: &mut gpui::TestAppContext) {
 693        init_test(cx, |_| {});
 694
 695        let mut cx = EditorLspTestContext::new_rust(
 696            lsp::ServerCapabilities {
 697                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 698                ..Default::default()
 699            },
 700            cx,
 701        )
 702        .await;
 703
 704        // Hover with keyboard has no delay
 705        cx.set_state(indoc! {"
 706            fˇn test() { println!(); }
 707        "});
 708        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 709        let symbol_range = cx.lsp_range(indoc! {"
 710            «fn» test() { println!(); }
 711        "});
 712        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 713            Ok(Some(lsp::Hover {
 714                contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 715                    kind: lsp::MarkupKind::Markdown,
 716                    value: "some other basic docs".to_string(),
 717                }),
 718                range: Some(symbol_range),
 719            }))
 720        })
 721        .next()
 722        .await;
 723
 724        cx.condition(|editor, _| editor.hover_state.visible()).await;
 725        cx.editor(|editor, _| {
 726            assert_eq!(
 727                editor.hover_state.info_popover.clone().unwrap().blocks,
 728                vec![HoverBlock {
 729                    text: "some other basic docs".to_string(),
 730                    kind: HoverBlockKind::Markdown,
 731                }]
 732            )
 733        });
 734    }
 735
 736    #[gpui::test]
 737    async fn test_empty_hovers_filtered(cx: &mut gpui::TestAppContext) {
 738        init_test(cx, |_| {});
 739
 740        let mut cx = EditorLspTestContext::new_rust(
 741            lsp::ServerCapabilities {
 742                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 743                ..Default::default()
 744            },
 745            cx,
 746        )
 747        .await;
 748
 749        // Hover with keyboard has no delay
 750        cx.set_state(indoc! {"
 751            fˇn test() { println!(); }
 752        "});
 753        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 754        let symbol_range = cx.lsp_range(indoc! {"
 755            «fn» test() { println!(); }
 756        "});
 757        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 758            Ok(Some(lsp::Hover {
 759                contents: lsp::HoverContents::Array(vec![
 760                    lsp::MarkedString::String("regular text for hover to show".to_string()),
 761                    lsp::MarkedString::String("".to_string()),
 762                    lsp::MarkedString::LanguageString(lsp::LanguageString {
 763                        language: "Rust".to_string(),
 764                        value: "".to_string(),
 765                    }),
 766                ]),
 767                range: Some(symbol_range),
 768            }))
 769        })
 770        .next()
 771        .await;
 772
 773        cx.condition(|editor, _| editor.hover_state.visible()).await;
 774        cx.editor(|editor, _| {
 775            assert_eq!(
 776                editor.hover_state.info_popover.clone().unwrap().blocks,
 777                vec![HoverBlock {
 778                    text: "regular text for hover to show".to_string(),
 779                    kind: HoverBlockKind::Markdown,
 780                }],
 781                "No empty string hovers should be shown"
 782            );
 783        });
 784    }
 785
 786    #[gpui::test]
 787    async fn test_line_ends_trimmed(cx: &mut gpui::TestAppContext) {
 788        init_test(cx, |_| {});
 789
 790        let mut cx = EditorLspTestContext::new_rust(
 791            lsp::ServerCapabilities {
 792                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 793                ..Default::default()
 794            },
 795            cx,
 796        )
 797        .await;
 798
 799        // Hover with keyboard has no delay
 800        cx.set_state(indoc! {"
 801            fˇn test() { println!(); }
 802        "});
 803        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 804        let symbol_range = cx.lsp_range(indoc! {"
 805            «fn» test() { println!(); }
 806        "});
 807
 808        let code_str = "\nlet hovered_point: Vector2F // size = 8, align = 0x4\n";
 809        let markdown_string = format!("\n```rust\n{code_str}```");
 810
 811        let closure_markdown_string = markdown_string.clone();
 812        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| {
 813            let future_markdown_string = closure_markdown_string.clone();
 814            async move {
 815                Ok(Some(lsp::Hover {
 816                    contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 817                        kind: lsp::MarkupKind::Markdown,
 818                        value: future_markdown_string,
 819                    }),
 820                    range: Some(symbol_range),
 821                }))
 822            }
 823        })
 824        .next()
 825        .await;
 826
 827        cx.condition(|editor, _| editor.hover_state.visible()).await;
 828        cx.editor(|editor, _| {
 829            let blocks = editor.hover_state.info_popover.clone().unwrap().blocks;
 830            assert_eq!(
 831                blocks,
 832                vec![HoverBlock {
 833                    text: markdown_string,
 834                    kind: HoverBlockKind::Markdown,
 835                }],
 836            );
 837
 838            let rendered = smol::block_on(parse_blocks(&blocks, &Default::default(), None));
 839            assert_eq!(
 840                rendered.text,
 841                code_str.trim(),
 842                "Should not have extra line breaks at end of rendered hover"
 843            );
 844        });
 845    }
 846
 847    #[gpui::test]
 848    async fn test_hover_diagnostic_and_info_popovers(cx: &mut gpui::TestAppContext) {
 849        init_test(cx, |_| {});
 850
 851        let mut cx = EditorLspTestContext::new_rust(
 852            lsp::ServerCapabilities {
 853                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 854                ..Default::default()
 855            },
 856            cx,
 857        )
 858        .await;
 859
 860        // Hover with just diagnostic, pops DiagnosticPopover immediately and then
 861        // info popover once request completes
 862        cx.set_state(indoc! {"
 863            fn teˇst() { println!(); }
 864        "});
 865
 866        // Send diagnostic to client
 867        let range = cx.text_anchor_range(indoc! {"
 868            fn «test»() { println!(); }
 869        "});
 870        cx.update_buffer(|buffer, cx| {
 871            let snapshot = buffer.text_snapshot();
 872            let set = DiagnosticSet::from_sorted_entries(
 873                vec![DiagnosticEntry {
 874                    range,
 875                    diagnostic: Diagnostic {
 876                        message: "A test diagnostic message.".to_string(),
 877                        ..Default::default()
 878                    },
 879                }],
 880                &snapshot,
 881            );
 882            buffer.update_diagnostics(LanguageServerId(0), set, cx);
 883        });
 884
 885        // Hover pops diagnostic immediately
 886        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 887        cx.background_executor.run_until_parked();
 888
 889        cx.editor(|Editor { hover_state, .. }, _| {
 890            assert!(hover_state.diagnostic_popover.is_some() && hover_state.info_popover.is_none())
 891        });
 892
 893        // Info Popover shows after request responded to
 894        let range = cx.lsp_range(indoc! {"
 895            fn «test»() { println!(); }
 896        "});
 897        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 898            Ok(Some(lsp::Hover {
 899                contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 900                    kind: lsp::MarkupKind::Markdown,
 901                    value: "some new docs".to_string(),
 902                }),
 903                range: Some(range),
 904            }))
 905        });
 906        cx.background_executor
 907            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
 908
 909        cx.background_executor.run_until_parked();
 910        cx.editor(|Editor { hover_state, .. }, _| {
 911            hover_state.diagnostic_popover.is_some() && hover_state.info_task.is_some()
 912        });
 913    }
 914
 915    #[gpui::test]
 916    fn test_render_blocks(cx: &mut gpui::TestAppContext) {
 917        init_test(cx, |_| {});
 918
 919        let editor = cx.add_window(|cx| Editor::single_line(cx));
 920        editor
 921            .update(cx, |editor, _cx| {
 922                let style = editor.style.clone().unwrap();
 923
 924                struct Row {
 925                    blocks: Vec<HoverBlock>,
 926                    expected_marked_text: String,
 927                    expected_styles: Vec<HighlightStyle>,
 928                }
 929
 930                let rows = &[
 931                    // Strong emphasis
 932                    Row {
 933                        blocks: vec![HoverBlock {
 934                            text: "one **two** three".to_string(),
 935                            kind: HoverBlockKind::Markdown,
 936                        }],
 937                        expected_marked_text: "one «two» three".to_string(),
 938                        expected_styles: vec![HighlightStyle {
 939                            font_weight: Some(FontWeight::BOLD),
 940                            ..Default::default()
 941                        }],
 942                    },
 943                    // Links
 944                    Row {
 945                        blocks: vec![HoverBlock {
 946                            text: "one [two](https://the-url) three".to_string(),
 947                            kind: HoverBlockKind::Markdown,
 948                        }],
 949                        expected_marked_text: "one «two» three".to_string(),
 950                        expected_styles: vec![HighlightStyle {
 951                            underline: Some(UnderlineStyle {
 952                                thickness: 1.0.into(),
 953                                ..Default::default()
 954                            }),
 955                            ..Default::default()
 956                        }],
 957                    },
 958                    // Lists
 959                    Row {
 960                        blocks: vec![HoverBlock {
 961                            text: "
 962                            lists:
 963                            * one
 964                                - a
 965                                - b
 966                            * two
 967                                - [c](https://the-url)
 968                                - d"
 969                            .unindent(),
 970                            kind: HoverBlockKind::Markdown,
 971                        }],
 972                        expected_marked_text: "
 973                        lists:
 974                        - one
 975                          - a
 976                          - b
 977                        - two
 978                          - «c»
 979                          - d"
 980                        .unindent(),
 981                        expected_styles: vec![HighlightStyle {
 982                            underline: Some(UnderlineStyle {
 983                                thickness: 1.0.into(),
 984                                ..Default::default()
 985                            }),
 986                            ..Default::default()
 987                        }],
 988                    },
 989                    // Multi-paragraph list items
 990                    Row {
 991                        blocks: vec![HoverBlock {
 992                            text: "
 993                            * one two
 994                              three
 995
 996                            * four five
 997                                * six seven
 998                                  eight
 999
1000                                  nine
1001                                * ten
1002                            * six"
1003                                .unindent(),
1004                            kind: HoverBlockKind::Markdown,
1005                        }],
1006                        expected_marked_text: "
1007                        - one two three
1008                        - four five
1009                          - six seven eight
1010
1011                            nine
1012                          - ten
1013                        - six"
1014                            .unindent(),
1015                        expected_styles: vec![HighlightStyle {
1016                            underline: Some(UnderlineStyle {
1017                                thickness: 1.0.into(),
1018                                ..Default::default()
1019                            }),
1020                            ..Default::default()
1021                        }],
1022                    },
1023                ];
1024
1025                for Row {
1026                    blocks,
1027                    expected_marked_text,
1028                    expected_styles,
1029                } in &rows[0..]
1030                {
1031                    let rendered = smol::block_on(parse_blocks(&blocks, &Default::default(), None));
1032
1033                    let (expected_text, ranges) = marked_text_ranges(expected_marked_text, false);
1034                    let expected_highlights = ranges
1035                        .into_iter()
1036                        .zip(expected_styles.iter().cloned())
1037                        .collect::<Vec<_>>();
1038                    assert_eq!(
1039                        rendered.text, expected_text,
1040                        "wrong text for input {blocks:?}"
1041                    );
1042
1043                    let rendered_highlights: Vec<_> = rendered
1044                        .highlights
1045                        .iter()
1046                        .filter_map(|(range, highlight)| {
1047                            let highlight = highlight.to_highlight_style(&style.syntax)?;
1048                            Some((range.clone(), highlight))
1049                        })
1050                        .collect();
1051
1052                    assert_eq!(
1053                        rendered_highlights, expected_highlights,
1054                        "wrong highlights for input {blocks:?}"
1055                    );
1056                }
1057            })
1058            .unwrap();
1059    }
1060
1061    #[gpui::test]
1062    async fn test_hover_inlay_label_parts(cx: &mut gpui::TestAppContext) {
1063        init_test(cx, |settings| {
1064            settings.defaults.inlay_hints = Some(InlayHintSettings {
1065                enabled: true,
1066                edit_debounce_ms: 0,
1067                scroll_debounce_ms: 0,
1068                show_type_hints: true,
1069                show_parameter_hints: true,
1070                show_other_hints: true,
1071            })
1072        });
1073
1074        let mut cx = EditorLspTestContext::new_rust(
1075            lsp::ServerCapabilities {
1076                inlay_hint_provider: Some(lsp::OneOf::Right(
1077                    lsp::InlayHintServerCapabilities::Options(lsp::InlayHintOptions {
1078                        resolve_provider: Some(true),
1079                        ..Default::default()
1080                    }),
1081                )),
1082                ..Default::default()
1083            },
1084            cx,
1085        )
1086        .await;
1087
1088        cx.set_state(indoc! {"
1089            struct TestStruct;
1090
1091            // ==================
1092
1093            struct TestNewType<T>(T);
1094
1095            fn main() {
1096                let variableˇ = TestNewType(TestStruct);
1097            }
1098        "});
1099
1100        let hint_start_offset = cx.ranges(indoc! {"
1101            struct TestStruct;
1102
1103            // ==================
1104
1105            struct TestNewType<T>(T);
1106
1107            fn main() {
1108                let variableˇ = TestNewType(TestStruct);
1109            }
1110        "})[0]
1111            .start;
1112        let hint_position = cx.to_lsp(hint_start_offset);
1113        let new_type_target_range = cx.lsp_range(indoc! {"
1114            struct TestStruct;
1115
1116            // ==================
1117
1118            struct «TestNewType»<T>(T);
1119
1120            fn main() {
1121                let variable = TestNewType(TestStruct);
1122            }
1123        "});
1124        let struct_target_range = cx.lsp_range(indoc! {"
1125            struct «TestStruct»;
1126
1127            // ==================
1128
1129            struct TestNewType<T>(T);
1130
1131            fn main() {
1132                let variable = TestNewType(TestStruct);
1133            }
1134        "});
1135
1136        let uri = cx.buffer_lsp_url.clone();
1137        let new_type_label = "TestNewType";
1138        let struct_label = "TestStruct";
1139        let entire_hint_label = ": TestNewType<TestStruct>";
1140        let closure_uri = uri.clone();
1141        cx.lsp
1142            .handle_request::<lsp::request::InlayHintRequest, _, _>(move |params, _| {
1143                let task_uri = closure_uri.clone();
1144                async move {
1145                    assert_eq!(params.text_document.uri, task_uri);
1146                    Ok(Some(vec![lsp::InlayHint {
1147                        position: hint_position,
1148                        label: lsp::InlayHintLabel::LabelParts(vec![lsp::InlayHintLabelPart {
1149                            value: entire_hint_label.to_string(),
1150                            ..Default::default()
1151                        }]),
1152                        kind: Some(lsp::InlayHintKind::TYPE),
1153                        text_edits: None,
1154                        tooltip: None,
1155                        padding_left: Some(false),
1156                        padding_right: Some(false),
1157                        data: None,
1158                    }]))
1159                }
1160            })
1161            .next()
1162            .await;
1163        cx.background_executor.run_until_parked();
1164        cx.update_editor(|editor, cx| {
1165            let expected_layers = vec![entire_hint_label.to_string()];
1166            assert_eq!(expected_layers, cached_hint_labels(editor));
1167            assert_eq!(expected_layers, visible_hint_labels(editor, cx));
1168        });
1169
1170        let inlay_range = cx
1171            .ranges(indoc! {"
1172                struct TestStruct;
1173
1174                // ==================
1175
1176                struct TestNewType<T>(T);
1177
1178                fn main() {
1179                    let variable« »= TestNewType(TestStruct);
1180                }
1181        "})
1182            .get(0)
1183            .cloned()
1184            .unwrap();
1185        let new_type_hint_part_hover_position = cx.update_editor(|editor, cx| {
1186            let snapshot = editor.snapshot(cx);
1187            let previous_valid = inlay_range.start.to_display_point(&snapshot);
1188            let next_valid = inlay_range.end.to_display_point(&snapshot);
1189            assert_eq!(previous_valid.row(), next_valid.row());
1190            assert!(previous_valid.column() < next_valid.column());
1191            let exact_unclipped = DisplayPoint::new(
1192                previous_valid.row(),
1193                previous_valid.column()
1194                    + (entire_hint_label.find(new_type_label).unwrap() + new_type_label.len() / 2)
1195                        as u32,
1196            );
1197            PointForPosition {
1198                previous_valid,
1199                next_valid,
1200                exact_unclipped,
1201                column_overshoot_after_line_end: 0,
1202            }
1203        });
1204        cx.update_editor(|editor, cx| {
1205            update_inlay_link_and_hover_points(
1206                &editor.snapshot(cx),
1207                new_type_hint_part_hover_position,
1208                editor,
1209                true,
1210                false,
1211                cx,
1212            );
1213        });
1214
1215        let resolve_closure_uri = uri.clone();
1216        cx.lsp
1217            .handle_request::<lsp::request::InlayHintResolveRequest, _, _>(
1218                move |mut hint_to_resolve, _| {
1219                    let mut resolved_hint_positions = BTreeSet::new();
1220                    let task_uri = resolve_closure_uri.clone();
1221                    async move {
1222                        let inserted = resolved_hint_positions.insert(hint_to_resolve.position);
1223                        assert!(inserted, "Hint {hint_to_resolve:?} was resolved twice");
1224
1225                        // `: TestNewType<TestStruct>`
1226                        hint_to_resolve.label = lsp::InlayHintLabel::LabelParts(vec![
1227                            lsp::InlayHintLabelPart {
1228                                value: ": ".to_string(),
1229                                ..Default::default()
1230                            },
1231                            lsp::InlayHintLabelPart {
1232                                value: new_type_label.to_string(),
1233                                location: Some(lsp::Location {
1234                                    uri: task_uri.clone(),
1235                                    range: new_type_target_range,
1236                                }),
1237                                tooltip: Some(lsp::InlayHintLabelPartTooltip::String(format!(
1238                                    "A tooltip for `{new_type_label}`"
1239                                ))),
1240                                ..Default::default()
1241                            },
1242                            lsp::InlayHintLabelPart {
1243                                value: "<".to_string(),
1244                                ..Default::default()
1245                            },
1246                            lsp::InlayHintLabelPart {
1247                                value: struct_label.to_string(),
1248                                location: Some(lsp::Location {
1249                                    uri: task_uri,
1250                                    range: struct_target_range,
1251                                }),
1252                                tooltip: Some(lsp::InlayHintLabelPartTooltip::MarkupContent(
1253                                    lsp::MarkupContent {
1254                                        kind: lsp::MarkupKind::Markdown,
1255                                        value: format!("A tooltip for `{struct_label}`"),
1256                                    },
1257                                )),
1258                                ..Default::default()
1259                            },
1260                            lsp::InlayHintLabelPart {
1261                                value: ">".to_string(),
1262                                ..Default::default()
1263                            },
1264                        ]);
1265
1266                        Ok(hint_to_resolve)
1267                    }
1268                },
1269            )
1270            .next()
1271            .await;
1272        cx.background_executor.run_until_parked();
1273
1274        cx.update_editor(|editor, cx| {
1275            update_inlay_link_and_hover_points(
1276                &editor.snapshot(cx),
1277                new_type_hint_part_hover_position,
1278                editor,
1279                true,
1280                false,
1281                cx,
1282            );
1283        });
1284        cx.background_executor
1285            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
1286        cx.background_executor.run_until_parked();
1287        cx.update_editor(|editor, cx| {
1288            let hover_state = &editor.hover_state;
1289            assert!(hover_state.diagnostic_popover.is_none() && hover_state.info_popover.is_some());
1290            let popover = hover_state.info_popover.as_ref().unwrap();
1291            let buffer_snapshot = editor.buffer().update(cx, |buffer, cx| buffer.snapshot(cx));
1292            assert_eq!(
1293                popover.symbol_range,
1294                RangeInEditor::Inlay(InlayHighlight {
1295                    inlay: InlayId::Hint(0),
1296                    inlay_position: buffer_snapshot.anchor_at(inlay_range.start, Bias::Right),
1297                    range: ": ".len()..": ".len() + new_type_label.len(),
1298                }),
1299                "Popover range should match the new type label part"
1300            );
1301            assert_eq!(
1302                popover.parsed_content.text,
1303                format!("A tooltip for `{new_type_label}`"),
1304                "Rendered text should not anyhow alter backticks"
1305            );
1306        });
1307
1308        let struct_hint_part_hover_position = cx.update_editor(|editor, cx| {
1309            let snapshot = editor.snapshot(cx);
1310            let previous_valid = inlay_range.start.to_display_point(&snapshot);
1311            let next_valid = inlay_range.end.to_display_point(&snapshot);
1312            assert_eq!(previous_valid.row(), next_valid.row());
1313            assert!(previous_valid.column() < next_valid.column());
1314            let exact_unclipped = DisplayPoint::new(
1315                previous_valid.row(),
1316                previous_valid.column()
1317                    + (entire_hint_label.find(struct_label).unwrap() + struct_label.len() / 2)
1318                        as u32,
1319            );
1320            PointForPosition {
1321                previous_valid,
1322                next_valid,
1323                exact_unclipped,
1324                column_overshoot_after_line_end: 0,
1325            }
1326        });
1327        cx.update_editor(|editor, cx| {
1328            update_inlay_link_and_hover_points(
1329                &editor.snapshot(cx),
1330                struct_hint_part_hover_position,
1331                editor,
1332                true,
1333                false,
1334                cx,
1335            );
1336        });
1337        cx.background_executor
1338            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
1339        cx.background_executor.run_until_parked();
1340        cx.update_editor(|editor, cx| {
1341            let hover_state = &editor.hover_state;
1342            assert!(hover_state.diagnostic_popover.is_none() && hover_state.info_popover.is_some());
1343            let popover = hover_state.info_popover.as_ref().unwrap();
1344            let buffer_snapshot = editor.buffer().update(cx, |buffer, cx| buffer.snapshot(cx));
1345            assert_eq!(
1346                popover.symbol_range,
1347                RangeInEditor::Inlay(InlayHighlight {
1348                    inlay: InlayId::Hint(0),
1349                    inlay_position: buffer_snapshot.anchor_at(inlay_range.start, Bias::Right),
1350                    range: ": ".len() + new_type_label.len() + "<".len()
1351                        ..": ".len() + new_type_label.len() + "<".len() + struct_label.len(),
1352                }),
1353                "Popover range should match the struct label part"
1354            );
1355            assert_eq!(
1356                popover.parsed_content.text,
1357                format!("A tooltip for {struct_label}"),
1358                "Rendered markdown element should remove backticks from text"
1359            );
1360        });
1361    }
1362}