tinymist_query/
document_link.rs

1use crate::{SemanticRequest, analysis::get_link_exprs, prelude::*};
2
3/// The [`textDocument/documentLink`] request is sent from the client to the
4/// server to request the location of links in a document.
5///
6/// A document link is a range in a text document that links to an internal or
7/// external resource, like another text document or a web site.
8///
9/// [`textDocument/documentLink`]: https://microsoft.github.io/language-server-protocol/specification#textDocument_documentLink
10///
11/// # Compatibility
12///
13/// The [`DocumentLink::tooltip`] field was introduced in specification version
14/// 3.15.0 and requires client-side support in order to be used.
15#[derive(Debug, Clone)]
16pub struct DocumentLinkRequest {
17    /// The path of the document to request color for.
18    pub path: PathBuf,
19}
20
21impl SemanticRequest for DocumentLinkRequest {
22    type Response = Vec<DocumentLink>;
23
24    fn request(self, ctx: &mut LocalContext) -> Option<Self::Response> {
25        let source = ctx.source_by_path(&self.path).ok()?;
26        let links = get_link_exprs(&source);
27        if links.objects.is_empty() {
28            return None;
29        }
30
31        let links = links.objects.iter().map(|obj| DocumentLink {
32            range: ctx.to_lsp_range(obj.range.clone(), &source),
33            target: obj.target.resolve(ctx),
34            tooltip: None,
35            data: None,
36        });
37        Some(links.collect())
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use crate::tests::*;
45
46    #[test]
47    fn test() {
48        snapshot_testing("document_link", &|ctx, path| {
49            let request = DocumentLinkRequest { path: path.clone() };
50
51            let result = request.request(ctx);
52            assert_snapshot!(JsonRepr::new_redacted(result, &REDACT_LOC));
53        });
54    }
55}