summaryrefslogtreecommitdiff
path: root/support/texlab/src/completion/latex/theorem.rs
blob: a8706bed0b57281a8580d9b155270313b50c624b (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
use super::combinators;
use crate::{
    completion::types::{Item, ItemData},
    feature::FeatureRequest,
    protocol::CompletionParams,
};

pub async fn complete_latex_theorem_environments<'a>(
    req: &'a FeatureRequest<CompletionParams>,
    items: &mut Vec<Item<'a>>,
) {
    combinators::environment(req, |ctx| async move {
        for table in req
            .related()
            .into_iter()
            .filter_map(|doc| doc.content.as_latex())
        {
            for theorem in &table.theorem_definitions {
                let name = theorem.name(&table).text();
                let data = ItemData::UserEnvironment { name };
                let item = Item::new(ctx.range, data);
                items.push(item);
            }
        }
    })
    .await;
}

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

    #[tokio::test]
    async fn empty_latex_document() {
        let req = FeatureTester::new()
            .file("main.tex", "")
            .main("main.tex")
            .position(0, 0)
            .test_completion_request()
            .await;
        let mut actual_items = Vec::new();

        complete_latex_theorem_environments(&req, &mut actual_items).await;

        assert!(actual_items.is_empty());
    }

    #[tokio::test]
    async fn empty_bibtex_document() {
        let req = FeatureTester::new()
            .file("main.bib", "")
            .main("main.bib")
            .position(0, 0)
            .test_completion_request()
            .await;
        let mut actual_items = Vec::new();

        complete_latex_theorem_environments(&req, &mut actual_items).await;

        assert!(actual_items.is_empty());
    }

    #[tokio::test]
    async fn inside_begin() {
        let req = FeatureTester::new()
            .file(
                "main.tex",
                indoc!(
                    r#"
                        \newtheorem{theorem}{Theorem}
                        \begin{th}
                    "#
                ),
            )
            .main("main.tex")
            .position(1, 8)
            .test_completion_request()
            .await;
        let mut actual_items = Vec::new();

        complete_latex_theorem_environments(&req, &mut actual_items).await;

        assert_eq!(actual_items.len(), 1);
        assert_eq!(actual_items[0].data.label(), "theorem");
        assert_eq!(actual_items[0].range, Range::new_simple(1, 7, 1, 9));
    }

    #[tokio::test]
    async fn outside_begin() {
        let req = FeatureTester::new()
            .file(
                "main.tex",
                indoc!(
                    r#"
                        \newtheorem{theorem}{Theorem}
                        \begin{th}
                    "#
                ),
            )
            .main("main.tex")
            .position(1, 10)
            .test_completion_request()
            .await;
        let mut actual_items = Vec::new();

        complete_latex_theorem_environments(&req, &mut actual_items).await;

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