sync_lsp/text_document/
references.rs

1//! implementation of the `textDocument/references` request
2//! 
3//! # Usage
4//! A client can resolve references to a symbol via [`Server::on_references`], which could
5//! be used to implement "Find all references" functionality.
6
7use crate::TypeProvider;
8use crate::{Server, connection::Endpoint};
9use crate::connection::Callback;
10use super::{TextDocumentIdentifer, Position, Location};
11use serde::Deserialize;
12
13#[derive(Default, Clone)]
14pub(crate) struct ReferenceOptions;
15
16#[derive(Deserialize, Debug)]
17#[serde(rename_all = "camelCase")]
18struct ReferenceParams {
19    text_document: TextDocumentIdentifer,
20    position: Position,
21    context: ReferenceContext
22}
23
24/// Additional information about which references should be returned.
25#[derive(Deserialize, Debug)]
26#[serde(rename_all = "camelCase")]
27pub struct ReferenceContext {
28    /// Include the declaration of the current symbol.
29    pub include_declaration: bool
30}
31
32impl ReferenceOptions {
33
34    pub(crate) const METHOD: &'static str = "textDocument/references";
35    
36    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, ReferenceOptions> {
37        Endpoint::new(Callback::request(|_, _: ReferenceParams| Vec::<Location>::new()))
38    }
39}
40
41impl<T: TypeProvider> Server<T> {
42
43    /// Sets the callback that will be called to [resolve references](self).
44    /// 
45    /// # Argument
46    /// * `callback` - A callback which is called with the following parameters as soon as references are requested:
47    ///    * The server instance receiving the response.
48    ///    * The [`TextDocumentIdentifer`] of the target document.
49    ///    * The [`Position`] of the cursor.
50    ///    * The [`ReferenceContext`] that specifies which references should be returned.
51    ///    * `return` - A list of locations that reference the symbol at the given position.
52
53    pub fn on_references(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, Position, context: ReferenceContext) -> Vec<Location>) {
54        self.text_document.references.set_callback(Callback::request(move |server, params: ReferenceParams| {
55            callback(server, params.text_document, params.position, params.context)
56        }))
57    }
58}