hover_popover.rs

   1use crate::{
   2    display_map::{InlayOffset, ToDisplayPoint},
   3    link_go_to_definition::{InlayRange, 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: InlayRange,
  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),
 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    };
 800    use collections::BTreeSet;
 801    use gpui::fonts::Weight;
 802    use indoc::indoc;
 803    use language::{language_settings::InlayHintSettings, Diagnostic, DiagnosticSet};
 804    use lsp::LanguageServerId;
 805    use project::{HoverBlock, HoverBlockKind};
 806    use smol::stream::StreamExt;
 807    use unindent::Unindent;
 808    use util::test::marked_text_ranges;
 809
 810    #[gpui::test]
 811    async fn test_mouse_hover_info_popover(cx: &mut gpui::TestAppContext) {
 812        init_test(cx, |_| {});
 813
 814        let mut cx = EditorLspTestContext::new_rust(
 815            lsp::ServerCapabilities {
 816                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 817                ..Default::default()
 818            },
 819            cx,
 820        )
 821        .await;
 822
 823        // Basic hover delays and then pops without moving the mouse
 824        cx.set_state(indoc! {"
 825            fn ˇtest() { println!(); }
 826        "});
 827        let hover_point = cx.display_point(indoc! {"
 828            fn test() { printˇln!(); }
 829        "});
 830
 831        cx.update_editor(|editor, cx| hover_at(editor, Some(hover_point), cx));
 832        assert!(!cx.editor(|editor, _| editor.hover_state.visible()));
 833
 834        // After delay, hover should be visible.
 835        let symbol_range = cx.lsp_range(indoc! {"
 836            fn test() { «println!»(); }
 837        "});
 838        let mut requests =
 839            cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 840                Ok(Some(lsp::Hover {
 841                    contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 842                        kind: lsp::MarkupKind::Markdown,
 843                        value: "some basic docs".to_string(),
 844                    }),
 845                    range: Some(symbol_range),
 846                }))
 847            });
 848        cx.foreground()
 849            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
 850        requests.next().await;
 851
 852        cx.editor(|editor, _| {
 853            assert!(editor.hover_state.visible());
 854            assert_eq!(
 855                editor.hover_state.info_popover.clone().unwrap().blocks,
 856                vec![HoverBlock {
 857                    text: "some basic docs".to_string(),
 858                    kind: HoverBlockKind::Markdown,
 859                },]
 860            )
 861        });
 862
 863        // Mouse moved with no hover response dismisses
 864        let hover_point = cx.display_point(indoc! {"
 865            fn teˇst() { println!(); }
 866        "});
 867        let mut request = cx
 868            .lsp
 869            .handle_request::<lsp::request::HoverRequest, _, _>(|_, _| async move { Ok(None) });
 870        cx.update_editor(|editor, cx| hover_at(editor, Some(hover_point), cx));
 871        cx.foreground()
 872            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
 873        request.next().await;
 874        cx.editor(|editor, _| {
 875            assert!(!editor.hover_state.visible());
 876        });
 877    }
 878
 879    #[gpui::test]
 880    async fn test_keyboard_hover_info_popover(cx: &mut gpui::TestAppContext) {
 881        init_test(cx, |_| {});
 882
 883        let mut cx = EditorLspTestContext::new_rust(
 884            lsp::ServerCapabilities {
 885                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 886                ..Default::default()
 887            },
 888            cx,
 889        )
 890        .await;
 891
 892        // Hover with keyboard has no delay
 893        cx.set_state(indoc! {"
 894            fˇn test() { println!(); }
 895        "});
 896        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 897        let symbol_range = cx.lsp_range(indoc! {"
 898            «fn» test() { println!(); }
 899        "});
 900        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 901            Ok(Some(lsp::Hover {
 902                contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 903                    kind: lsp::MarkupKind::Markdown,
 904                    value: "some other basic docs".to_string(),
 905                }),
 906                range: Some(symbol_range),
 907            }))
 908        })
 909        .next()
 910        .await;
 911
 912        cx.condition(|editor, _| editor.hover_state.visible()).await;
 913        cx.editor(|editor, _| {
 914            assert_eq!(
 915                editor.hover_state.info_popover.clone().unwrap().blocks,
 916                vec![HoverBlock {
 917                    text: "some other basic docs".to_string(),
 918                    kind: HoverBlockKind::Markdown,
 919                }]
 920            )
 921        });
 922    }
 923
 924    #[gpui::test]
 925    async fn test_empty_hovers_filtered(cx: &mut gpui::TestAppContext) {
 926        init_test(cx, |_| {});
 927
 928        let mut cx = EditorLspTestContext::new_rust(
 929            lsp::ServerCapabilities {
 930                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 931                ..Default::default()
 932            },
 933            cx,
 934        )
 935        .await;
 936
 937        // Hover with keyboard has no delay
 938        cx.set_state(indoc! {"
 939            fˇn test() { println!(); }
 940        "});
 941        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 942        let symbol_range = cx.lsp_range(indoc! {"
 943            «fn» test() { println!(); }
 944        "});
 945        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 946            Ok(Some(lsp::Hover {
 947                contents: lsp::HoverContents::Array(vec![
 948                    lsp::MarkedString::String("regular text for hover to show".to_string()),
 949                    lsp::MarkedString::String("".to_string()),
 950                    lsp::MarkedString::LanguageString(lsp::LanguageString {
 951                        language: "Rust".to_string(),
 952                        value: "".to_string(),
 953                    }),
 954                ]),
 955                range: Some(symbol_range),
 956            }))
 957        })
 958        .next()
 959        .await;
 960
 961        cx.condition(|editor, _| editor.hover_state.visible()).await;
 962        cx.editor(|editor, _| {
 963            assert_eq!(
 964                editor.hover_state.info_popover.clone().unwrap().blocks,
 965                vec![HoverBlock {
 966                    text: "regular text for hover to show".to_string(),
 967                    kind: HoverBlockKind::Markdown,
 968                }],
 969                "No empty string hovers should be shown"
 970            );
 971        });
 972    }
 973
 974    #[gpui::test]
 975    async fn test_line_ends_trimmed(cx: &mut gpui::TestAppContext) {
 976        init_test(cx, |_| {});
 977
 978        let mut cx = EditorLspTestContext::new_rust(
 979            lsp::ServerCapabilities {
 980                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 981                ..Default::default()
 982            },
 983            cx,
 984        )
 985        .await;
 986
 987        // Hover with keyboard has no delay
 988        cx.set_state(indoc! {"
 989            fˇn test() { println!(); }
 990        "});
 991        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 992        let symbol_range = cx.lsp_range(indoc! {"
 993            «fn» test() { println!(); }
 994        "});
 995
 996        let code_str = "\nlet hovered_point: Vector2F // size = 8, align = 0x4\n";
 997        let markdown_string = format!("\n```rust\n{code_str}```");
 998
 999        let closure_markdown_string = markdown_string.clone();
1000        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| {
1001            let future_markdown_string = closure_markdown_string.clone();
1002            async move {
1003                Ok(Some(lsp::Hover {
1004                    contents: lsp::HoverContents::Markup(lsp::MarkupContent {
1005                        kind: lsp::MarkupKind::Markdown,
1006                        value: future_markdown_string,
1007                    }),
1008                    range: Some(symbol_range),
1009                }))
1010            }
1011        })
1012        .next()
1013        .await;
1014
1015        cx.condition(|editor, _| editor.hover_state.visible()).await;
1016        cx.editor(|editor, cx| {
1017            let blocks = editor.hover_state.info_popover.clone().unwrap().blocks;
1018            assert_eq!(
1019                blocks,
1020                vec![HoverBlock {
1021                    text: markdown_string,
1022                    kind: HoverBlockKind::Markdown,
1023                }],
1024            );
1025
1026            let style = editor.style(cx);
1027            let rendered = render_blocks(0, &blocks, &Default::default(), None, &style);
1028            assert_eq!(
1029                rendered.text,
1030                code_str.trim(),
1031                "Should not have extra line breaks at end of rendered hover"
1032            );
1033        });
1034    }
1035
1036    #[gpui::test]
1037    async fn test_hover_diagnostic_and_info_popovers(cx: &mut gpui::TestAppContext) {
1038        init_test(cx, |_| {});
1039
1040        let mut cx = EditorLspTestContext::new_rust(
1041            lsp::ServerCapabilities {
1042                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
1043                ..Default::default()
1044            },
1045            cx,
1046        )
1047        .await;
1048
1049        // Hover with just diagnostic, pops DiagnosticPopover immediately and then
1050        // info popover once request completes
1051        cx.set_state(indoc! {"
1052            fn teˇst() { println!(); }
1053        "});
1054
1055        // Send diagnostic to client
1056        let range = cx.text_anchor_range(indoc! {"
1057            fn «test»() { println!(); }
1058        "});
1059        cx.update_buffer(|buffer, cx| {
1060            let snapshot = buffer.text_snapshot();
1061            let set = DiagnosticSet::from_sorted_entries(
1062                vec![DiagnosticEntry {
1063                    range,
1064                    diagnostic: Diagnostic {
1065                        message: "A test diagnostic message.".to_string(),
1066                        ..Default::default()
1067                    },
1068                }],
1069                &snapshot,
1070            );
1071            buffer.update_diagnostics(LanguageServerId(0), set, cx);
1072        });
1073
1074        // Hover pops diagnostic immediately
1075        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
1076        cx.foreground().run_until_parked();
1077
1078        cx.editor(|Editor { hover_state, .. }, _| {
1079            assert!(hover_state.diagnostic_popover.is_some() && hover_state.info_popover.is_none())
1080        });
1081
1082        // Info Popover shows after request responded to
1083        let range = cx.lsp_range(indoc! {"
1084            fn «test»() { println!(); }
1085        "});
1086        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
1087            Ok(Some(lsp::Hover {
1088                contents: lsp::HoverContents::Markup(lsp::MarkupContent {
1089                    kind: lsp::MarkupKind::Markdown,
1090                    value: "some new docs".to_string(),
1091                }),
1092                range: Some(range),
1093            }))
1094        });
1095        cx.foreground()
1096            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
1097
1098        cx.foreground().run_until_parked();
1099        cx.editor(|Editor { hover_state, .. }, _| {
1100            hover_state.diagnostic_popover.is_some() && hover_state.info_task.is_some()
1101        });
1102    }
1103
1104    #[gpui::test]
1105    fn test_render_blocks(cx: &mut gpui::TestAppContext) {
1106        init_test(cx, |_| {});
1107
1108        cx.add_window(|cx| {
1109            let editor = Editor::single_line(None, cx);
1110            let style = editor.style(cx);
1111
1112            struct Row {
1113                blocks: Vec<HoverBlock>,
1114                expected_marked_text: String,
1115                expected_styles: Vec<HighlightStyle>,
1116            }
1117
1118            let rows = &[
1119                // Strong emphasis
1120                Row {
1121                    blocks: vec![HoverBlock {
1122                        text: "one **two** three".to_string(),
1123                        kind: HoverBlockKind::Markdown,
1124                    }],
1125                    expected_marked_text: "one «two» three".to_string(),
1126                    expected_styles: vec![HighlightStyle {
1127                        weight: Some(Weight::BOLD),
1128                        ..Default::default()
1129                    }],
1130                },
1131                // Links
1132                Row {
1133                    blocks: vec![HoverBlock {
1134                        text: "one [two](the-url) three".to_string(),
1135                        kind: HoverBlockKind::Markdown,
1136                    }],
1137                    expected_marked_text: "one «two» three".to_string(),
1138                    expected_styles: vec![HighlightStyle {
1139                        underline: Some(Underline {
1140                            thickness: 1.0.into(),
1141                            ..Default::default()
1142                        }),
1143                        ..Default::default()
1144                    }],
1145                },
1146                // Lists
1147                Row {
1148                    blocks: vec![HoverBlock {
1149                        text: "
1150                            lists:
1151                            * one
1152                                - a
1153                                - b
1154                            * two
1155                                - [c](the-url)
1156                                - d"
1157                        .unindent(),
1158                        kind: HoverBlockKind::Markdown,
1159                    }],
1160                    expected_marked_text: "
1161                        lists:
1162                        - one
1163                          - a
1164                          - b
1165                        - two
1166                          - «c»
1167                          - d"
1168                    .unindent(),
1169                    expected_styles: vec![HighlightStyle {
1170                        underline: Some(Underline {
1171                            thickness: 1.0.into(),
1172                            ..Default::default()
1173                        }),
1174                        ..Default::default()
1175                    }],
1176                },
1177                // Multi-paragraph list items
1178                Row {
1179                    blocks: vec![HoverBlock {
1180                        text: "
1181                            * one two
1182                              three
1183
1184                            * four five
1185                                * six seven
1186                                  eight
1187
1188                                  nine
1189                                * ten
1190                            * six"
1191                            .unindent(),
1192                        kind: HoverBlockKind::Markdown,
1193                    }],
1194                    expected_marked_text: "
1195                        - one two three
1196                        - four five
1197                          - six seven eight
1198
1199                            nine
1200                          - ten
1201                        - six"
1202                        .unindent(),
1203                    expected_styles: vec![HighlightStyle {
1204                        underline: Some(Underline {
1205                            thickness: 1.0.into(),
1206                            ..Default::default()
1207                        }),
1208                        ..Default::default()
1209                    }],
1210                },
1211            ];
1212
1213            for Row {
1214                blocks,
1215                expected_marked_text,
1216                expected_styles,
1217            } in &rows[0..]
1218            {
1219                let rendered = render_blocks(0, &blocks, &Default::default(), None, &style);
1220
1221                let (expected_text, ranges) = marked_text_ranges(expected_marked_text, false);
1222                let expected_highlights = ranges
1223                    .into_iter()
1224                    .zip(expected_styles.iter().cloned())
1225                    .collect::<Vec<_>>();
1226                assert_eq!(
1227                    rendered.text, expected_text,
1228                    "wrong text for input {blocks:?}"
1229                );
1230                assert_eq!(
1231                    rendered.highlights, expected_highlights,
1232                    "wrong highlights for input {blocks:?}"
1233                );
1234            }
1235
1236            editor
1237        });
1238    }
1239
1240    #[gpui::test]
1241    async fn test_hover_inlay_label_parts(cx: &mut gpui::TestAppContext) {
1242        init_test(cx, |settings| {
1243            settings.defaults.inlay_hints = Some(InlayHintSettings {
1244                enabled: true,
1245                show_type_hints: true,
1246                show_parameter_hints: true,
1247                show_other_hints: true,
1248            })
1249        });
1250
1251        let mut cx = EditorLspTestContext::new_rust(
1252            lsp::ServerCapabilities {
1253                inlay_hint_provider: Some(lsp::OneOf::Right(
1254                    lsp::InlayHintServerCapabilities::Options(lsp::InlayHintOptions {
1255                        resolve_provider: Some(true),
1256                        ..Default::default()
1257                    }),
1258                )),
1259                ..Default::default()
1260            },
1261            cx,
1262        )
1263        .await;
1264
1265        cx.set_state(indoc! {"
1266            struct TestStruct;
1267
1268            // ==================
1269
1270            struct TestNewType<T>(T);
1271
1272            fn main() {
1273                let variableˇ = TestNewType(TestStruct);
1274            }
1275        "});
1276
1277        let hint_start_offset = cx.ranges(indoc! {"
1278            struct TestStruct;
1279
1280            // ==================
1281
1282            struct TestNewType<T>(T);
1283
1284            fn main() {
1285                let variableˇ = TestNewType(TestStruct);
1286            }
1287        "})[0]
1288            .start;
1289        let hint_position = cx.to_lsp(hint_start_offset);
1290        let new_type_target_range = cx.lsp_range(indoc! {"
1291            struct TestStruct;
1292
1293            // ==================
1294
1295            struct «TestNewType»<T>(T);
1296
1297            fn main() {
1298                let variable = TestNewType(TestStruct);
1299            }
1300        "});
1301        let struct_target_range = cx.lsp_range(indoc! {"
1302            struct «TestStruct»;
1303
1304            // ==================
1305
1306            struct TestNewType<T>(T);
1307
1308            fn main() {
1309                let variable = TestNewType(TestStruct);
1310            }
1311        "});
1312
1313        let uri = cx.buffer_lsp_url.clone();
1314        let new_type_label = "TestNewType";
1315        let struct_label = "TestStruct";
1316        let entire_hint_label = ": TestNewType<TestStruct>";
1317        let closure_uri = uri.clone();
1318        cx.lsp
1319            .handle_request::<lsp::request::InlayHintRequest, _, _>(move |params, _| {
1320                let task_uri = closure_uri.clone();
1321                async move {
1322                    assert_eq!(params.text_document.uri, task_uri);
1323                    Ok(Some(vec![lsp::InlayHint {
1324                        position: hint_position,
1325                        label: lsp::InlayHintLabel::LabelParts(vec![lsp::InlayHintLabelPart {
1326                            value: entire_hint_label.to_string(),
1327                            ..Default::default()
1328                        }]),
1329                        kind: Some(lsp::InlayHintKind::TYPE),
1330                        text_edits: None,
1331                        tooltip: None,
1332                        padding_left: Some(false),
1333                        padding_right: Some(false),
1334                        data: None,
1335                    }]))
1336                }
1337            })
1338            .next()
1339            .await;
1340        cx.foreground().run_until_parked();
1341        cx.update_editor(|editor, cx| {
1342            let expected_layers = vec![entire_hint_label.to_string()];
1343            assert_eq!(expected_layers, cached_hint_labels(editor));
1344            assert_eq!(expected_layers, visible_hint_labels(editor, cx));
1345        });
1346
1347        let inlay_range = cx
1348            .ranges(indoc! {"
1349                struct TestStruct;
1350
1351                // ==================
1352
1353                struct TestNewType<T>(T);
1354
1355                fn main() {
1356                    let variable« »= TestNewType(TestStruct);
1357                }
1358        "})
1359            .get(0)
1360            .cloned()
1361            .unwrap();
1362        let new_type_hint_part_hover_position = cx.update_editor(|editor, cx| {
1363            let snapshot = editor.snapshot(cx);
1364            let previous_valid = inlay_range.start.to_display_point(&snapshot);
1365            let next_valid = inlay_range.end.to_display_point(&snapshot);
1366            assert_eq!(previous_valid.row(), next_valid.row());
1367            assert!(previous_valid.column() < next_valid.column());
1368            let exact_unclipped = DisplayPoint::new(
1369                previous_valid.row(),
1370                previous_valid.column()
1371                    + (entire_hint_label.find(new_type_label).unwrap() + new_type_label.len() / 2)
1372                        as u32,
1373            );
1374            PointForPosition {
1375                previous_valid,
1376                next_valid,
1377                exact_unclipped,
1378                column_overshoot_after_line_end: 0,
1379            }
1380        });
1381        cx.update_editor(|editor, cx| {
1382            update_inlay_link_and_hover_points(
1383                &editor.snapshot(cx),
1384                new_type_hint_part_hover_position,
1385                editor,
1386                true,
1387                false,
1388                cx,
1389            );
1390        });
1391
1392        let resolve_closure_uri = uri.clone();
1393        cx.lsp
1394            .handle_request::<lsp::request::InlayHintResolveRequest, _, _>(
1395                move |mut hint_to_resolve, _| {
1396                    let mut resolved_hint_positions = BTreeSet::new();
1397                    let task_uri = resolve_closure_uri.clone();
1398                    async move {
1399                        let inserted = resolved_hint_positions.insert(hint_to_resolve.position);
1400                        assert!(inserted, "Hint {hint_to_resolve:?} was resolved twice");
1401
1402                        // `: TestNewType<TestStruct>`
1403                        hint_to_resolve.label = lsp::InlayHintLabel::LabelParts(vec![
1404                            lsp::InlayHintLabelPart {
1405                                value: ": ".to_string(),
1406                                ..Default::default()
1407                            },
1408                            lsp::InlayHintLabelPart {
1409                                value: new_type_label.to_string(),
1410                                location: Some(lsp::Location {
1411                                    uri: task_uri.clone(),
1412                                    range: new_type_target_range,
1413                                }),
1414                                tooltip: Some(lsp::InlayHintLabelPartTooltip::String(format!(
1415                                    "A tooltip for `{new_type_label}`"
1416                                ))),
1417                                ..Default::default()
1418                            },
1419                            lsp::InlayHintLabelPart {
1420                                value: "<".to_string(),
1421                                ..Default::default()
1422                            },
1423                            lsp::InlayHintLabelPart {
1424                                value: struct_label.to_string(),
1425                                location: Some(lsp::Location {
1426                                    uri: task_uri,
1427                                    range: struct_target_range,
1428                                }),
1429                                tooltip: Some(lsp::InlayHintLabelPartTooltip::MarkupContent(
1430                                    lsp::MarkupContent {
1431                                        kind: lsp::MarkupKind::Markdown,
1432                                        value: format!("A tooltip for `{struct_label}`"),
1433                                    },
1434                                )),
1435                                ..Default::default()
1436                            },
1437                            lsp::InlayHintLabelPart {
1438                                value: ">".to_string(),
1439                                ..Default::default()
1440                            },
1441                        ]);
1442
1443                        Ok(hint_to_resolve)
1444                    }
1445                },
1446            )
1447            .next()
1448            .await;
1449        cx.foreground().run_until_parked();
1450
1451        cx.update_editor(|editor, cx| {
1452            update_inlay_link_and_hover_points(
1453                &editor.snapshot(cx),
1454                new_type_hint_part_hover_position,
1455                editor,
1456                true,
1457                false,
1458                cx,
1459            );
1460        });
1461        cx.foreground()
1462            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
1463        cx.foreground().run_until_parked();
1464        cx.update_editor(|editor, cx| {
1465            let snapshot = editor.snapshot(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            let entire_inlay_start = snapshot.display_point_to_inlay_offset(
1471                inlay_range.start.to_display_point(&snapshot),
1472                Bias::Left,
1473            );
1474
1475            let expected_new_type_label_start = InlayOffset(entire_inlay_start.0 + ": ".len());
1476            // TODO kb
1477            // assert_eq!(
1478            //     popover.symbol_range,
1479            //     RangeInEditor::Inlay(InlayRange {
1480            //         inlay_position: buffer_snapshot.anchor_at(inlay_range.start, Bias::Right),
1481            //         highlight_start: expected_new_type_label_start,
1482            //         highlight_end: InlayOffset(
1483            //             expected_new_type_label_start.0 + new_type_label.len()
1484            //         ),
1485            //     }),
1486            //     "Popover range should match the new type label part"
1487            // );
1488            assert_eq!(
1489                popover
1490                    .rendered_content
1491                    .as_ref()
1492                    .expect("should have label text for new type hint")
1493                    .text,
1494                format!("A tooltip for `{new_type_label}`"),
1495                "Rendered text should not anyhow alter backticks"
1496            );
1497        });
1498
1499        let struct_hint_part_hover_position = cx.update_editor(|editor, cx| {
1500            let snapshot = editor.snapshot(cx);
1501            let previous_valid = inlay_range.start.to_display_point(&snapshot);
1502            let next_valid = inlay_range.end.to_display_point(&snapshot);
1503            assert_eq!(previous_valid.row(), next_valid.row());
1504            assert!(previous_valid.column() < next_valid.column());
1505            let exact_unclipped = DisplayPoint::new(
1506                previous_valid.row(),
1507                previous_valid.column()
1508                    + (entire_hint_label.find(struct_label).unwrap() + struct_label.len() / 2)
1509                        as u32,
1510            );
1511            PointForPosition {
1512                previous_valid,
1513                next_valid,
1514                exact_unclipped,
1515                column_overshoot_after_line_end: 0,
1516            }
1517        });
1518        cx.update_editor(|editor, cx| {
1519            update_inlay_link_and_hover_points(
1520                &editor.snapshot(cx),
1521                struct_hint_part_hover_position,
1522                editor,
1523                true,
1524                false,
1525                cx,
1526            );
1527        });
1528        cx.foreground()
1529            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
1530        cx.foreground().run_until_parked();
1531        cx.update_editor(|editor, cx| {
1532            let snapshot = editor.snapshot(cx);
1533            let hover_state = &editor.hover_state;
1534            assert!(hover_state.diagnostic_popover.is_none() && hover_state.info_popover.is_some());
1535            let popover = hover_state.info_popover.as_ref().unwrap();
1536            let buffer_snapshot = editor.buffer().update(cx, |buffer, cx| buffer.snapshot(cx));
1537            let entire_inlay_start = snapshot.display_point_to_inlay_offset(
1538                inlay_range.start.to_display_point(&snapshot),
1539                Bias::Left,
1540            );
1541            let expected_struct_label_start =
1542                InlayOffset(entire_inlay_start.0 + ": ".len() + new_type_label.len() + "<".len());
1543            // TODO kb
1544            // assert_eq!(
1545            //     popover.symbol_range,
1546            //     RangeInEditor::Inlay(InlayRange {
1547            //         inlay_position: buffer_snapshot.anchor_at(inlay_range.start, Bias::Right),
1548            //         highlight_start: expected_struct_label_start,
1549            //         highlight_end: InlayOffset(expected_struct_label_start.0 + struct_label.len()),
1550            //     }),
1551            //     "Popover range should match the struct label part"
1552            // );
1553            assert_eq!(
1554                popover
1555                    .rendered_content
1556                    .as_ref()
1557                    .expect("should have label text for struct hint")
1558                    .text,
1559                format!("A tooltip for {struct_label}"),
1560                "Rendered markdown element should remove backticks from text"
1561            );
1562        });
1563    }
1564}