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