sync_lsp/text_document/
code_lens.rs1use 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#[derive(Serialize, Deserialize, Debug)]
33pub struct CodeLens<C: Command, V> {
34 pub range: Range,
36 #[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 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 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 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}