summaryrefslogtreecommitdiff
path: root/support/texlab/src/link/latex_include.rs
blob: 5f4f592f14c726fabc980d1e4ba7292890c52225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use crate::{
    feature::{FeatureProvider, FeatureRequest},
    protocol::{DocumentLink, DocumentLinkParams},
    syntax::{latex, SyntaxNode},
    workspace::DocumentContent,
};
use async_trait::async_trait;

#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct LatexIncludeLinkProvider;

#[async_trait]
impl FeatureProvider for LatexIncludeLinkProvider {
    type Params = DocumentLinkParams;
    type Output = Vec<DocumentLink>;

    async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
        if let DocumentContent::Latex(table) = &req.current().content {
            table
                .includes
                .iter()
                .flat_map(|include| Self::resolve(req, table, include))
                .collect()
        } else {
            Vec::new()
        }
    }
}

impl LatexIncludeLinkProvider {
    fn resolve(
        req: &FeatureRequest<DocumentLinkParams>,
        table: &latex::SymbolTable,
        include: &latex::Include,
    ) -> Vec<DocumentLink> {
        let mut links = Vec::new();
        let paths = include.paths(&table);
        for (i, targets) in include.all_targets.iter().enumerate() {
            for target in targets {
                if let Some(link) = req.snapshot().find(target).map(|doc| DocumentLink {
                    range: paths[i].range(),
                    target: doc.uri.clone().into(),
                    tooltip: None,
                }) {
                    links.push(link);
                    break;
                }
            }
        }
        links
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        feature::FeatureTester,
        protocol::{Range, RangeExt},
    };

    #[tokio::test]
    async fn empty_latex_document_command() {
        let actual_links = FeatureTester::new()
            .file("main.tex", "")
            .main("main.tex")
            .test_link(LatexIncludeLinkProvider)
            .await;

        assert!(actual_links.is_empty());
    }

    #[tokio::test]
    async fn empty_bibtex_document_command() {
        let actual_links = FeatureTester::new()
            .file("main.bib", "")
            .main("main.bib")
            .test_link(LatexIncludeLinkProvider)
            .await;

        assert!(actual_links.is_empty());
    }

    #[tokio::test]
    async fn has_links() {
        let actual_links = FeatureTester::new()
            .file("foo.tex", r#"\input{bar.tex}"#)
            .file("bar.tex", r#""#)
            .main("foo.tex")
            .test_link(LatexIncludeLinkProvider)
            .await;

        let expected_links = vec![DocumentLink {
            range: Range::new_simple(0, 7, 0, 14),
            target: FeatureTester::uri("bar.tex").into(),
            tooltip: None,
        }];

        assert_eq!(actual_links, expected_links);
    }
}