sync_lsp/text_document/
definition.rs

1//! implementation of the `textDocument/definition` request
2//! 
3//! # Usage
4//! [`Server::on_definition`] is invoked, to compute the definition
5//! of a symbol at a given cursor position.
6
7use crate::TypeProvider;
8use crate::{Server, connection::Endpoint};
9use crate::connection::Callback;
10use super::{TextDocumentIdentifer, TextDocumentPositionParams, Location, Position};
11
12#[derive(Default, Clone)]
13pub(crate) struct DefinitionOptions;
14
15impl DefinitionOptions {
16
17    pub(crate) const METHOD: &'static str = "textDocument/definition";
18    
19    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, DefinitionOptions> {
20        Endpoint::new(Callback::request(|_, _: TextDocumentPositionParams| Vec::<Location>::new()))
21    }
22}
23
24impl<T: TypeProvider> Server<T> {
25
26    /// Sets the callback that will be called to locate a  [definition](self).
27    /// 
28    /// # Argument
29    /// * `callback` - A callback which is called with the following parameters to resolve a definition:
30    ///     * The server instance receiving the response.
31    ///     * The [`TextDocumentIdentifer`] of the document for which a definition is requested.
32    ///    * The [`Position`] at which a definition is requested.
33    ///     * `return` - A list of [`Location`]s to display.
34
35    pub fn on_definition(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, Position) -> Vec<Location>) {
36        self.text_document.definition.set_callback(Callback::request(move |server, params: TextDocumentPositionParams | {
37            callback(server, params.text_document, params.position)
38        }))
39    }
40}