hover_popover.rs

   1use crate::{
   2    display_map::ToDisplayPoint, Anchor, AnchorRangeExt, DisplayPoint, Editor, EditorSettings,
   3    EditorSnapshot, EditorStyle, RangeToAnchorExt,
   4};
   5use futures::FutureExt;
   6use gpui::{
   7    actions,
   8    elements::{Flex, MouseEventHandler, Padding, ParentElement, Text},
   9    fonts::{HighlightStyle, Underline, Weight},
  10    platform::{CursorStyle, MouseButton},
  11    AnyElement, AppContext, CursorRegion, Element, ModelHandle, MouseRegion, Task, ViewContext,
  12};
  13use language::{Bias, DiagnosticEntry, DiagnosticSeverity, Language, LanguageRegistry};
  14use project::{HoverBlock, HoverBlockKind, Project};
  15use std::{ops::Range, sync::Arc, time::Duration};
  16use util::TryFutureExt;
  17
  18pub const HOVER_DELAY_MILLIS: u64 = 350;
  19pub const HOVER_REQUEST_DELAY_MILLIS: u64 = 200;
  20
  21pub const MIN_POPOVER_CHARACTER_WIDTH: f32 = 20.;
  22pub const MIN_POPOVER_LINE_HEIGHT: f32 = 4.;
  23pub const HOVER_POPOVER_GAP: f32 = 10.;
  24
  25actions!(editor, [Hover]);
  26
  27pub fn init(cx: &mut AppContext) {
  28    cx.add_action(hover);
  29}
  30
  31/// Bindable action which uses the most recent selection head to trigger a hover
  32pub fn hover(editor: &mut Editor, _: &Hover, cx: &mut ViewContext<Editor>) {
  33    let head = editor.selections.newest_display(cx).head();
  34    show_hover(editor, head, true, cx);
  35}
  36
  37/// The internal hover action dispatches between `show_hover` or `hide_hover`
  38/// depending on whether a point to hover over is provided.
  39pub fn hover_at(editor: &mut Editor, point: Option<DisplayPoint>, cx: &mut ViewContext<Editor>) {
  40    if settings::get::<EditorSettings>(cx).hover_popover_enabled {
  41        if let Some(point) = point {
  42            show_hover(editor, point, false, cx);
  43        } else {
  44            hide_hover(editor, cx);
  45        }
  46    }
  47}
  48
  49/// Hides the type information popup.
  50/// Triggered by the `Hover` action when the cursor is not over a symbol or when the
  51/// selections changed.
  52pub fn hide_hover(editor: &mut Editor, cx: &mut ViewContext<Editor>) -> bool {
  53    let did_hide = editor.hover_state.info_popover.take().is_some()
  54        | editor.hover_state.diagnostic_popover.take().is_some();
  55
  56    editor.hover_state.info_task = None;
  57    editor.hover_state.triggered_from = None;
  58
  59    editor.clear_background_highlights::<HoverState>(cx);
  60
  61    if did_hide {
  62        cx.notify();
  63    }
  64
  65    did_hide
  66}
  67
  68/// Queries the LSP and shows type info and documentation
  69/// about the symbol the mouse is currently hovering over.
  70/// Triggered by the `Hover` action when the cursor may be over a symbol.
  71fn show_hover(
  72    editor: &mut Editor,
  73    point: DisplayPoint,
  74    ignore_timeout: bool,
  75    cx: &mut ViewContext<Editor>,
  76) {
  77    if editor.pending_rename.is_some() {
  78        return;
  79    }
  80
  81    let snapshot = editor.snapshot(cx);
  82    let multibuffer_offset = point.to_offset(&snapshot.display_snapshot, Bias::Left);
  83
  84    let (buffer, buffer_position) = if let Some(output) = editor
  85        .buffer
  86        .read(cx)
  87        .text_anchor_for_position(multibuffer_offset, cx)
  88    {
  89        output
  90    } else {
  91        return;
  92    };
  93
  94    let excerpt_id = if let Some((excerpt_id, _, _)) = editor
  95        .buffer()
  96        .read(cx)
  97        .excerpt_containing(multibuffer_offset, cx)
  98    {
  99        excerpt_id
 100    } else {
 101        return;
 102    };
 103
 104    let project = if let Some(project) = editor.project.clone() {
 105        project
 106    } else {
 107        return;
 108    };
 109
 110    if !ignore_timeout {
 111        if let Some(InfoPopover { symbol_range, .. }) = &editor.hover_state.info_popover {
 112            if symbol_range
 113                .to_offset(&snapshot.buffer_snapshot)
 114                .contains(&multibuffer_offset)
 115            {
 116                // Hover triggered from same location as last time. Don't show again.
 117                return;
 118            } else {
 119                hide_hover(editor, cx);
 120            }
 121        }
 122    }
 123
 124    // Get input anchor
 125    let anchor = snapshot
 126        .buffer_snapshot
 127        .anchor_at(multibuffer_offset, Bias::Left);
 128
 129    // Don't request again if the location is the same as the previous request
 130    if let Some(triggered_from) = &editor.hover_state.triggered_from {
 131        if triggered_from
 132            .cmp(&anchor, &snapshot.buffer_snapshot)
 133            .is_eq()
 134        {
 135            return;
 136        }
 137    }
 138
 139    let task = cx.spawn(|this, mut cx| {
 140        async move {
 141            // If we need to delay, delay a set amount initially before making the lsp request
 142            let delay = if !ignore_timeout {
 143                // Construct delay task to wait for later
 144                let total_delay = Some(
 145                    cx.background()
 146                        .timer(Duration::from_millis(HOVER_DELAY_MILLIS)),
 147                );
 148
 149                cx.background()
 150                    .timer(Duration::from_millis(HOVER_REQUEST_DELAY_MILLIS))
 151                    .await;
 152                total_delay
 153            } else {
 154                None
 155            };
 156
 157            // query the LSP for hover info
 158            let hover_request = cx.update(|cx| {
 159                project.update(cx, |project, cx| {
 160                    project.hover(&buffer, buffer_position, cx)
 161                })
 162            });
 163
 164            if let Some(delay) = delay {
 165                delay.await;
 166            }
 167
 168            // If there's a diagnostic, assign it on the hover state and notify
 169            let local_diagnostic = snapshot
 170                .buffer_snapshot
 171                .diagnostics_in_range::<_, usize>(multibuffer_offset..multibuffer_offset, false)
 172                // Find the entry with the most specific range
 173                .min_by_key(|entry| entry.range.end - entry.range.start)
 174                .map(|entry| DiagnosticEntry {
 175                    diagnostic: entry.diagnostic,
 176                    range: entry.range.to_anchors(&snapshot.buffer_snapshot),
 177                });
 178
 179            // Pull the primary diagnostic out so we can jump to it if the popover is clicked
 180            let primary_diagnostic = local_diagnostic.as_ref().and_then(|local_diagnostic| {
 181                snapshot
 182                    .buffer_snapshot
 183                    .diagnostic_group::<usize>(local_diagnostic.diagnostic.group_id)
 184                    .find(|diagnostic| diagnostic.diagnostic.is_primary)
 185                    .map(|entry| DiagnosticEntry {
 186                        diagnostic: entry.diagnostic,
 187                        range: entry.range.to_anchors(&snapshot.buffer_snapshot),
 188                    })
 189            });
 190
 191            this.update(&mut cx, |this, _| {
 192                this.hover_state.diagnostic_popover =
 193                    local_diagnostic.map(|local_diagnostic| DiagnosticPopover {
 194                        local_diagnostic,
 195                        primary_diagnostic,
 196                    });
 197            })?;
 198
 199            // Construct new hover popover from hover request
 200            let hover_popover = hover_request.await.ok().flatten().and_then(|hover_result| {
 201                if hover_result.is_empty() {
 202                    return None;
 203                }
 204
 205                // Create symbol range of anchors for highlighting and filtering
 206                // of future requests.
 207                let range = if let Some(range) = hover_result.range {
 208                    let start = snapshot
 209                        .buffer_snapshot
 210                        .anchor_in_excerpt(excerpt_id.clone(), range.start);
 211                    let end = snapshot
 212                        .buffer_snapshot
 213                        .anchor_in_excerpt(excerpt_id.clone(), range.end);
 214
 215                    start..end
 216                } else {
 217                    anchor..anchor
 218                };
 219
 220                Some(InfoPopover {
 221                    project: project.clone(),
 222                    symbol_range: range,
 223                    blocks: hover_result.contents,
 224                    language: hover_result.language,
 225                    rendered_content: None,
 226                })
 227            });
 228
 229            this.update(&mut cx, |this, cx| {
 230                if let Some(hover_popover) = hover_popover.as_ref() {
 231                    // Highlight the selected symbol using a background highlight
 232                    this.highlight_background::<HoverState>(
 233                        vec![hover_popover.symbol_range.clone()],
 234                        |theme| theme.editor.hover_popover.highlight,
 235                        cx,
 236                    );
 237                } else {
 238                    this.clear_background_highlights::<HoverState>(cx);
 239                }
 240
 241                this.hover_state.info_popover = hover_popover;
 242                cx.notify();
 243            })?;
 244
 245            Ok::<_, anyhow::Error>(())
 246        }
 247        .log_err()
 248    });
 249
 250    editor.hover_state.info_task = Some(task);
 251}
 252
 253fn render_blocks(
 254    theme_id: usize,
 255    blocks: &[HoverBlock],
 256    language_registry: &Arc<LanguageRegistry>,
 257    language: Option<&Arc<Language>>,
 258    style: &EditorStyle,
 259) -> RenderedInfo {
 260    let mut text = String::new();
 261    let mut highlights = Vec::new();
 262    let mut region_ranges = Vec::new();
 263    let mut regions = Vec::new();
 264
 265    for block in blocks {
 266        match &block.kind {
 267            HoverBlockKind::PlainText => {
 268                new_paragraph(&mut text, &mut Vec::new());
 269                text.push_str(&block.text);
 270            }
 271            HoverBlockKind::Markdown => {
 272                use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
 273
 274                let mut bold_depth = 0;
 275                let mut italic_depth = 0;
 276                let mut link_url = None;
 277                let mut current_language = None;
 278                let mut list_stack = Vec::new();
 279
 280                for event in Parser::new_ext(&block.text, Options::all()) {
 281                    let prev_len = text.len();
 282                    match event {
 283                        Event::Text(t) => {
 284                            if let Some(language) = &current_language {
 285                                render_code(
 286                                    &mut text,
 287                                    &mut highlights,
 288                                    t.as_ref(),
 289                                    language,
 290                                    style,
 291                                );
 292                            } else {
 293                                text.push_str(t.as_ref());
 294
 295                                let mut style = HighlightStyle::default();
 296                                if bold_depth > 0 {
 297                                    style.weight = Some(Weight::BOLD);
 298                                }
 299                                if italic_depth > 0 {
 300                                    style.italic = Some(true);
 301                                }
 302                                if let Some(link_url) = link_url.clone() {
 303                                    region_ranges.push(prev_len..text.len());
 304                                    regions.push(RenderedRegion {
 305                                        link_url: Some(link_url),
 306                                        code: false,
 307                                    });
 308                                    style.underline = Some(Underline {
 309                                        thickness: 1.0.into(),
 310                                        ..Default::default()
 311                                    });
 312                                }
 313
 314                                if style != HighlightStyle::default() {
 315                                    let mut new_highlight = true;
 316                                    if let Some((last_range, last_style)) = highlights.last_mut() {
 317                                        if last_range.end == prev_len && last_style == &style {
 318                                            last_range.end = text.len();
 319                                            new_highlight = false;
 320                                        }
 321                                    }
 322                                    if new_highlight {
 323                                        highlights.push((prev_len..text.len(), style));
 324                                    }
 325                                }
 326                            }
 327                        }
 328                        Event::Code(t) => {
 329                            text.push_str(t.as_ref());
 330                            region_ranges.push(prev_len..text.len());
 331                            if link_url.is_some() {
 332                                highlights.push((
 333                                    prev_len..text.len(),
 334                                    HighlightStyle {
 335                                        underline: Some(Underline {
 336                                            thickness: 1.0.into(),
 337                                            ..Default::default()
 338                                        }),
 339                                        ..Default::default()
 340                                    },
 341                                ));
 342                            }
 343                            regions.push(RenderedRegion {
 344                                code: true,
 345                                link_url: link_url.clone(),
 346                            });
 347                        }
 348                        Event::Start(tag) => match tag {
 349                            Tag::Paragraph => new_paragraph(&mut text, &mut list_stack),
 350                            Tag::Heading(_, _, _) => {
 351                                new_paragraph(&mut text, &mut list_stack);
 352                                bold_depth += 1;
 353                            }
 354                            Tag::CodeBlock(kind) => {
 355                                new_paragraph(&mut text, &mut list_stack);
 356                                current_language = if let CodeBlockKind::Fenced(language) = kind {
 357                                    language_registry
 358                                        .language_for_name(language.as_ref())
 359                                        .now_or_never()
 360                                        .and_then(Result::ok)
 361                                } else {
 362                                    language.cloned()
 363                                }
 364                            }
 365                            Tag::Emphasis => italic_depth += 1,
 366                            Tag::Strong => bold_depth += 1,
 367                            Tag::Link(_, url, _) => link_url = Some(url.to_string()),
 368                            Tag::List(number) => {
 369                                list_stack.push((number, false));
 370                            }
 371                            Tag::Item => {
 372                                let len = list_stack.len();
 373                                if let Some((list_number, has_content)) = list_stack.last_mut() {
 374                                    *has_content = false;
 375                                    if !text.is_empty() && !text.ends_with('\n') {
 376                                        text.push('\n');
 377                                    }
 378                                    for _ in 0..len - 1 {
 379                                        text.push_str("  ");
 380                                    }
 381                                    if let Some(number) = list_number {
 382                                        text.push_str(&format!("{}. ", number));
 383                                        *number += 1;
 384                                        *has_content = false;
 385                                    } else {
 386                                        text.push_str("- ");
 387                                    }
 388                                }
 389                            }
 390                            _ => {}
 391                        },
 392                        Event::End(tag) => match tag {
 393                            Tag::Heading(_, _, _) => bold_depth -= 1,
 394                            Tag::CodeBlock(_) => current_language = None,
 395                            Tag::Emphasis => italic_depth -= 1,
 396                            Tag::Strong => bold_depth -= 1,
 397                            Tag::Link(_, _, _) => link_url = None,
 398                            Tag::List(_) => drop(list_stack.pop()),
 399                            _ => {}
 400                        },
 401                        Event::HardBreak => text.push('\n'),
 402                        Event::SoftBreak => text.push(' '),
 403                        _ => {}
 404                    }
 405                }
 406            }
 407            HoverBlockKind::Code { language } => {
 408                if let Some(language) = language_registry
 409                    .language_for_name(language)
 410                    .now_or_never()
 411                    .and_then(Result::ok)
 412                {
 413                    render_code(&mut text, &mut highlights, &block.text, &language, style);
 414                } else {
 415                    text.push_str(&block.text);
 416                }
 417            }
 418        }
 419    }
 420
 421    RenderedInfo {
 422        theme_id,
 423        text: text.trim().to_string(),
 424        highlights,
 425        region_ranges,
 426        regions,
 427    }
 428}
 429
 430fn render_code(
 431    text: &mut String,
 432    highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
 433    content: &str,
 434    language: &Arc<Language>,
 435    style: &EditorStyle,
 436) {
 437    let prev_len = text.len();
 438    text.push_str(content);
 439    for (range, highlight_id) in language.highlight_text(&content.into(), 0..content.len()) {
 440        if let Some(style) = highlight_id.style(&style.syntax) {
 441            highlights.push((prev_len + range.start..prev_len + range.end, style));
 442        }
 443    }
 444}
 445
 446fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option<u64>, bool)>) {
 447    let mut is_subsequent_paragraph_of_list = false;
 448    if let Some((_, has_content)) = list_stack.last_mut() {
 449        if *has_content {
 450            is_subsequent_paragraph_of_list = true;
 451        } else {
 452            *has_content = true;
 453            return;
 454        }
 455    }
 456
 457    if !text.is_empty() {
 458        if !text.ends_with('\n') {
 459            text.push('\n');
 460        }
 461        text.push('\n');
 462    }
 463    for _ in 0..list_stack.len().saturating_sub(1) {
 464        text.push_str("  ");
 465    }
 466    if is_subsequent_paragraph_of_list {
 467        text.push_str("  ");
 468    }
 469}
 470
 471#[derive(Default)]
 472pub struct HoverState {
 473    pub info_popover: Option<InfoPopover>,
 474    pub diagnostic_popover: Option<DiagnosticPopover>,
 475    pub triggered_from: Option<Anchor>,
 476    pub info_task: Option<Task<Option<()>>>,
 477}
 478
 479impl HoverState {
 480    pub fn visible(&self) -> bool {
 481        self.info_popover.is_some() || self.diagnostic_popover.is_some()
 482    }
 483
 484    pub fn render(
 485        &mut self,
 486        snapshot: &EditorSnapshot,
 487        style: &EditorStyle,
 488        visible_rows: Range<u32>,
 489        cx: &mut ViewContext<Editor>,
 490    ) -> Option<(DisplayPoint, Vec<AnyElement<Editor>>)> {
 491        // If there is a diagnostic, position the popovers based on that.
 492        // Otherwise use the start of the hover range
 493        let anchor = self
 494            .diagnostic_popover
 495            .as_ref()
 496            .map(|diagnostic_popover| &diagnostic_popover.local_diagnostic.range.start)
 497            .or_else(|| {
 498                self.info_popover
 499                    .as_ref()
 500                    .map(|info_popover| &info_popover.symbol_range.start)
 501            })?;
 502        let point = anchor.to_display_point(&snapshot.display_snapshot);
 503
 504        // Don't render if the relevant point isn't on screen
 505        if !self.visible() || !visible_rows.contains(&point.row()) {
 506            return None;
 507        }
 508
 509        let mut elements = Vec::new();
 510
 511        if let Some(diagnostic_popover) = self.diagnostic_popover.as_ref() {
 512            elements.push(diagnostic_popover.render(style, cx));
 513        }
 514        if let Some(info_popover) = self.info_popover.as_mut() {
 515            elements.push(info_popover.render(style, cx));
 516        }
 517
 518        Some((point, elements))
 519    }
 520}
 521
 522#[derive(Debug, Clone)]
 523pub struct InfoPopover {
 524    pub project: ModelHandle<Project>,
 525    pub symbol_range: Range<Anchor>,
 526    pub blocks: Vec<HoverBlock>,
 527    language: Option<Arc<Language>>,
 528    rendered_content: Option<RenderedInfo>,
 529}
 530
 531#[derive(Debug, Clone)]
 532struct RenderedInfo {
 533    theme_id: usize,
 534    text: String,
 535    highlights: Vec<(Range<usize>, HighlightStyle)>,
 536    region_ranges: Vec<Range<usize>>,
 537    regions: Vec<RenderedRegion>,
 538}
 539
 540#[derive(Debug, Clone)]
 541struct RenderedRegion {
 542    code: bool,
 543    link_url: Option<String>,
 544}
 545
 546impl InfoPopover {
 547    pub fn render(
 548        &mut self,
 549        style: &EditorStyle,
 550        cx: &mut ViewContext<Editor>,
 551    ) -> AnyElement<Editor> {
 552        if let Some(rendered) = &self.rendered_content {
 553            if rendered.theme_id != style.theme_id {
 554                self.rendered_content = None;
 555            }
 556        }
 557
 558        let rendered_content = self.rendered_content.get_or_insert_with(|| {
 559            render_blocks(
 560                style.theme_id,
 561                &self.blocks,
 562                self.project.read(cx).languages(),
 563                self.language.as_ref(),
 564                style,
 565            )
 566        });
 567
 568        MouseEventHandler::new::<InfoPopover, _>(0, cx, |_, cx| {
 569            let mut region_id = 0;
 570            let view_id = cx.view_id();
 571
 572            let code_span_background_color = style.document_highlight_read_background;
 573            let regions = rendered_content.regions.clone();
 574            Flex::column()
 575                .scrollable::<HoverBlock>(1, None, cx)
 576                .with_child(
 577                    Text::new(rendered_content.text.clone(), style.text.clone())
 578                        .with_highlights(rendered_content.highlights.clone())
 579                        .with_custom_runs(
 580                            rendered_content.region_ranges.clone(),
 581                            move |ix, bounds, scene, _| {
 582                                region_id += 1;
 583                                let region = regions[ix].clone();
 584                                if let Some(url) = region.link_url {
 585                                    scene.push_cursor_region(CursorRegion {
 586                                        bounds,
 587                                        style: CursorStyle::PointingHand,
 588                                    });
 589                                    scene.push_mouse_region(
 590                                        MouseRegion::new::<Self>(view_id, region_id, bounds)
 591                                            .on_click::<Editor, _>(
 592                                                MouseButton::Left,
 593                                                move |_, _, cx| cx.platform().open_url(&url),
 594                                            ),
 595                                    );
 596                                }
 597                                if region.code {
 598                                    scene.push_quad(gpui::Quad {
 599                                        bounds,
 600                                        background: Some(code_span_background_color),
 601                                        border: Default::default(),
 602                                        corner_radii: (2.0).into(),
 603                                    });
 604                                }
 605                            },
 606                        )
 607                        .with_soft_wrap(true),
 608                )
 609                .contained()
 610                .with_style(style.hover_popover.container)
 611        })
 612        .on_move(|_, _, _| {}) // Consume move events so they don't reach regions underneath.
 613        .with_cursor_style(CursorStyle::Arrow)
 614        .with_padding(Padding {
 615            bottom: HOVER_POPOVER_GAP,
 616            top: HOVER_POPOVER_GAP,
 617            ..Default::default()
 618        })
 619        .into_any()
 620    }
 621}
 622
 623#[derive(Debug, Clone)]
 624pub struct DiagnosticPopover {
 625    local_diagnostic: DiagnosticEntry<Anchor>,
 626    primary_diagnostic: Option<DiagnosticEntry<Anchor>>,
 627}
 628
 629impl DiagnosticPopover {
 630    pub fn render(&self, style: &EditorStyle, cx: &mut ViewContext<Editor>) -> AnyElement<Editor> {
 631        enum PrimaryDiagnostic {}
 632
 633        let mut text_style = style.hover_popover.prose.clone();
 634        text_style.font_size = style.text.font_size;
 635        let diagnostic_source_style = style.hover_popover.diagnostic_source_highlight.clone();
 636
 637        let text = match &self.local_diagnostic.diagnostic.source {
 638            Some(source) => Text::new(
 639                format!("{source}: {}", self.local_diagnostic.diagnostic.message),
 640                text_style,
 641            )
 642            .with_highlights(vec![(0..source.len(), diagnostic_source_style)]),
 643
 644            None => Text::new(self.local_diagnostic.diagnostic.message.clone(), text_style),
 645        };
 646
 647        let container_style = match self.local_diagnostic.diagnostic.severity {
 648            DiagnosticSeverity::HINT => style.hover_popover.info_container,
 649            DiagnosticSeverity::INFORMATION => style.hover_popover.info_container,
 650            DiagnosticSeverity::WARNING => style.hover_popover.warning_container,
 651            DiagnosticSeverity::ERROR => style.hover_popover.error_container,
 652            _ => style.hover_popover.container,
 653        };
 654
 655        let tooltip_style = theme::current(cx).tooltip.clone();
 656
 657        MouseEventHandler::new::<DiagnosticPopover, _>(0, cx, |_, _| {
 658            text.with_soft_wrap(true)
 659                .contained()
 660                .with_style(container_style)
 661        })
 662        .with_padding(Padding {
 663            top: HOVER_POPOVER_GAP,
 664            bottom: HOVER_POPOVER_GAP,
 665            ..Default::default()
 666        })
 667        .on_move(|_, _, _| {}) // Consume move events so they don't reach regions underneath.
 668        .on_click(MouseButton::Left, |_, this, cx| {
 669            this.go_to_diagnostic(&Default::default(), cx)
 670        })
 671        .with_cursor_style(CursorStyle::PointingHand)
 672        .with_tooltip::<PrimaryDiagnostic>(
 673            0,
 674            "Go To Diagnostic".to_string(),
 675            Some(Box::new(crate::GoToDiagnostic)),
 676            tooltip_style,
 677            cx,
 678        )
 679        .into_any()
 680    }
 681
 682    pub fn activation_info(&self) -> (usize, Anchor) {
 683        let entry = self
 684            .primary_diagnostic
 685            .as_ref()
 686            .unwrap_or(&self.local_diagnostic);
 687
 688        (entry.diagnostic.group_id, entry.range.start.clone())
 689    }
 690}
 691
 692#[cfg(test)]
 693mod tests {
 694    use super::*;
 695    use crate::{editor_tests::init_test, test::editor_lsp_test_context::EditorLspTestContext};
 696    use gpui::fonts::Weight;
 697    use indoc::indoc;
 698    use language::{Diagnostic, DiagnosticSet};
 699    use lsp::LanguageServerId;
 700    use project::{HoverBlock, HoverBlockKind};
 701    use smol::stream::StreamExt;
 702    use unindent::Unindent;
 703    use util::test::marked_text_ranges;
 704
 705    #[gpui::test]
 706    async fn test_mouse_hover_info_popover(cx: &mut gpui::TestAppContext) {
 707        init_test(cx, |_| {});
 708
 709        let mut cx = EditorLspTestContext::new_rust(
 710            lsp::ServerCapabilities {
 711                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 712                ..Default::default()
 713            },
 714            cx,
 715        )
 716        .await;
 717
 718        // Basic hover delays and then pops without moving the mouse
 719        cx.set_state(indoc! {"
 720            fn ˇtest() { println!(); }
 721        "});
 722        let hover_point = cx.display_point(indoc! {"
 723            fn test() { printˇln!(); }
 724        "});
 725
 726        cx.update_editor(|editor, cx| hover_at(editor, Some(hover_point), cx));
 727        assert!(!cx.editor(|editor, _| editor.hover_state.visible()));
 728
 729        // After delay, hover should be visible.
 730        let symbol_range = cx.lsp_range(indoc! {"
 731            fn test() { «println!»(); }
 732        "});
 733        let mut requests =
 734            cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 735                Ok(Some(lsp::Hover {
 736                    contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 737                        kind: lsp::MarkupKind::Markdown,
 738                        value: "some basic docs".to_string(),
 739                    }),
 740                    range: Some(symbol_range),
 741                }))
 742            });
 743        cx.foreground()
 744            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
 745        requests.next().await;
 746
 747        cx.editor(|editor, _| {
 748            assert!(editor.hover_state.visible());
 749            assert_eq!(
 750                editor.hover_state.info_popover.clone().unwrap().blocks,
 751                vec![HoverBlock {
 752                    text: "some basic docs".to_string(),
 753                    kind: HoverBlockKind::Markdown,
 754                },]
 755            )
 756        });
 757
 758        // Mouse moved with no hover response dismisses
 759        let hover_point = cx.display_point(indoc! {"
 760            fn teˇst() { println!(); }
 761        "});
 762        let mut request = cx
 763            .lsp
 764            .handle_request::<lsp::request::HoverRequest, _, _>(|_, _| async move { Ok(None) });
 765        cx.update_editor(|editor, cx| hover_at(editor, Some(hover_point), cx));
 766        cx.foreground()
 767            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
 768        request.next().await;
 769        cx.editor(|editor, _| {
 770            assert!(!editor.hover_state.visible());
 771        });
 772    }
 773
 774    #[gpui::test]
 775    async fn test_keyboard_hover_info_popover(cx: &mut gpui::TestAppContext) {
 776        init_test(cx, |_| {});
 777
 778        let mut cx = EditorLspTestContext::new_rust(
 779            lsp::ServerCapabilities {
 780                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 781                ..Default::default()
 782            },
 783            cx,
 784        )
 785        .await;
 786
 787        // Hover with keyboard has no delay
 788        cx.set_state(indoc! {"
 789            fˇn test() { println!(); }
 790        "});
 791        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 792        let symbol_range = cx.lsp_range(indoc! {"
 793            «fn» test() { println!(); }
 794        "});
 795        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 796            Ok(Some(lsp::Hover {
 797                contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 798                    kind: lsp::MarkupKind::Markdown,
 799                    value: "some other basic docs".to_string(),
 800                }),
 801                range: Some(symbol_range),
 802            }))
 803        })
 804        .next()
 805        .await;
 806
 807        cx.condition(|editor, _| editor.hover_state.visible()).await;
 808        cx.editor(|editor, _| {
 809            assert_eq!(
 810                editor.hover_state.info_popover.clone().unwrap().blocks,
 811                vec![HoverBlock {
 812                    text: "some other basic docs".to_string(),
 813                    kind: HoverBlockKind::Markdown,
 814                }]
 815            )
 816        });
 817    }
 818
 819    #[gpui::test]
 820    async fn test_empty_hovers_filtered(cx: &mut gpui::TestAppContext) {
 821        init_test(cx, |_| {});
 822
 823        let mut cx = EditorLspTestContext::new_rust(
 824            lsp::ServerCapabilities {
 825                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 826                ..Default::default()
 827            },
 828            cx,
 829        )
 830        .await;
 831
 832        // Hover with keyboard has no delay
 833        cx.set_state(indoc! {"
 834            fˇn test() { println!(); }
 835        "});
 836        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 837        let symbol_range = cx.lsp_range(indoc! {"
 838            «fn» test() { println!(); }
 839        "});
 840        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 841            Ok(Some(lsp::Hover {
 842                contents: lsp::HoverContents::Array(vec![
 843                    lsp::MarkedString::String("regular text for hover to show".to_string()),
 844                    lsp::MarkedString::String("".to_string()),
 845                    lsp::MarkedString::LanguageString(lsp::LanguageString {
 846                        language: "Rust".to_string(),
 847                        value: "".to_string(),
 848                    }),
 849                ]),
 850                range: Some(symbol_range),
 851            }))
 852        })
 853        .next()
 854        .await;
 855
 856        cx.condition(|editor, _| editor.hover_state.visible()).await;
 857        cx.editor(|editor, _| {
 858            assert_eq!(
 859                editor.hover_state.info_popover.clone().unwrap().blocks,
 860                vec![HoverBlock {
 861                    text: "regular text for hover to show".to_string(),
 862                    kind: HoverBlockKind::Markdown,
 863                }],
 864                "No empty string hovers should be shown"
 865            );
 866        });
 867    }
 868
 869    #[gpui::test]
 870    async fn test_line_ends_trimmed(cx: &mut gpui::TestAppContext) {
 871        init_test(cx, |_| {});
 872
 873        let mut cx = EditorLspTestContext::new_rust(
 874            lsp::ServerCapabilities {
 875                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 876                ..Default::default()
 877            },
 878            cx,
 879        )
 880        .await;
 881
 882        // Hover with keyboard has no delay
 883        cx.set_state(indoc! {"
 884            fˇn test() { println!(); }
 885        "});
 886        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 887        let symbol_range = cx.lsp_range(indoc! {"
 888            «fn» test() { println!(); }
 889        "});
 890
 891        let code_str = "\nlet hovered_point: Vector2F // size = 8, align = 0x4\n";
 892        let markdown_string = format!("\n```rust\n{code_str}```");
 893
 894        let closure_markdown_string = markdown_string.clone();
 895        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| {
 896            let future_markdown_string = closure_markdown_string.clone();
 897            async move {
 898                Ok(Some(lsp::Hover {
 899                    contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 900                        kind: lsp::MarkupKind::Markdown,
 901                        value: future_markdown_string,
 902                    }),
 903                    range: Some(symbol_range),
 904                }))
 905            }
 906        })
 907        .next()
 908        .await;
 909
 910        cx.condition(|editor, _| editor.hover_state.visible()).await;
 911        cx.editor(|editor, cx| {
 912            let blocks = editor.hover_state.info_popover.clone().unwrap().blocks;
 913            assert_eq!(
 914                blocks,
 915                vec![HoverBlock {
 916                    text: markdown_string,
 917                    kind: HoverBlockKind::Markdown,
 918                }],
 919            );
 920
 921            let style = editor.style(cx);
 922            let rendered = render_blocks(0, &blocks, &Default::default(), None, &style);
 923            assert_eq!(
 924                rendered.text,
 925                code_str.trim(),
 926                "Should not have extra line breaks at end of rendered hover"
 927            );
 928        });
 929    }
 930
 931    #[gpui::test]
 932    async fn test_hover_diagnostic_and_info_popovers(cx: &mut gpui::TestAppContext) {
 933        init_test(cx, |_| {});
 934
 935        let mut cx = EditorLspTestContext::new_rust(
 936            lsp::ServerCapabilities {
 937                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 938                ..Default::default()
 939            },
 940            cx,
 941        )
 942        .await;
 943
 944        // Hover with just diagnostic, pops DiagnosticPopover immediately and then
 945        // info popover once request completes
 946        cx.set_state(indoc! {"
 947            fn teˇst() { println!(); }
 948        "});
 949
 950        // Send diagnostic to client
 951        let range = cx.text_anchor_range(indoc! {"
 952            fn «test»() { println!(); }
 953        "});
 954        cx.update_buffer(|buffer, cx| {
 955            let snapshot = buffer.text_snapshot();
 956            let set = DiagnosticSet::from_sorted_entries(
 957                vec![DiagnosticEntry {
 958                    range,
 959                    diagnostic: Diagnostic {
 960                        message: "A test diagnostic message.".to_string(),
 961                        ..Default::default()
 962                    },
 963                }],
 964                &snapshot,
 965            );
 966            buffer.update_diagnostics(LanguageServerId(0), set, cx);
 967        });
 968
 969        // Hover pops diagnostic immediately
 970        cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
 971        cx.foreground().run_until_parked();
 972
 973        cx.editor(|Editor { hover_state, .. }, _| {
 974            assert!(hover_state.diagnostic_popover.is_some() && hover_state.info_popover.is_none())
 975        });
 976
 977        // Info Popover shows after request responded to
 978        let range = cx.lsp_range(indoc! {"
 979            fn «test»() { println!(); }
 980        "});
 981        cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
 982            Ok(Some(lsp::Hover {
 983                contents: lsp::HoverContents::Markup(lsp::MarkupContent {
 984                    kind: lsp::MarkupKind::Markdown,
 985                    value: "some new docs".to_string(),
 986                }),
 987                range: Some(range),
 988            }))
 989        });
 990        cx.foreground()
 991            .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
 992
 993        cx.foreground().run_until_parked();
 994        cx.editor(|Editor { hover_state, .. }, _| {
 995            hover_state.diagnostic_popover.is_some() && hover_state.info_task.is_some()
 996        });
 997    }
 998
 999    #[gpui::test]
1000    fn test_render_blocks(cx: &mut gpui::TestAppContext) {
1001        init_test(cx, |_| {});
1002
1003        cx.add_window(|cx| {
1004            let editor = Editor::single_line(None, cx);
1005            let style = editor.style(cx);
1006
1007            struct Row {
1008                blocks: Vec<HoverBlock>,
1009                expected_marked_text: String,
1010                expected_styles: Vec<HighlightStyle>,
1011            }
1012
1013            let rows = &[
1014                // Strong emphasis
1015                Row {
1016                    blocks: vec![HoverBlock {
1017                        text: "one **two** three".to_string(),
1018                        kind: HoverBlockKind::Markdown,
1019                    }],
1020                    expected_marked_text: "one «two» three".to_string(),
1021                    expected_styles: vec![HighlightStyle {
1022                        weight: Some(Weight::BOLD),
1023                        ..Default::default()
1024                    }],
1025                },
1026                // Links
1027                Row {
1028                    blocks: vec![HoverBlock {
1029                        text: "one [two](the-url) three".to_string(),
1030                        kind: HoverBlockKind::Markdown,
1031                    }],
1032                    expected_marked_text: "one «two» three".to_string(),
1033                    expected_styles: vec![HighlightStyle {
1034                        underline: Some(Underline {
1035                            thickness: 1.0.into(),
1036                            ..Default::default()
1037                        }),
1038                        ..Default::default()
1039                    }],
1040                },
1041                // Lists
1042                Row {
1043                    blocks: vec![HoverBlock {
1044                        text: "
1045                            lists:
1046                            * one
1047                                - a
1048                                - b
1049                            * two
1050                                - [c](the-url)
1051                                - d"
1052                        .unindent(),
1053                        kind: HoverBlockKind::Markdown,
1054                    }],
1055                    expected_marked_text: "
1056                        lists:
1057                        - one
1058                          - a
1059                          - b
1060                        - two
1061                          - «c»
1062                          - d"
1063                    .unindent(),
1064                    expected_styles: vec![HighlightStyle {
1065                        underline: Some(Underline {
1066                            thickness: 1.0.into(),
1067                            ..Default::default()
1068                        }),
1069                        ..Default::default()
1070                    }],
1071                },
1072                // Multi-paragraph list items
1073                Row {
1074                    blocks: vec![HoverBlock {
1075                        text: "
1076                            * one two
1077                              three
1078
1079                            * four five
1080                                * six seven
1081                                  eight
1082
1083                                  nine
1084                                * ten
1085                            * six"
1086                            .unindent(),
1087                        kind: HoverBlockKind::Markdown,
1088                    }],
1089                    expected_marked_text: "
1090                        - one two three
1091                        - four five
1092                          - six seven eight
1093
1094                            nine
1095                          - ten
1096                        - six"
1097                        .unindent(),
1098                    expected_styles: vec![HighlightStyle {
1099                        underline: Some(Underline {
1100                            thickness: 1.0.into(),
1101                            ..Default::default()
1102                        }),
1103                        ..Default::default()
1104                    }],
1105                },
1106            ];
1107
1108            for Row {
1109                blocks,
1110                expected_marked_text,
1111                expected_styles,
1112            } in &rows[0..]
1113            {
1114                let rendered = render_blocks(0, &blocks, &Default::default(), None, &style);
1115
1116                let (expected_text, ranges) = marked_text_ranges(expected_marked_text, false);
1117                let expected_highlights = ranges
1118                    .into_iter()
1119                    .zip(expected_styles.iter().cloned())
1120                    .collect::<Vec<_>>();
1121                assert_eq!(
1122                    rendered.text, expected_text,
1123                    "wrong text for input {blocks:?}"
1124                );
1125                assert_eq!(
1126                    rendered.highlights, expected_highlights,
1127                    "wrong highlights for input {blocks:?}"
1128                );
1129            }
1130
1131            editor
1132        });
1133    }
1134}