summaryrefslogtreecommitdiff
path: root/support/texlab/src/link/latex_include.rs
blob: 0cc6c36364549e277988098d1dfdbf40a90abd94 (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
102
103
104
105
106
107
108
109
110
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::{DocumentLink, DocumentLinkParams};

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

impl FeatureProvider for LatexIncludeLinkProvider {
    type Params = DocumentLinkParams;
    type Output = Vec<DocumentLink>;

    #[boxed]
    async fn execute<'a>(
        &'a self,
        request: &'a FeatureRequest<DocumentLinkParams>,
    ) -> Vec<DocumentLink> {
        if let SyntaxTree::Latex(tree) = &request.document().tree {
            return tree
                .includes
                .iter()
                .flat_map(|include| Self::resolve(request, include))
                .collect();
        }
        Vec::new()
    }
}

impl LatexIncludeLinkProvider {
    fn resolve(
        request: &FeatureRequest<DocumentLinkParams>,
        include: &LatexInclude,
    ) -> Vec<DocumentLink> {
        let mut links = Vec::new();
        let paths = include.paths();
        for (i, targets) in include.all_targets.iter().enumerate() {
            for target in targets {
                if let Some(link) = request
                    .workspace()
                    .find(target)
                    .map(|document| DocumentLink {
                        range: paths[i].range(),
                        target: document.uri.clone().into(),
                    })
                {
                    links.push(link);
                }
            }
        }
        links
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::range::RangeExt;
    use lsp_types::{Position, Range};

    #[test]
    fn test_has_links() {
        let links = test_feature(
            LatexIncludeLinkProvider,
            FeatureSpec {
                files: vec![
                    FeatureSpec::file("foo.tex", "\\input{bar.tex}"),
                    FeatureSpec::file("bar.tex", ""),
                ],
                main_file: "foo.tex",
                position: Position::new(0, 15),
                ..FeatureSpec::default()
            },
        );
        assert_eq!(
            links,
            vec![DocumentLink {
                range: Range::new_simple(0, 7, 0, 14),
                target: FeatureSpec::uri("bar.tex"),
            }]
        );
    }

    #[test]
    fn test_no_links_latex() {
        let links = test_feature(
            LatexIncludeLinkProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file("foo.tex", "")],
                main_file: "foo.tex",
                position: Position::new(0, 15),
                ..FeatureSpec::default()
            },
        );
        assert!(links.is_empty());
    }

    #[test]
    fn test_no_links_bibtex() {
        let links = test_feature(
            LatexIncludeLinkProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file("foo.bib", "")],
                main_file: "foo.bib",
                position: Position::new(0, 15),
                ..FeatureSpec::default()
            },
        );
        assert!(links.is_empty());
    }
}