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 doc_lens = |title: &str, args: Vec<JsonValue>| CodeLens {
25            range: doc_start,
26            command: Some(Command {
27                title: title.to_string(),
28                command: "tinymist.runCodeLens".to_string(),
29                arguments: Some(args),
30            }),
31            data: None,
32        };
33
34        res.push(doc_lens(
35            &tinymist_l10n::t!("tinymist-query.code-action.profile", "Profile"),
36            vec!["profile".into()],
37        ));
38        res.push(doc_lens(
39            &tinymist_l10n::t!("tinymist-query.code-action.preview", "Preview"),
40            vec!["preview".into()],
41        ));
42
43        let is_html = ctx.world.library.features.is_enabled(typst::Feature::Html);
44
45        if is_html {
46            res.push(doc_lens(
47                &tinymist_l10n::t!("tinymist-query.code-action.exportHtml", "Export HTML"),
48                vec!["export-html".into()],
49            ));
50        } else {
51            res.push(doc_lens(
52                &tinymist_l10n::t!("tinymist-query.code-action.exportPdf", "Export PDF"),
53                vec!["export-pdf".into()],
54            ));
55        }
56
57        res.push(doc_lens(
58            &tinymist_l10n::t!("tinymist-query.code-action.more", "More .."),
59            vec!["more".into()],
60        ));
61
62        Some(res)
63    }
64}