summaryrefslogtreecommitdiff
path: root/support/texlab/src/completion/latex/glossary.rs
blob: 417152dd212b64c314fe5ceb1f0ca580a85670ec (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
use super::combinators::{self, Parameter};
use crate::{
    completion::types::{Item, ItemData},
    feature::FeatureRequest,
    protocol::CompletionParams,
    syntax::{
        LatexGlossaryEntryKind::{Acronym, General},
        LANGUAGE_DATA,
    },
    workspace::DocumentContent,
};

pub async fn complete_latex_glossary_entries<'a>(
    req: &'a FeatureRequest<CompletionParams>,
    items: &mut Vec<Item<'a>>,
) {
    let parameters = LANGUAGE_DATA
        .glossary_entry_reference_commands
        .iter()
        .map(|cmd| Parameter {
            name: &cmd.name[1..],
            index: cmd.index,
        });

    combinators::argument(req, parameters, |ctx| async move {
        let cmd_kind = LANGUAGE_DATA
            .glossary_entry_reference_commands
            .iter()
            .find(|cmd| &cmd.name[1..] == ctx.parameter.name)
            .unwrap()
            .kind;

        for doc in req.related() {
            if let DocumentContent::Latex(table) = &doc.content {
                for entry in &table.glossary_entries {
                    match (cmd_kind, entry.kind) {
                        (Acronym, Acronym) | (General, General) | (General, Acronym) => {
                            let name = entry.label(&table).text();
                            let item = Item::new(ctx.range, ItemData::GlossaryEntry { name });
                            items.push(item);
                        }
                        (Acronym, General) => {}
                    }
                }
            }
        }
    })
    .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_glossary_entries(&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_glossary_entries(&req, &mut actual_items).await;

        assert!(actual_items.is_empty());
    }

    #[tokio::test]
    async fn acronym() {
        let req = FeatureTester::new()
            .file(
                "main.tex",
                indoc!(
                    r#"
                        \newacronym{lvm}{LVM}{Logical Volume Manager}
                        \acrfull{foo}
                    "#
                ),
            )
            .main("main.tex")
            .position(1, 9)
            .test_completion_request()
            .await;
        let mut actual_items = Vec::new();

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

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