summaryrefslogtreecommitdiff
path: root/support/texlab/src/definition/latex_cmd.rs
blob: 35fc42be4ffbdbe3d3c242ec402fb60b8f062a1c (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::{
    feature::{FeatureProvider, FeatureRequest},
    protocol::{LocationLink, TextDocumentPositionParams},
    syntax::SyntaxNode,
    workspace::DocumentContent,
};
use async_trait::async_trait;

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

#[async_trait]
impl FeatureProvider for LatexCommandDefinitionProvider {
    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 DocumentContent::Latex(table) = &req.current().content {
            if let Some(cmd) = table
                .find(req.params.position)
                .last()
                .and_then(|node| table.as_command(*node))
            {
                for doc in req.related() {
                    if let DocumentContent::Latex(table) = &doc.content {
                        table
                            .command_definitions
                            .iter()
                            .filter(|def| def.definition_name(&table) == cmd.name.text())
                            .map(|def| {
                                let def_range = table[def.parent].range();
                                LocationLink {
                                    origin_selection_range: Some(cmd.range()),
                                    target_uri: doc.uri.clone().into(),
                                    target_range: def_range,
                                    target_selection_range: def_range,
                                }
                            })
                            .for_each(|link| links.push(link));

                        table
                            .math_operators
                            .iter()
                            .filter(|op| op.definition_name(&table) == cmd.name.text())
                            .map(|op| {
                                let def_range = table[op.parent].range();
                                LocationLink {
                                    origin_selection_range: Some(cmd.range()),
                                    target_uri: doc.uri.clone().into(),
                                    target_range: def_range,
                                    target_selection_range: def_range,
                                }
                            })
                            .for_each(|link| links.push(link));
                    }
                }
            }
        }
        links
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        feature::FeatureTester,
        protocol::{Range, RangeExt},
    };
    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(LatexCommandDefinitionProvider)
            .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(LatexCommandDefinitionProvider)
            .await;

        assert!(actual_links.is_empty());
    }

    #[tokio::test]
    async fn command_definition() {
        let actual_links = FeatureTester::new()
            .file(
                "foo.tex",
                indoc!(
                    r#"
                        \include{bar}
                        \foo
                    "#
                ),
            )
            .file("bar.tex", r#"\newcommand{\foo}{bar}"#)
            .file("baz.tex", r#"\newcommand{\foo}{baz}"#)
            .main("foo.tex")
            .position(1, 3)
            .test_position(LatexCommandDefinitionProvider)
            .await;

        let expected_links = vec![LocationLink {
            origin_selection_range: Some(Range::new_simple(1, 0, 1, 4)),
            target_uri: FeatureTester::uri("bar.tex").into(),
            target_range: Range::new_simple(0, 0, 0, 22),
            target_selection_range: Range::new_simple(0, 0, 0, 22),
        }];

        assert_eq!(actual_links, expected_links);
    }

    #[tokio::test]
    async fn math_operator() {
        let actual_links = FeatureTester::new()
            .file(
                "foo.tex",
                indoc!(
                    r#"
                        \include{bar}
                        \foo
                    "#
                ),
            )
            .file("bar.tex", r#"\DeclareMathOperator{\foo}{bar}"#)
            .file("baz.tex", r#"\DeclareMathOperator{\foo}{baz}"#)
            .main("foo.tex")
            .position(1, 3)
            .test_position(LatexCommandDefinitionProvider)
            .await;

        let expected_links = vec![LocationLink {
            origin_selection_range: Some(Range::new_simple(1, 0, 1, 4)),
            target_uri: FeatureTester::uri("bar.tex").into(),
            target_range: Range::new_simple(0, 0, 0, 31),
            target_selection_range: Range::new_simple(0, 0, 0, 31),
        }];

        assert_eq!(actual_links, expected_links);
    }
}