summaryrefslogtreecommitdiff
path: root/support/texlab/src/definition/latex_citation.rs
blob: aa93e25fc0033b4240ecf24cf2fbfab1deb67d20 (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
111
112
113
114
115
116
117
118
use crate::{
    feature::{FeatureProvider, FeatureRequest},
    protocol::{LocationLink, RangeExt, TextDocumentPositionParams},
    syntax::{latex, SyntaxNode},
    workspace::{Document, DocumentContent},
};
use async_trait::async_trait;

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

#[async_trait]
impl FeatureProvider for LatexCitationDefinitionProvider {
    type Params = TextDocumentPositionParams;
    type Output = Vec<LocationLink>;

    async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
        let mut links = Vec::new();
        if let Some(reference) = Self::find_reference(req) {
            for doc in req.related() {
                Self::find_definitions(&doc, &reference, &mut links);
            }
        }
        links
    }
}

impl LatexCitationDefinitionProvider {
    fn find_reference(req: &FeatureRequest<TextDocumentPositionParams>) -> Option<&latex::Token> {
        req.current().content.as_latex().and_then(|table| {
            table
                .citations
                .iter()
                .flat_map(|citation| citation.keys(&table))
                .find(|key| key.range().contains(req.params.position))
        })
    }

    fn find_definitions(doc: &Document, reference: &latex::Token, links: &mut Vec<LocationLink>) {
        if let DocumentContent::Bibtex(tree) = &doc.content {
            for entry in tree
                .children(tree.root)
                .filter_map(|node| tree.as_entry(node))
            {
                if let Some(key) = &entry.key {
                    if key.text() == reference.text() {
                        links.push(LocationLink {
                            origin_selection_range: Some(reference.range()),
                            target_uri: doc.uri.clone().into(),
                            target_range: entry.range(),
                            target_selection_range: key.range(),
                        });
                    }
                }
            }
        }
    }
}

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

    #[tokio::test]
    async fn empty_latex_document() {
        let actual_links = FeatureTester::new()
            .file("main.tex", "")
            .main("main.tex")
            .position(0, 0)
            .test_position(LatexCitationDefinitionProvider)
            .await;

        assert!(actual_links.is_empty());
    }

    #[tokio::test]
    async fn empty_bibtex_document() {
        let actual_links = FeatureTester::new()
            .file("main.bib", "")
            .main("main.bib")
            .position(0, 0)
            .test_position(LatexCitationDefinitionProvider)
            .await;

        assert!(actual_links.is_empty());
    }

    #[tokio::test]
    async fn has_definition() {
        let actual_links = FeatureTester::new()
            .file(
                "foo.tex",
                indoc!(
                    r#"
                        \addbibresource{baz.bib}
                        \cite{foo}
                    "#
                ),
            )
            .file("bar.bib", r#"@article{foo, bar = {baz}}"#)
            .file("baz.bib", r#"@article{foo, bar = {baz}}"#)
            .main("foo.tex")
            .position(1, 6)
            .test_position(LatexCitationDefinitionProvider)
            .await;

        let exepcted_links = vec![LocationLink {
            origin_selection_range: Some(Range::new_simple(1, 6, 1, 9)),
            target_uri: FeatureTester::uri("baz.bib").into(),
            target_range: Range::new_simple(0, 0, 0, 26),
            target_selection_range: Range::new_simple(0, 9, 0, 12),
        }];

        assert_eq!(actual_links, exepcted_links);
    }
}