hover_popover.rs

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