tinymist_query/
code_lens.rs

1use lsp_types::Command;
2
3use crate::{SemanticRequest, prelude::*};
4
5/// The [`textDocument/codeLens`] request is sent from the client to the server
6/// to compute code lenses for a given text document.
7///
8/// [`textDocument/codeLens`]: https://microsoft.github.io/language-server-protocol/specification#textDocument_codeLens
9#[derive(Debug, Clone)]
10pub struct CodeLensRequest {
11    /// The path of the document to request for.
12    pub path: PathBuf,
13}
14
15impl SemanticRequest for CodeLensRequest {
16    type Response = Vec<CodeLens>;
17
18    fn request(self, ctx: &mut LocalContext) -> Option<Self::Response> {
19        let source = ctx.source_by_path(&self.path).ok()?;
20
21        let mut res = vec![];
22
23        let doc_start = ctx.to_lsp_range(0..0, &source);
24        let mut doc_lens = |title: &str, args: Vec<JsonValue>| {
25            if !ctx.analysis.support_client_codelens {
26                return;
27            }
28            res.push(CodeLens {
29                range: doc_start,
30                command: Some(Command {
31                    title: title.to_string(),
32                    command: "tinymist.runCodeLens".to_string(),
33                    arguments: Some(args),
34                }),
35                data: None,
36            })
37        };
38
39        doc_lens(
40            &tinymist_l10n::t!("tinymist-query.code-action.profile", "Profile"),
41            vec!["profile".into()],
42        );
43        doc_lens(
44            &tinymist_l10n::t!("tinymist-query.code-action.preview", "Preview"),
45            vec!["preview".into()],
46        );
47
48        let is_html = ctx
49            .world()
50            .library
51            .features
52            .is_enabled(typst::Feature::Html);
53
54        doc_lens(
55            &tinymist_l10n::t!("tinymist-query.code-action.export", "Export"),
56            vec!["export".into()],
57        );
58        if is_html {
59            doc_lens("HTML", vec!["export-html".into()]);
60        } else {
61            doc_lens("PDF", vec!["export-pdf".into()]);
62        }
63
64        doc_lens(
65            &tinymist_l10n::t!("tinymist-query.code-action.more", "More .."),
66            vec!["more".into()],
67        );
68
69        Some(res)
70    }
71}