1use futures::FutureExt;
2use gpui::{
3 actions,
4 elements::{Flex, MouseEventHandler, Padding, ParentElement, Text},
5 fonts::{HighlightStyle, Underline, Weight},
6 platform::{CursorStyle, MouseButton},
7 AnyElement, AppContext, CursorRegion, Element, ModelHandle, MouseRegion, Task, ViewContext,
8};
9use language::{Bias, DiagnosticEntry, DiagnosticSeverity, Language, LanguageRegistry};
10use project::{HoverBlock, HoverBlockKind, Project};
11use settings::Settings;
12use std::{ops::Range, sync::Arc, time::Duration};
13use util::TryFutureExt;
14
15use crate::{
16 display_map::ToDisplayPoint, Anchor, AnchorRangeExt, DisplayPoint, Editor, EditorSnapshot,
17 EditorStyle, GoToDiagnostic, RangeToAnchorExt,
18};
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 cx.global::<Settings>().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
51/// Hides the type information popup.
52/// Triggered by the `Hover` action when the cursor is not over a symbol or when the
53/// selections changed.
54pub fn hide_hover(editor: &mut Editor, cx: &mut ViewContext<Editor>) -> bool {
55 let did_hide = editor.hover_state.info_popover.take().is_some()
56 | editor.hover_state.diagnostic_popover.take().is_some();
57
58 editor.hover_state.info_task = None;
59 editor.hover_state.triggered_from = None;
60
61 editor.clear_background_highlights::<HoverState>(cx);
62
63 if did_hide {
64 cx.notify();
65 }
66
67 did_hide
68}
69
70/// Queries the LSP and shows type info and documentation
71/// about the symbol the mouse is currently hovering over.
72/// Triggered by the `Hover` action when the cursor may be over a symbol.
73fn show_hover(
74 editor: &mut Editor,
75 point: DisplayPoint,
76 ignore_timeout: bool,
77 cx: &mut ViewContext<Editor>,
78) {
79 if editor.pending_rename.is_some() {
80 return;
81 }
82
83 let snapshot = editor.snapshot(cx);
84 let multibuffer_offset = point.to_offset(&snapshot.display_snapshot, Bias::Left);
85
86 let (buffer, buffer_position) = if let Some(output) = editor
87 .buffer
88 .read(cx)
89 .text_anchor_for_position(multibuffer_offset, cx)
90 {
91 output
92 } else {
93 return;
94 };
95
96 let excerpt_id = if let Some((excerpt_id, _, _)) = editor
97 .buffer()
98 .read(cx)
99 .excerpt_containing(multibuffer_offset, cx)
100 {
101 excerpt_id
102 } else {
103 return;
104 };
105
106 let project = if let Some(project) = editor.project.clone() {
107 project
108 } else {
109 return;
110 };
111
112 if !ignore_timeout {
113 if let Some(InfoPopover { symbol_range, .. }) = &editor.hover_state.info_popover {
114 if symbol_range
115 .to_offset(&snapshot.buffer_snapshot)
116 .contains(&multibuffer_offset)
117 {
118 // Hover triggered from same location as last time. Don't show again.
119 return;
120 } else {
121 hide_hover(editor, cx);
122 }
123 }
124 }
125
126 // Get input anchor
127 let anchor = snapshot
128 .buffer_snapshot
129 .anchor_at(multibuffer_offset, Bias::Left);
130
131 // Don't request again if the location is the same as the previous request
132 if let Some(triggered_from) = &editor.hover_state.triggered_from {
133 if triggered_from
134 .cmp(&anchor, &snapshot.buffer_snapshot)
135 .is_eq()
136 {
137 return;
138 }
139 }
140
141 let task = cx.spawn(|this, mut cx| {
142 async move {
143 // If we need to delay, delay a set amount initially before making the lsp request
144 let delay = if !ignore_timeout {
145 // Construct delay task to wait for later
146 let total_delay = Some(
147 cx.background()
148 .timer(Duration::from_millis(HOVER_DELAY_MILLIS)),
149 );
150
151 cx.background()
152 .timer(Duration::from_millis(HOVER_REQUEST_DELAY_MILLIS))
153 .await;
154 total_delay
155 } else {
156 None
157 };
158
159 // query the LSP for hover info
160 let hover_request = cx.update(|cx| {
161 project.update(cx, |project, cx| {
162 project.hover(&buffer, buffer_position, cx)
163 })
164 });
165
166 if let Some(delay) = delay {
167 delay.await;
168 }
169
170 // If there's a diagnostic, assign it on the hover state and notify
171 let local_diagnostic = snapshot
172 .buffer_snapshot
173 .diagnostics_in_range::<_, usize>(multibuffer_offset..multibuffer_offset, false)
174 // Find the entry with the most specific range
175 .min_by_key(|entry| entry.range.end - entry.range.start)
176 .map(|entry| DiagnosticEntry {
177 diagnostic: entry.diagnostic,
178 range: entry.range.to_anchors(&snapshot.buffer_snapshot),
179 });
180
181 // Pull the primary diagnostic out so we can jump to it if the popover is clicked
182 let primary_diagnostic = local_diagnostic.as_ref().and_then(|local_diagnostic| {
183 snapshot
184 .buffer_snapshot
185 .diagnostic_group::<usize>(local_diagnostic.diagnostic.group_id)
186 .find(|diagnostic| diagnostic.diagnostic.is_primary)
187 .map(|entry| DiagnosticEntry {
188 diagnostic: entry.diagnostic,
189 range: entry.range.to_anchors(&snapshot.buffer_snapshot),
190 })
191 });
192
193 this.update(&mut cx, |this, _| {
194 this.hover_state.diagnostic_popover =
195 local_diagnostic.map(|local_diagnostic| DiagnosticPopover {
196 local_diagnostic,
197 primary_diagnostic,
198 });
199 })?;
200
201 // Construct new hover popover from hover request
202 let hover_popover = hover_request.await.ok().flatten().and_then(|hover_result| {
203 if hover_result.contents.is_empty() {
204 return None;
205 }
206
207 // Create symbol range of anchors for highlighting and filtering
208 // of future requests.
209 let range = if let Some(range) = hover_result.range {
210 let start = snapshot
211 .buffer_snapshot
212 .anchor_in_excerpt(excerpt_id.clone(), range.start);
213 let end = snapshot
214 .buffer_snapshot
215 .anchor_in_excerpt(excerpt_id.clone(), range.end);
216
217 start..end
218 } else {
219 anchor..anchor
220 };
221
222 Some(InfoPopover {
223 project: project.clone(),
224 symbol_range: range,
225 blocks: hover_result.contents,
226 rendered_content: None,
227 })
228 });
229
230 this.update(&mut cx, |this, cx| {
231 if let Some(hover_popover) = hover_popover.as_ref() {
232 // Highlight the selected symbol using a background highlight
233 this.highlight_background::<HoverState>(
234 vec![hover_popover.symbol_range.clone()],
235 |theme| theme.editor.hover_popover.highlight,
236 cx,
237 );
238 } else {
239 this.clear_background_highlights::<HoverState>(cx);
240 }
241
242 this.hover_state.info_popover = hover_popover;
243 cx.notify();
244 })?;
245
246 Ok::<_, anyhow::Error>(())
247 }
248 .log_err()
249 });
250
251 editor.hover_state.info_task = Some(task);
252}
253
254fn render_blocks(
255 theme_id: usize,
256 blocks: &[HoverBlock],
257 language_registry: &Arc<LanguageRegistry>,
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) = ¤t_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 if let CodeBlockKind::Fenced(language) = kind {
357 current_language = language_registry
358 .language_for_name(language.as_ref())
359 .now_or_never()
360 .and_then(Result::ok);
361 }
362 }
363 Tag::Emphasis => italic_depth += 1,
364 Tag::Strong => bold_depth += 1,
365 Tag::Link(_, url, _) => link_url = Some(url.to_string()),
366 Tag::List(number) => {
367 list_stack.push((number, false));
368 }
369 Tag::Item => {
370 let len = list_stack.len();
371 if let Some((list_number, has_content)) = list_stack.last_mut() {
372 *has_content = false;
373 if !text.is_empty() && !text.ends_with('\n') {
374 text.push('\n');
375 }
376 for _ in 0..len - 1 {
377 text.push_str(" ");
378 }
379 if let Some(number) = list_number {
380 text.push_str(&format!("{}. ", number));
381 *number += 1;
382 *has_content = false;
383 } else {
384 text.push_str("- ");
385 }
386 }
387 }
388 _ => {}
389 },
390 Event::End(tag) => match tag {
391 Tag::Heading(_, _, _) => bold_depth -= 1,
392 Tag::CodeBlock(_) => current_language = None,
393 Tag::Emphasis => italic_depth -= 1,
394 Tag::Strong => bold_depth -= 1,
395 Tag::Link(_, _, _) => link_url = None,
396 Tag::List(_) => drop(list_stack.pop()),
397 _ => {}
398 },
399 Event::HardBreak => text.push('\n'),
400 Event::SoftBreak => text.push(' '),
401 _ => {}
402 }
403 }
404 }
405 HoverBlockKind::Code { language } => {
406 if let Some(language) = language_registry
407 .language_for_name(language)
408 .now_or_never()
409 .and_then(Result::ok)
410 {
411 render_code(&mut text, &mut highlights, &block.text, &language, style);
412 } else {
413 text.push_str(&block.text);
414 }
415 }
416 }
417 }
418
419 if !text.is_empty() && !text.ends_with('\n') {
420 text.push('\n');
421 }
422
423 RenderedInfo {
424 theme_id,
425 text,
426 highlights,
427 region_ranges,
428 regions,
429 }
430}
431
432fn render_code(
433 text: &mut String,
434 highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
435 content: &str,
436 language: &Arc<Language>,
437 style: &EditorStyle,
438) {
439 let prev_len = text.len();
440 text.push_str(content);
441 for (range, highlight_id) in language.highlight_text(&content.into(), 0..content.len()) {
442 if let Some(style) = highlight_id.style(&style.syntax) {
443 highlights.push((prev_len + range.start..prev_len + range.end, style));
444 }
445 }
446}
447
448fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option<u64>, bool)>) {
449 let mut is_subsequent_paragraph_of_list = false;
450 if let Some((_, has_content)) = list_stack.last_mut() {
451 if *has_content {
452 is_subsequent_paragraph_of_list = true;
453 } else {
454 *has_content = true;
455 return;
456 }
457 }
458
459 if !text.is_empty() {
460 if !text.ends_with('\n') {
461 text.push('\n');
462 }
463 text.push('\n');
464 }
465 for _ in 0..list_stack.len().saturating_sub(1) {
466 text.push_str(" ");
467 }
468 if is_subsequent_paragraph_of_list {
469 text.push_str(" ");
470 }
471}
472
473#[derive(Default)]
474pub struct HoverState {
475 pub info_popover: Option<InfoPopover>,
476 pub diagnostic_popover: Option<DiagnosticPopover>,
477 pub triggered_from: Option<Anchor>,
478 pub info_task: Option<Task<Option<()>>>,
479}
480
481impl HoverState {
482 pub fn visible(&self) -> bool {
483 self.info_popover.is_some() || self.diagnostic_popover.is_some()
484 }
485
486 pub fn render(
487 &mut self,
488 snapshot: &EditorSnapshot,
489 style: &EditorStyle,
490 visible_rows: Range<u32>,
491 cx: &mut ViewContext<Editor>,
492 ) -> Option<(DisplayPoint, Vec<AnyElement<Editor>>)> {
493 // If there is a diagnostic, position the popovers based on that.
494 // Otherwise use the start of the hover range
495 let anchor = self
496 .diagnostic_popover
497 .as_ref()
498 .map(|diagnostic_popover| &diagnostic_popover.local_diagnostic.range.start)
499 .or_else(|| {
500 self.info_popover
501 .as_ref()
502 .map(|info_popover| &info_popover.symbol_range.start)
503 })?;
504 let point = anchor.to_display_point(&snapshot.display_snapshot);
505
506 // Don't render if the relevant point isn't on screen
507 if !self.visible() || !visible_rows.contains(&point.row()) {
508 return None;
509 }
510
511 let mut elements = Vec::new();
512
513 if let Some(diagnostic_popover) = self.diagnostic_popover.as_ref() {
514 elements.push(diagnostic_popover.render(style, cx));
515 }
516 if let Some(info_popover) = self.info_popover.as_mut() {
517 elements.push(info_popover.render(style, cx));
518 }
519
520 Some((point, elements))
521 }
522}
523
524#[derive(Debug, Clone)]
525pub struct InfoPopover {
526 pub project: ModelHandle<Project>,
527 pub symbol_range: Range<Anchor>,
528 pub blocks: Vec<HoverBlock>,
529 rendered_content: Option<RenderedInfo>,
530}
531
532#[derive(Debug, Clone)]
533struct RenderedInfo {
534 theme_id: usize,
535 text: String,
536 highlights: Vec<(Range<usize>, HighlightStyle)>,
537 region_ranges: Vec<Range<usize>>,
538 regions: Vec<RenderedRegion>,
539}
540
541#[derive(Debug, Clone)]
542struct RenderedRegion {
543 code: bool,
544 link_url: Option<String>,
545}
546
547impl InfoPopover {
548 pub fn render(
549 &mut self,
550 style: &EditorStyle,
551 cx: &mut ViewContext<Editor>,
552 ) -> AnyElement<Editor> {
553 if let Some(rendered) = &self.rendered_content {
554 if rendered.theme_id != style.theme_id {
555 self.rendered_content = None;
556 }
557 }
558
559 let rendered_content = self.rendered_content.get_or_insert_with(|| {
560 render_blocks(
561 style.theme_id,
562 &self.blocks,
563 self.project.read(cx).languages(),
564 style,
565 )
566 });
567
568 MouseEventHandler::<InfoPopover, _>::new(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| {
594 println!("clicked link {url}");
595 cx.platform().open_url(&url);
596 },
597 ),
598 );
599 }
600 if region.code {
601 scene.push_quad(gpui::Quad {
602 bounds,
603 background: Some(code_span_background_color),
604 border: Default::default(),
605 corner_radius: 2.0,
606 });
607 }
608 },
609 )
610 .with_soft_wrap(true),
611 )
612 .contained()
613 .with_style(style.hover_popover.container)
614 })
615 .on_move(|_, _, _| {}) // Consume move events so they don't reach regions underneath.
616 .with_cursor_style(CursorStyle::Arrow)
617 .with_padding(Padding {
618 bottom: HOVER_POPOVER_GAP,
619 top: HOVER_POPOVER_GAP,
620 ..Default::default()
621 })
622 .into_any()
623 }
624}
625
626#[derive(Debug, Clone)]
627pub struct DiagnosticPopover {
628 local_diagnostic: DiagnosticEntry<Anchor>,
629 primary_diagnostic: Option<DiagnosticEntry<Anchor>>,
630}
631
632impl DiagnosticPopover {
633 pub fn render(&self, style: &EditorStyle, cx: &mut ViewContext<Editor>) -> AnyElement<Editor> {
634 enum PrimaryDiagnostic {}
635
636 let mut text_style = style.hover_popover.prose.clone();
637 text_style.font_size = style.text.font_size;
638 let diagnostic_source_style = style.hover_popover.diagnostic_source_highlight.clone();
639
640 let text = match &self.local_diagnostic.diagnostic.source {
641 Some(source) => Text::new(
642 format!("{source}: {}", self.local_diagnostic.diagnostic.message),
643 text_style,
644 )
645 .with_highlights(vec![(0..source.len(), diagnostic_source_style)]),
646
647 None => Text::new(self.local_diagnostic.diagnostic.message.clone(), text_style),
648 };
649
650 let container_style = match self.local_diagnostic.diagnostic.severity {
651 DiagnosticSeverity::HINT => style.hover_popover.info_container,
652 DiagnosticSeverity::INFORMATION => style.hover_popover.info_container,
653 DiagnosticSeverity::WARNING => style.hover_popover.warning_container,
654 DiagnosticSeverity::ERROR => style.hover_popover.error_container,
655 _ => style.hover_popover.container,
656 };
657
658 let tooltip_style = cx.global::<Settings>().theme.tooltip.clone();
659
660 MouseEventHandler::<DiagnosticPopover, _>::new(0, cx, |_, _| {
661 text.with_soft_wrap(true)
662 .contained()
663 .with_style(container_style)
664 })
665 .with_padding(Padding {
666 top: HOVER_POPOVER_GAP,
667 bottom: HOVER_POPOVER_GAP,
668 ..Default::default()
669 })
670 .on_move(|_, _, _| {}) // Consume move events so they don't reach regions underneath.
671 .on_click(MouseButton::Left, |_, _, cx| {
672 cx.dispatch_action(GoToDiagnostic)
673 })
674 .with_cursor_style(CursorStyle::PointingHand)
675 .with_tooltip::<PrimaryDiagnostic>(
676 0,
677 "Go To Diagnostic".to_string(),
678 Some(Box::new(crate::GoToDiagnostic)),
679 tooltip_style,
680 cx,
681 )
682 .into_any()
683 }
684
685 pub fn activation_info(&self) -> (usize, Anchor) {
686 let entry = self
687 .primary_diagnostic
688 .as_ref()
689 .unwrap_or(&self.local_diagnostic);
690
691 (entry.diagnostic.group_id, entry.range.start.clone())
692 }
693}
694
695#[cfg(test)]
696mod tests {
697 use super::*;
698 use crate::test::editor_lsp_test_context::EditorLspTestContext;
699 use gpui::fonts::Weight;
700 use indoc::indoc;
701 use language::{Diagnostic, DiagnosticSet};
702 use lsp::LanguageServerId;
703 use project::{HoverBlock, HoverBlockKind};
704 use smol::stream::StreamExt;
705 use unindent::Unindent;
706 use util::test::marked_text_ranges;
707
708 #[gpui::test]
709 async fn test_mouse_hover_info_popover(cx: &mut gpui::TestAppContext) {
710 let mut cx = EditorLspTestContext::new_rust(
711 lsp::ServerCapabilities {
712 hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
713 ..Default::default()
714 },
715 cx,
716 )
717 .await;
718
719 // Basic hover delays and then pops without moving the mouse
720 cx.set_state(indoc! {"
721 fn ˇtest() { println!(); }
722 "});
723 let hover_point = cx.display_point(indoc! {"
724 fn test() { printˇln!(); }
725 "});
726
727 cx.update_editor(|editor, cx| hover_at(editor, Some(hover_point), cx));
728 assert!(!cx.editor(|editor, _| editor.hover_state.visible()));
729
730 // After delay, hover should be visible.
731 let symbol_range = cx.lsp_range(indoc! {"
732 fn test() { «println!»(); }
733 "});
734 let mut requests =
735 cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
736 Ok(Some(lsp::Hover {
737 contents: lsp::HoverContents::Markup(lsp::MarkupContent {
738 kind: lsp::MarkupKind::Markdown,
739 value: "some basic docs".to_string(),
740 }),
741 range: Some(symbol_range),
742 }))
743 });
744 cx.foreground()
745 .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
746 requests.next().await;
747
748 cx.editor(|editor, _| {
749 assert!(editor.hover_state.visible());
750 assert_eq!(
751 editor.hover_state.info_popover.clone().unwrap().blocks,
752 vec![HoverBlock {
753 text: "some basic docs".to_string(),
754 kind: HoverBlockKind::Markdown,
755 },]
756 )
757 });
758
759 // Mouse moved with no hover response dismisses
760 let hover_point = cx.display_point(indoc! {"
761 fn teˇst() { println!(); }
762 "});
763 let mut request = cx
764 .lsp
765 .handle_request::<lsp::request::HoverRequest, _, _>(|_, _| async move { Ok(None) });
766 cx.update_editor(|editor, cx| hover_at(editor, Some(hover_point), cx));
767 cx.foreground()
768 .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
769 request.next().await;
770 cx.editor(|editor, _| {
771 assert!(!editor.hover_state.visible());
772 });
773 }
774
775 #[gpui::test]
776 async fn test_keyboard_hover_info_popover(cx: &mut gpui::TestAppContext) {
777 let mut cx = EditorLspTestContext::new_rust(
778 lsp::ServerCapabilities {
779 hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
780 ..Default::default()
781 },
782 cx,
783 )
784 .await;
785
786 // Hover with keyboard has no delay
787 cx.set_state(indoc! {"
788 fˇn test() { println!(); }
789 "});
790 cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
791 let symbol_range = cx.lsp_range(indoc! {"
792 «fn» test() { println!(); }
793 "});
794 cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
795 Ok(Some(lsp::Hover {
796 contents: lsp::HoverContents::Markup(lsp::MarkupContent {
797 kind: lsp::MarkupKind::Markdown,
798 value: "some other basic docs".to_string(),
799 }),
800 range: Some(symbol_range),
801 }))
802 })
803 .next()
804 .await;
805
806 cx.condition(|editor, _| editor.hover_state.visible()).await;
807 cx.editor(|editor, _| {
808 assert_eq!(
809 editor.hover_state.info_popover.clone().unwrap().blocks,
810 vec![HoverBlock {
811 text: "some other basic docs".to_string(),
812 kind: HoverBlockKind::Markdown,
813 }]
814 )
815 });
816 }
817
818 #[gpui::test]
819 async fn test_hover_diagnostic_and_info_popovers(cx: &mut gpui::TestAppContext) {
820 let mut cx = EditorLspTestContext::new_rust(
821 lsp::ServerCapabilities {
822 hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
823 ..Default::default()
824 },
825 cx,
826 )
827 .await;
828
829 // Hover with just diagnostic, pops DiagnosticPopover immediately and then
830 // info popover once request completes
831 cx.set_state(indoc! {"
832 fn teˇst() { println!(); }
833 "});
834
835 // Send diagnostic to client
836 let range = cx.text_anchor_range(indoc! {"
837 fn «test»() { println!(); }
838 "});
839 cx.update_buffer(|buffer, cx| {
840 let snapshot = buffer.text_snapshot();
841 let set = DiagnosticSet::from_sorted_entries(
842 vec![DiagnosticEntry {
843 range,
844 diagnostic: Diagnostic {
845 message: "A test diagnostic message.".to_string(),
846 ..Default::default()
847 },
848 }],
849 &snapshot,
850 );
851 buffer.update_diagnostics(LanguageServerId(0), set, cx);
852 });
853
854 // Hover pops diagnostic immediately
855 cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
856 cx.foreground().run_until_parked();
857
858 cx.editor(|Editor { hover_state, .. }, _| {
859 assert!(hover_state.diagnostic_popover.is_some() && hover_state.info_popover.is_none())
860 });
861
862 // Info Popover shows after request responded to
863 let range = cx.lsp_range(indoc! {"
864 fn «test»() { println!(); }
865 "});
866 cx.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
867 Ok(Some(lsp::Hover {
868 contents: lsp::HoverContents::Markup(lsp::MarkupContent {
869 kind: lsp::MarkupKind::Markdown,
870 value: "some new docs".to_string(),
871 }),
872 range: Some(range),
873 }))
874 });
875 cx.foreground()
876 .advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
877
878 cx.foreground().run_until_parked();
879 cx.editor(|Editor { hover_state, .. }, _| {
880 hover_state.diagnostic_popover.is_some() && hover_state.info_task.is_some()
881 });
882 }
883
884 #[gpui::test]
885 fn test_render_blocks(cx: &mut gpui::TestAppContext) {
886 Settings::test_async(cx);
887 cx.add_window(|cx| {
888 let editor = Editor::single_line(None, cx);
889 let style = editor.style(cx);
890
891 struct Row {
892 blocks: Vec<HoverBlock>,
893 expected_marked_text: String,
894 expected_styles: Vec<HighlightStyle>,
895 }
896
897 let rows = &[
898 // Strong emphasis
899 Row {
900 blocks: vec![HoverBlock {
901 text: "one **two** three".to_string(),
902 kind: HoverBlockKind::Markdown,
903 }],
904 expected_marked_text: "one «two» three\n".to_string(),
905 expected_styles: vec![HighlightStyle {
906 weight: Some(Weight::BOLD),
907 ..Default::default()
908 }],
909 },
910 // Links
911 Row {
912 blocks: vec three".to_string(),
914 kind: HoverBlockKind::Markdown,
915 }],
916 expected_marked_text: "one «two» three\n".to_string(),
917 expected_styles: vec![HighlightStyle {
918 underline: Some(Underline {
919 thickness: 1.0.into(),
920 ..Default::default()
921 }),
922 ..Default::default()
923 }],
924 },
925 // Lists
926 Row {
927 blocks: vec
935 - d
936 "
937 .unindent(),
938 kind: HoverBlockKind::Markdown,
939 }],
940 expected_marked_text: "
941 lists:
942 - one
943 - a
944 - b
945 - two
946 - «c»
947 - d
948 "
949 .unindent(),
950 expected_styles: vec![HighlightStyle {
951 underline: Some(Underline {
952 thickness: 1.0.into(),
953 ..Default::default()
954 }),
955 ..Default::default()
956 }],
957 },
958 // Multi-paragraph list items
959 Row {
960 blocks: vec![HoverBlock {
961 text: "
962 * one two
963 three
964
965 * four five
966 * six seven
967 eight
968
969 nine
970 * ten
971 * six
972 "
973 .unindent(),
974 kind: HoverBlockKind::Markdown,
975 }],
976 expected_marked_text: "
977 - one two three
978 - four five
979 - six seven eight
980
981 nine
982 - ten
983 - six
984 "
985 .unindent(),
986 expected_styles: vec![HighlightStyle {
987 underline: Some(Underline {
988 thickness: 1.0.into(),
989 ..Default::default()
990 }),
991 ..Default::default()
992 }],
993 },
994 ];
995
996 for Row {
997 blocks,
998 expected_marked_text,
999 expected_styles,
1000 } in &rows[0..]
1001 {
1002 let rendered = render_blocks(0, &blocks, &Default::default(), &style);
1003
1004 let (expected_text, ranges) = marked_text_ranges(expected_marked_text, false);
1005 let expected_highlights = ranges
1006 .into_iter()
1007 .zip(expected_styles.iter().cloned())
1008 .collect::<Vec<_>>();
1009 assert_eq!(
1010 rendered.text,
1011 dbg!(expected_text),
1012 "wrong text for input {blocks:?}"
1013 );
1014 assert_eq!(
1015 rendered.highlights, expected_highlights,
1016 "wrong highlights for input {blocks:?}"
1017 );
1018 }
1019
1020 editor
1021 });
1022 }
1023}