summaryrefslogtreecommitdiff
path: root/support/texlab/src/highlight/latex_label.rs
blob: c6d4aab6ee8f4656b92108388afa7e7970f7ee68 (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
use crate::{
    feature::{FeatureProvider, FeatureRequest},
    protocol::{DocumentHighlight, DocumentHighlightKind, RangeExt, TextDocumentPositionParams},
    syntax::{latex, LatexLabelKind, SyntaxNode},
    workspace::DocumentContent,
};
use async_trait::async_trait;

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

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

    async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
        let mut highlights = Vec::new();
        if let DocumentContent::Latex(table) = &req.current().content {
            if let Some(name) = table
                .labels
                .iter()
                .flat_map(|label| label.names(&table))
                .find(|label| label.range().contains(req.params.position))
                .map(latex::Token::text)
            {
                for label_group in &table.labels {
                    for label in label_group.names(&table) {
                        if label.text() == name {
                            let kind = match label_group.kind {
                                LatexLabelKind::Definition => DocumentHighlightKind::Write,
                                LatexLabelKind::Reference(_) => DocumentHighlightKind::Read,
                            };

                            let highlight = DocumentHighlight {
                                range: label.range(),
                                kind: Some(kind),
                            };
                            highlights.push(highlight);
                        }
                    }
                }
            }
        }
        highlights
    }
}

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

    #[tokio::test]
    async fn has_label() {
        let actual_highlights = FeatureTester::new()
            .file(
                "foo.tex",
                indoc!(
                    r#"
                        \label{foo}
                        \ref{foo}
                    "#
                ),
            )
            .main("foo.tex")
            .position(0, 7)
            .test_position(LatexLabelHighlightProvider)
            .await;

        let expected_highlights = vec![
            DocumentHighlight {
                range: Range::new_simple(0, 7, 0, 10),
                kind: Some(DocumentHighlightKind::Write),
            },
            DocumentHighlight {
                range: Range::new_simple(1, 5, 1, 8),
                kind: Some(DocumentHighlightKind::Read),
            },
        ];

        assert_eq!(actual_highlights, expected_highlights);
    }

    #[tokio::test]
    async fn no_label_latex() {
        let actual_highlights = FeatureTester::new()
            .file("foo.tex", "")
            .main("foo.tex")
            .position(0, 0)
            .test_position(LatexLabelHighlightProvider)
            .await;

        assert!(actual_highlights.is_empty());
    }

    #[tokio::test]
    async fn no_label_bibtex() {
        let actual_highlights = FeatureTester::new()
            .file("foo.bib", "")
            .main("foo.bib")
            .position(0, 0)
            .test_position(LatexLabelHighlightProvider)
            .await;

        assert!(actual_highlights.is_empty());
    }
}