sync_lsp/text_document/
did_open.rs

1//! implementation of the `textDocument/didOpen` notification
2//! 
3//! # Usage
4//! Whenever a document is opened, [`Server::on_open`] is invoked.
5
6use crate::{Server, TypeProvider};
7use crate::connection::{Callback, Endpoint};
8use serde::Deserialize;
9use super::DocumentUri;
10
11#[derive(Default, Clone)]
12pub(crate) struct DidOpenOptions;
13
14/// A document that has been opened.
15#[derive(Deserialize, Debug)]
16#[serde(rename_all = "camelCase")]
17pub struct TextDocumentItem {
18    /// The text document's URI.
19    pub uri: DocumentUri,
20    /// A language identifier.
21    pub language_id: String,
22    /// The initial version number of the document.
23    pub version: i32,
24    /// The entire content of the document.
25    pub text: String,
26}
27
28#[derive(Deserialize)]
29#[serde(rename_all = "camelCase")]
30struct DidOpenParams {
31    text_document: TextDocumentItem,
32}
33
34impl DidOpenOptions {
35
36    pub(crate) const METHOD: &'static str = "textDocument/didOpen";
37    
38    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, DidOpenOptions> {
39        Endpoint::new(Callback::notification(|_, _: DidOpenParams| ()))
40    }
41}
42
43impl<T: TypeProvider> Server<T> {
44
45    /// Sets the callback that will be called if a [file is opened](self).
46    /// 
47    /// # Argument
48    /// * `callback` - A callback which is called with the following parameters as soon as a file is opened:
49    ///     * The server instance receiving the response.
50    ///     * The [`TextDocumentItem`] of the document that has been opened.
51    ///
52
53    pub fn on_open(&mut self, callback: fn(&mut Server<T>, TextDocumentItem)) {
54        self.text_document.did_open.set_callback(Callback::notification(move |server, params: DidOpenParams| {
55            callback(server, params.text_document)
56        }))
57    }
58}