2022-06-08 18:09:25 +00:00
|
|
|
use gpui::{
|
|
|
|
actions,
|
|
|
|
elements::{Flex, MouseEventHandler, Padding, Text},
|
|
|
|
impl_internal_actions,
|
|
|
|
platform::CursorStyle,
|
|
|
|
Axis, Element, ElementBox, ModelHandle, MutableAppContext, RenderContext, Task, ViewContext,
|
|
|
|
};
|
|
|
|
use language::Bias;
|
|
|
|
use project::{HoverBlock, Project};
|
2022-06-27 23:21:22 +00:00
|
|
|
use settings::Settings;
|
2022-06-24 22:02:18 +00:00
|
|
|
use std::{ops::Range, time::Duration};
|
2022-06-08 18:09:25 +00:00
|
|
|
use util::TryFutureExt;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
display_map::ToDisplayPoint, Anchor, AnchorRangeExt, DisplayPoint, Editor, EditorSnapshot,
|
|
|
|
EditorStyle,
|
|
|
|
};
|
|
|
|
|
2022-06-10 01:08:23 +00:00
|
|
|
pub const HOVER_DELAY_MILLIS: u64 = 350;
|
|
|
|
pub const HOVER_REQUEST_DELAY_MILLIS: u64 = 200;
|
2022-06-09 17:53:58 +00:00
|
|
|
|
2022-06-08 18:09:25 +00:00
|
|
|
#[derive(Clone, PartialEq)]
|
|
|
|
pub struct HoverAt {
|
|
|
|
pub point: Option<DisplayPoint>,
|
2022-06-08 01:07:24 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 18:09:25 +00:00
|
|
|
actions!(editor, [Hover]);
|
|
|
|
impl_internal_actions!(editor, [HoverAt]);
|
2022-06-08 01:07:24 +00:00
|
|
|
|
2022-06-08 18:09:25 +00:00
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
|
|
|
cx.add_action(hover);
|
|
|
|
cx.add_action(hover_at);
|
|
|
|
}
|
|
|
|
|
2022-07-14 02:35:18 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct HoverState {
|
|
|
|
pub info_popover: Option<InfoPopover>,
|
|
|
|
pub diagnostic_popover: Option<DiagnosticPopover>,
|
|
|
|
pub triggered_from: Option<Anchor>,
|
|
|
|
pub symbol_range: Option<Range<Anchor>>,
|
|
|
|
pub task: Option<Task<Option<()>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HoverState {
|
|
|
|
pub fn visible(&self) -> bool {
|
|
|
|
self.info_popover.is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 18:09:25 +00:00
|
|
|
/// Bindable action which uses the most recent selection head to trigger a hover
|
2022-06-09 17:53:58 +00:00
|
|
|
pub fn hover(editor: &mut Editor, _: &Hover, cx: &mut ViewContext<Editor>) {
|
2022-06-08 18:09:25 +00:00
|
|
|
let head = editor.selections.newest_display(cx).head();
|
|
|
|
show_hover(editor, head, true, cx);
|
|
|
|
}
|
2022-06-08 01:07:24 +00:00
|
|
|
|
2022-06-08 18:09:25 +00:00
|
|
|
/// The internal hover action dispatches between `show_hover` or `hide_hover`
|
|
|
|
/// depending on whether a point to hover over is provided.
|
2022-06-09 17:53:58 +00:00
|
|
|
pub fn hover_at(editor: &mut Editor, action: &HoverAt, cx: &mut ViewContext<Editor>) {
|
2022-06-27 23:21:22 +00:00
|
|
|
if cx.global::<Settings>().hover_popover_enabled {
|
|
|
|
if let Some(point) = action.point {
|
|
|
|
show_hover(editor, point, false, cx);
|
|
|
|
} else {
|
|
|
|
hide_hover(editor, cx);
|
|
|
|
}
|
2022-06-08 01:07:24 +00:00
|
|
|
}
|
2022-06-08 18:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Hides the type information popup.
|
|
|
|
/// Triggered by the `Hover` action when the cursor is not over a symbol or when the
|
|
|
|
/// selections changed.
|
|
|
|
pub fn hide_hover(editor: &mut Editor, cx: &mut ViewContext<Editor>) -> bool {
|
|
|
|
let mut did_hide = false;
|
2022-06-08 01:07:24 +00:00
|
|
|
|
2022-06-08 18:09:25 +00:00
|
|
|
// only notify the context once
|
2022-07-14 02:35:18 +00:00
|
|
|
if editor.hover_state.info_popover.is_some() {
|
|
|
|
editor.hover_state.info_popover = None;
|
2022-06-08 18:09:25 +00:00
|
|
|
did_hide = true;
|
|
|
|
cx.notify();
|
2022-06-08 01:07:24 +00:00
|
|
|
}
|
2022-06-08 18:54:24 +00:00
|
|
|
editor.hover_state.task = None;
|
2022-06-09 21:29:32 +00:00
|
|
|
editor.hover_state.triggered_from = None;
|
2022-06-09 17:53:58 +00:00
|
|
|
editor.hover_state.symbol_range = None;
|
2022-06-08 18:09:25 +00:00
|
|
|
|
|
|
|
editor.clear_background_highlights::<HoverState>(cx);
|
|
|
|
|
|
|
|
did_hide
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Queries the LSP and shows type info and documentation
|
|
|
|
/// about the symbol the mouse is currently hovering over.
|
|
|
|
/// Triggered by the `Hover` action when the cursor may be over a symbol.
|
|
|
|
fn show_hover(
|
|
|
|
editor: &mut Editor,
|
|
|
|
point: DisplayPoint,
|
|
|
|
ignore_timeout: bool,
|
|
|
|
cx: &mut ViewContext<Editor>,
|
|
|
|
) {
|
|
|
|
if editor.pending_rename.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let snapshot = editor.snapshot(cx);
|
|
|
|
let multibuffer_offset = point.to_offset(&snapshot.display_snapshot, Bias::Left);
|
|
|
|
|
|
|
|
let (buffer, buffer_position) = if let Some(output) = editor
|
|
|
|
.buffer
|
|
|
|
.read(cx)
|
|
|
|
.text_anchor_for_position(multibuffer_offset, cx)
|
|
|
|
{
|
|
|
|
output
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let excerpt_id = if let Some((excerpt_id, _, _)) = editor
|
|
|
|
.buffer()
|
|
|
|
.read(cx)
|
|
|
|
.excerpt_containing(multibuffer_offset, cx)
|
|
|
|
{
|
|
|
|
excerpt_id
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let project = if let Some(project) = editor.project.clone() {
|
|
|
|
project
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2022-06-10 01:08:23 +00:00
|
|
|
if !ignore_timeout {
|
2022-06-08 18:54:24 +00:00
|
|
|
if let Some(range) = &editor.hover_state.symbol_range {
|
|
|
|
if range
|
|
|
|
.to_offset(&snapshot.buffer_snapshot)
|
|
|
|
.contains(&multibuffer_offset)
|
|
|
|
{
|
|
|
|
// Hover triggered from same location as last time. Don't show again.
|
|
|
|
return;
|
2022-06-10 01:08:23 +00:00
|
|
|
} else {
|
|
|
|
hide_hover(editor, cx);
|
2022-06-08 18:54:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 18:09:25 +00:00
|
|
|
// Get input anchor
|
|
|
|
let anchor = snapshot
|
|
|
|
.buffer_snapshot
|
|
|
|
.anchor_at(multibuffer_offset, Bias::Left);
|
|
|
|
|
2022-06-09 21:29:32 +00:00
|
|
|
// Don't request again if the location is the same as the previous request
|
|
|
|
if let Some(triggered_from) = &editor.hover_state.triggered_from {
|
|
|
|
if triggered_from
|
|
|
|
.cmp(&anchor, &snapshot.buffer_snapshot)
|
|
|
|
.is_eq()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 18:09:25 +00:00
|
|
|
let task = cx.spawn_weak(|this, mut cx| {
|
|
|
|
async move {
|
2022-06-09 17:53:58 +00:00
|
|
|
// If we need to delay, delay a set amount initially before making the lsp request
|
2022-06-10 01:08:23 +00:00
|
|
|
let delay = if !ignore_timeout {
|
2022-06-09 17:53:58 +00:00
|
|
|
// Construct delay task to wait for later
|
|
|
|
let total_delay = Some(
|
|
|
|
cx.background()
|
|
|
|
.timer(Duration::from_millis(HOVER_DELAY_MILLIS)),
|
|
|
|
);
|
|
|
|
|
|
|
|
cx.background()
|
|
|
|
.timer(Duration::from_millis(HOVER_REQUEST_DELAY_MILLIS))
|
|
|
|
.await;
|
|
|
|
total_delay
|
2022-06-08 18:09:25 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2022-06-09 21:29:32 +00:00
|
|
|
// query the LSP for hover info
|
|
|
|
let hover_request = cx.update(|cx| {
|
|
|
|
project.update(cx, |project, cx| {
|
|
|
|
project.hover(&buffer, buffer_position.clone(), cx)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2022-07-14 02:35:18 +00:00
|
|
|
if let Some(delay) = delay {
|
|
|
|
delay.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's a diagnostic, assign it on the hover state and notify
|
|
|
|
let diagnostic = snapshot
|
|
|
|
.buffer_snapshot
|
|
|
|
.diagnostics_in_range::<_, usize>(multibuffer_offset..multibuffer_offset, false)
|
|
|
|
.next();
|
|
|
|
|
2022-06-08 18:09:25 +00:00
|
|
|
// Construct new hover popover from hover request
|
|
|
|
let hover_popover = hover_request.await.ok().flatten().and_then(|hover_result| {
|
|
|
|
if hover_result.contents.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-06-09 17:53:58 +00:00
|
|
|
// Create symbol range of anchors for highlighting and filtering
|
|
|
|
// of future requests.
|
2022-06-08 18:09:25 +00:00
|
|
|
let range = if let Some(range) = hover_result.range {
|
|
|
|
let start = snapshot
|
|
|
|
.buffer_snapshot
|
|
|
|
.anchor_in_excerpt(excerpt_id.clone(), range.start);
|
|
|
|
let end = snapshot
|
|
|
|
.buffer_snapshot
|
|
|
|
.anchor_in_excerpt(excerpt_id.clone(), range.end);
|
|
|
|
|
|
|
|
start..end
|
|
|
|
} else {
|
|
|
|
anchor.clone()..anchor.clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(this) = this.upgrade(&cx) {
|
|
|
|
this.update(&mut cx, |this, _| {
|
|
|
|
this.hover_state.symbol_range = Some(range.clone());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-14 02:35:18 +00:00
|
|
|
Some(InfoPopover {
|
2022-06-08 18:09:25 +00:00
|
|
|
project: project.clone(),
|
|
|
|
anchor: range.start.clone(),
|
|
|
|
contents: hover_result.contents,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(this) = this.upgrade(&cx) {
|
|
|
|
this.update(&mut cx, |this, cx| {
|
2022-06-09 17:53:58 +00:00
|
|
|
if hover_popover.is_some() {
|
2022-06-08 18:09:25 +00:00
|
|
|
// Highlight the selected symbol using a background highlight
|
|
|
|
if let Some(range) = this.hover_state.symbol_range.clone() {
|
|
|
|
this.highlight_background::<HoverState>(
|
|
|
|
vec![range],
|
|
|
|
|theme| theme.editor.hover_popover.highlight,
|
|
|
|
cx,
|
|
|
|
);
|
|
|
|
}
|
2022-07-14 02:35:18 +00:00
|
|
|
this.hover_state.info_popover = hover_popover;
|
2022-06-08 18:09:25 +00:00
|
|
|
cx.notify();
|
2022-06-09 17:53:58 +00:00
|
|
|
} else {
|
2022-06-09 20:14:25 +00:00
|
|
|
if this.hover_state.visible() {
|
2022-06-09 17:53:58 +00:00
|
|
|
// Popover was visible, but now is hidden. Dismiss it
|
|
|
|
hide_hover(this, cx);
|
|
|
|
} else {
|
|
|
|
// Clear selected symbol range for future requests
|
|
|
|
this.hover_state.symbol_range = None;
|
|
|
|
}
|
2022-06-08 18:54:24 +00:00
|
|
|
}
|
2022-06-08 18:09:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok::<_, anyhow::Error>(())
|
|
|
|
}
|
|
|
|
.log_err()
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.hover_state.task = Some(task);
|
|
|
|
}
|
|
|
|
|
2022-06-09 17:53:58 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2022-07-14 02:35:18 +00:00
|
|
|
pub struct InfoPopover {
|
2022-06-08 01:07:24 +00:00
|
|
|
pub project: ModelHandle<Project>,
|
2022-06-08 18:09:25 +00:00
|
|
|
pub anchor: Anchor,
|
2022-06-08 01:07:24 +00:00
|
|
|
pub contents: Vec<HoverBlock>,
|
|
|
|
}
|
|
|
|
|
2022-07-14 02:35:18 +00:00
|
|
|
impl InfoPopover {
|
2022-06-08 18:09:25 +00:00
|
|
|
pub fn render(
|
2022-06-08 01:07:24 +00:00
|
|
|
&self,
|
2022-06-08 18:09:25 +00:00
|
|
|
snapshot: &EditorSnapshot,
|
2022-06-08 01:07:24 +00:00
|
|
|
style: EditorStyle,
|
|
|
|
cx: &mut RenderContext<Editor>,
|
|
|
|
) -> (DisplayPoint, ElementBox) {
|
2022-07-14 02:35:18 +00:00
|
|
|
let element = MouseEventHandler::new::<InfoPopover, _, _>(0, cx, |_, cx| {
|
2022-06-08 01:07:24 +00:00
|
|
|
let mut flex = Flex::new(Axis::Vertical).scrollable::<HoverBlock, _>(1, None, cx);
|
|
|
|
flex.extend(self.contents.iter().map(|content| {
|
|
|
|
let project = self.project.read(cx);
|
|
|
|
if let Some(language) = content
|
|
|
|
.language
|
|
|
|
.clone()
|
|
|
|
.and_then(|language| project.languages().get_language(&language))
|
|
|
|
{
|
|
|
|
let runs = language
|
|
|
|
.highlight_text(&content.text.as_str().into(), 0..content.text.len());
|
|
|
|
|
|
|
|
Text::new(content.text.clone(), style.text.clone())
|
|
|
|
.with_soft_wrap(true)
|
|
|
|
.with_highlights(
|
|
|
|
runs.iter()
|
|
|
|
.filter_map(|(range, id)| {
|
|
|
|
id.style(style.theme.syntax.as_ref())
|
|
|
|
.map(|style| (range.clone(), style))
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
.boxed()
|
|
|
|
} else {
|
2022-06-08 18:09:25 +00:00
|
|
|
let mut text_style = style.hover_popover.prose.clone();
|
|
|
|
text_style.font_size = style.text.font_size;
|
|
|
|
|
|
|
|
Text::new(content.text.clone(), text_style)
|
2022-06-08 01:07:24 +00:00
|
|
|
.with_soft_wrap(true)
|
|
|
|
.contained()
|
|
|
|
.with_style(style.hover_popover.block_style)
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
flex.contained()
|
|
|
|
.with_style(style.hover_popover.container)
|
|
|
|
.boxed()
|
|
|
|
})
|
|
|
|
.with_cursor_style(CursorStyle::Arrow)
|
|
|
|
.with_padding(Padding {
|
|
|
|
bottom: 5.,
|
|
|
|
top: 5.,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.boxed();
|
|
|
|
|
2022-06-08 18:09:25 +00:00
|
|
|
let display_point = self.anchor.to_display_point(&snapshot.display_snapshot);
|
|
|
|
(display_point, element)
|
2022-06-08 01:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-10 01:08:23 +00:00
|
|
|
|
2022-07-14 02:35:18 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct DiagnosticPopover {}
|
|
|
|
|
2022-06-10 01:08:23 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use futures::StreamExt;
|
|
|
|
use indoc::indoc;
|
|
|
|
|
|
|
|
use project::HoverBlock;
|
|
|
|
|
|
|
|
use crate::test::EditorLspTestContext;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_hover_popover(cx: &mut gpui::TestAppContext) {
|
|
|
|
let mut cx = EditorLspTestContext::new_rust(
|
|
|
|
lsp::ServerCapabilities {
|
|
|
|
hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
cx,
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// Basic hover delays and then pops without moving the mouse
|
|
|
|
cx.set_state(indoc! {"
|
|
|
|
fn |test()
|
|
|
|
println!();"});
|
|
|
|
let hover_point = cx.display_point(indoc! {"
|
|
|
|
fn test()
|
|
|
|
print|ln!();"});
|
|
|
|
|
|
|
|
cx.update_editor(|editor, cx| {
|
|
|
|
hover_at(
|
|
|
|
editor,
|
|
|
|
&HoverAt {
|
|
|
|
point: Some(hover_point),
|
|
|
|
},
|
|
|
|
cx,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
assert!(!cx.editor(|editor, _| editor.hover_state.visible()));
|
|
|
|
|
|
|
|
// After delay, hover should be visible.
|
|
|
|
let symbol_range = cx.lsp_range(indoc! {"
|
|
|
|
fn test()
|
|
|
|
[println!]();"});
|
|
|
|
let mut requests =
|
|
|
|
cx.lsp
|
|
|
|
.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _| async move {
|
|
|
|
Ok(Some(lsp::Hover {
|
|
|
|
contents: lsp::HoverContents::Markup(lsp::MarkupContent {
|
|
|
|
kind: lsp::MarkupKind::Markdown,
|
|
|
|
value: indoc! {"
|
|
|
|
# Some basic docs
|
|
|
|
Some test documentation"}
|
|
|
|
.to_string(),
|
|
|
|
}),
|
|
|
|
range: Some(symbol_range),
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
cx.foreground()
|
|
|
|
.advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
|
|
|
|
requests.next().await;
|
|
|
|
|
|
|
|
cx.editor(|editor, _| {
|
|
|
|
assert!(editor.hover_state.visible());
|
|
|
|
assert_eq!(
|
2022-07-14 02:35:18 +00:00
|
|
|
editor.hover_state.info_popover.clone().unwrap().contents,
|
2022-06-10 01:08:23 +00:00
|
|
|
vec![
|
|
|
|
HoverBlock {
|
|
|
|
text: "Some basic docs".to_string(),
|
|
|
|
language: None
|
|
|
|
},
|
|
|
|
HoverBlock {
|
|
|
|
text: "Some test documentation".to_string(),
|
|
|
|
language: None
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mouse moved with no hover response dismisses
|
|
|
|
let hover_point = cx.display_point(indoc! {"
|
|
|
|
fn te|st()
|
|
|
|
println!();"});
|
|
|
|
cx.update_editor(|editor, cx| {
|
|
|
|
hover_at(
|
|
|
|
editor,
|
|
|
|
&HoverAt {
|
|
|
|
point: Some(hover_point),
|
|
|
|
},
|
|
|
|
cx,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
let mut request = cx
|
|
|
|
.lsp
|
|
|
|
.handle_request::<lsp::request::HoverRequest, _, _>(|_, _| async move { Ok(None) });
|
|
|
|
cx.foreground()
|
|
|
|
.advance_clock(Duration::from_millis(HOVER_DELAY_MILLIS + 100));
|
|
|
|
request.next().await;
|
|
|
|
cx.editor(|editor, _| {
|
|
|
|
assert!(!editor.hover_state.visible());
|
|
|
|
});
|
|
|
|
|
|
|
|
// Hover with keyboard has no delay
|
|
|
|
cx.set_state(indoc! {"
|
|
|
|
f|n test()
|
|
|
|
println!();"});
|
|
|
|
cx.update_editor(|editor, cx| hover(editor, &Hover, cx));
|
|
|
|
let symbol_range = cx.lsp_range(indoc! {"
|
|
|
|
[fn] test()
|
|
|
|
println!();"});
|
|
|
|
cx.lsp
|
|
|
|
.handle_request::<lsp::request::HoverRequest, _, _>(move |_, _| async move {
|
|
|
|
Ok(Some(lsp::Hover {
|
|
|
|
contents: lsp::HoverContents::Markup(lsp::MarkupContent {
|
|
|
|
kind: lsp::MarkupKind::Markdown,
|
|
|
|
value: indoc! {"
|
|
|
|
# Some other basic docs
|
|
|
|
Some other test documentation"}
|
|
|
|
.to_string(),
|
|
|
|
}),
|
|
|
|
range: Some(symbol_range),
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
.next()
|
|
|
|
.await;
|
|
|
|
cx.foreground().run_until_parked();
|
|
|
|
cx.editor(|editor, _| {
|
|
|
|
assert!(editor.hover_state.visible());
|
|
|
|
assert_eq!(
|
2022-07-14 02:35:18 +00:00
|
|
|
editor.hover_state.info_popover.clone().unwrap().contents,
|
2022-06-10 01:08:23 +00:00
|
|
|
vec![
|
|
|
|
HoverBlock {
|
|
|
|
text: "Some other basic docs".to_string(),
|
|
|
|
language: None
|
|
|
|
},
|
|
|
|
HoverBlock {
|
|
|
|
text: "Some other test documentation".to_string(),
|
|
|
|
language: None
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|