sync_lsp/text_document/
hover.rs

1//! implementation of the hover request
2//! 
3//! # Usage
4//! Additional information about a specific symbol in the document can be requested via [`Server::on_hover`].
5//! by the client.
6
7use crate::TypeProvider;
8use crate::{Server, connection::Endpoint};
9use crate::connection::Callback;
10use serde::Serialize;
11use super::{TextDocumentIdentifer, TextDocumentPositionParams, Range, Position};
12
13#[derive(Default, Clone)]
14pub(crate) struct HoverOptions;
15
16/// A hover represents additional information for a symbol.
17#[derive(Serialize, Debug, Default)]
18pub struct Hover {
19    /// The information to display.
20    pub contents: Vec<MarkedString>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    /// This is used to highlight the specific range in the editor.
23    pub range: Option<Range>
24}
25
26/// This may either be a markdown string or a language snippet.
27#[derive(Serialize, Debug)]
28#[serde(untagged)]
29pub enum MarkedString {
30    /// A markdown string.
31    String(String),
32    /// A language snippet.
33    LanguageString {
34        language: String,
35        value: String
36    }
37}
38
39impl HoverOptions {
40
41    pub(crate) const METHOD: &'static str = "textDocument/hover";
42    
43    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, HoverOptions> {
44        Endpoint::new(Callback::request(|_, _: TextDocumentPositionParams| Hover::default()))
45    }
46}
47
48impl<T: TypeProvider> Server<T> {
49
50    /// Sets the callback that will be called to [compute hover information](self).
51    /// 
52    /// # Argument
53    /// * `callback` - A callback which is called with the following parameters as soon as hover information is requested:
54    ///     * The server instance receiving the response.
55    ///     * The [`TextDocumentIdentifer`] of the target document.
56    ///     * The [`Position`] of the cursor.
57    ///     * `return` - The hover information to display.
58
59    pub fn on_hover(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, Position) -> Hover) {
60        self.text_document.hover.set_callback(Callback::request(move |server, params: TextDocumentPositionParams| {
61            callback(server, params.text_document, params.position)
62        }))
63    }
64}