sync_lsp/text_document/
document_symbol.rs

1//! implementation of the `textDocument/documentSymbol` request
2//! 
3//! # Usage
4//! The client can request a list of symbols from the server via [`Server::on_document_symbol`]
5//! at any time. This is useful for the user to navigate to a specific symbol in a file.
6
7use crate::TypeProvider;
8use crate::workspace::symbol::SymbolInformation;
9use crate::{Server, connection::Endpoint};
10use crate::connection::Callback;
11use super::TextDocumentIdentifer;
12use serde::Deserialize;
13
14#[derive(Default, Clone)]
15pub struct DocumentSymbolOptions;
16
17#[derive(Deserialize, Debug)]
18#[serde(rename_all = "camelCase")]
19struct DocumentSymbolParams {
20    text_document: TextDocumentIdentifer,
21}
22
23impl DocumentSymbolOptions {
24
25    pub(crate) const METHOD: &'static str = "textDocument/documentSymbol";
26    
27    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, DocumentSymbolOptions> {
28        Endpoint::new(Callback::request(|_, _: DocumentSymbolParams| Vec::<SymbolInformation>::new()))
29    }
30}
31
32impl<T: TypeProvider> Server<T> {
33
34    /// Sets the callback that will be called to [compute document symbols](self).
35    /// 
36    /// # Argument
37    /// * `callback` - A callback which is called with the following parameters as soon as document symbols are requested:
38    ///     * The server instance receiving the response.
39    ///     * The [`TextDocumentIdentifer`] of the target document.
40    ///     * `return` - A list of symbols to display.
41
42    pub fn on_document_symbol(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer) -> Vec<SymbolInformation>) {
43        self.text_document.document_symbol.set_callback(Callback::request(move |server, params: DocumentSymbolParams| {
44            callback(server, params.text_document)
45        }))
46    }
47}