sync_lsp/text_document/
code_lens.rs

1//! implementation of the `textDocument/codeLens` request
2//! 
3//! # Usage
4//! [`Server::on_code_lens`] is called to compute commands, which are
5//! commonly displayed in the user interface as some kind of button
6//! in the source code. Optionally a command can be attached later on
7//! via [`Server::on_resolve_code_lens`].
8
9use crate::TypeProvider;
10use crate::{Server, connection::Endpoint};
11use crate::connection::Callback;
12use serde::{Deserialize, Serialize};
13use super::{TextDocumentIdentifer, Range};
14use crate::workspace::execute_command::{Command, serialize_opt_command, deserialize_opt_command};
15
16#[derive(Serialize, Clone)]
17#[serde(rename_all = "camelCase")]
18pub(crate) struct CodeLensOptions {
19    resolve_provider: bool
20}
21
22#[derive(Clone, Default)]
23pub(crate) struct CodeLensResolveOptions;
24
25#[derive(Deserialize)]
26#[serde(rename_all = "camelCase")]
27struct CodeLensParams  {
28    text_document: TextDocumentIdentifer,
29}
30
31/// A code lens represents a command.
32#[derive(Serialize, Deserialize, Debug)]
33pub struct CodeLens<C: Command, V> {
34    /// The range in which this code lens is valid. Should only span a single line.
35    pub range: Range,
36    /// A server command as defined in [`TypeProvider::Command`]
37    #[serde(default = "Option::default")]
38    #[serde(skip_serializing_if = "Option::is_none")]
39    #[serde(serialize_with = "serialize_opt_command")]
40    #[serde(deserialize_with = "deserialize_opt_command")]
41    pub command: Option<C>,
42    /// Arbitrary data as defined in [`TypeProvider::CodeLensData`]
43    /// used to identify this code lens when [`Server::on_resolve_code_lens`] is invoked.
44    pub data: V,
45}
46
47impl CodeLensOptions {
48    pub(crate) const METHOD: &'static str = "textDocument/codeLens";
49    
50    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, CodeLensOptions> {
51        Endpoint::new(Callback::request(|_, _: CodeLensParams| {
52            Vec::<CodeLens<T::Command, T::CodeLensData>>::new()
53        }))
54    }
55}
56
57impl CodeLensResolveOptions {
58    pub(crate) const METHOD: &'static str = "codeLens/resolve";
59        
60    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, CodeLensResolveOptions> {
61        Endpoint::new(Callback::request(|_, lens: CodeLens<T::Command, T::CodeLensData>| lens))
62    }
63}
64
65impl<T: TypeProvider> Server<T> {
66
67    /// Sets the callback that will be called to [compute code lenses](self).
68    /// 
69    /// # Argument
70    /// * `callback` - A callback which is called with the following parameters as soon as code lenses are requested:
71    ///     * The server instance receiving the response.
72    ///     * The [`TextDocumentIdentifer`] of the document for which code actions are requested.
73    ///     * `return` - A list of code lenses to display.
74
75    pub fn on_code_lens(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer) -> Vec<CodeLens<T::Command, T::CodeLensData>>) {
76        self.text_document.code_lens.set_callback(Callback::request(move |server, params: CodeLensParams| {
77            callback(server, params.text_document)
78        }));
79    }
80
81    /// Sets the callback that will be called to [compute commands of code lenses](self), which were previously returned by [`Server::on_code_lens`].
82    /// 
83    /// # Argument
84    /// * `callback` - A callback which is called with the following parameters as soon a code lens can be resolved:
85    ///     * The server instance receiving the response.
86    ///     * The [`CodeLens`] to resolve with `command` set to `None`.
87    ///    * `return` - The resolved code lens.
88
89    pub fn on_resolve_code_lens(&mut self, callback: fn(&mut Server<T>, CodeLens<T::Command, T::CodeLensData>) -> CodeLens<T::Command, T::CodeLensData>) {
90        self.text_document.resolve_code_lens.set_callback(Callback::request(move |server, params| {
91            callback(server, params)
92        }));
93    }
94}
95
96impl Default for CodeLensOptions {
97    fn default() -> Self {
98        CodeLensOptions {
99            resolve_provider: true
100        }
101    }
102}