sync_lsp/text_document/
signature_help.rs

1//! implementation of the `textDocument/signatureHelp` request
2//! 
3//! # Usage
4//! A client can request the signature of a item using [`Server::on_signature_help`].
5//! Additionally, the specific characters that trigger signature help can be set via
6//! [`Server::set_signature_help_trigger_characters`].
7
8use crate::TypeProvider;
9use crate::{Server, connection::Endpoint};
10use crate::connection::Callback;
11use serde::Serialize;
12use super::{TextDocumentIdentifer, TextDocumentPositionParams, Position};
13
14#[derive(Serialize, Default, Clone)]
15#[serde(rename_all = "camelCase")]
16pub(crate) struct SignatureHelpOptions {
17    trigger_characters: Vec<String>
18}
19
20/// The signature of some item.
21#[derive(Serialize, Debug, Default)]
22pub struct SignatureHelp {
23    /// One or more signatures.
24    pub signatures: Vec<SignatureInformation>,
25    /// The index of the active signature.
26    pub active_signature: Option<u32>,
27    /// The index of the active parameter.
28    pub active_parameter: Option<u32>
29}
30
31/// Represents the signature of some item.
32#[derive(Serialize, Debug)]
33pub struct SignatureInformation {
34    /// A string representing the name of the item.
35    pub label: String,
36    /// Documentation of the item.
37    pub documentation: Option<String>,
38    /// The parameters of this signature.
39    #[serde(skip_serializing_if = "Vec::is_empty")]
40    pub parameters: Vec<ParameterInformation>
41}
42
43/// Represents a parameter of a callable-signature.
44#[derive(Serialize, Debug)]
45pub struct ParameterInformation {
46    /// The label of this parameter.
47    pub label: String,
48    /// The markdown documentation of this parameter.
49    pub documentation: Option<String>
50}
51
52impl SignatureHelpOptions {
53
54    pub(crate) const METHOD: &'static str = "textDocument/signatureHelp";
55    
56    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, SignatureHelpOptions> {
57        Endpoint::new(Callback::request(|_, _: TextDocumentPositionParams| SignatureHelp::default()))
58    }
59}
60
61impl<T: TypeProvider> Server<T> {
62
63    /// Sets the callback that will be called to [compute signature help](self).
64    /// 
65    /// # Argument
66    /// * `callback` - A callback which is called with the following parameters as soon as signature help is requested:
67    ///     * The server instance receiving the response.
68    ///     * The [`TextDocumentIdentifer`] of the target document.
69    ///     * The [`Position`] of the cursor.
70    ///     * `return` - The signature help to display.
71
72    pub fn on_signature_help(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, Position) -> SignatureHelp) {
73        self.text_document.signature_help.set_callback(Callback::request(move |server, params: TextDocumentPositionParams| {
74            callback(server, params.text_document, params.position)
75        }))
76    }
77
78    /// Sets the characters that trigger [signature help](self).
79    /// 
80    /// # Argument
81    /// * `value` - The characters that trigger signature help.
82
83    pub fn set_signature_help_trigger_characters(&mut self, value: Vec<String>) {
84        self.text_document.signature_help.options_mut().trigger_characters = value;
85    }
86}