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

pub async fn complete_latex_begin_command<'a>(
    req: &'a FeatureRequest<CompletionParams>,
    items: &mut Vec<Item<'a>>,
) {
    combinators::command(req, |cmd_node| async move {
        let table = req.current().content.as_latex().unwrap();
        let cmd = table.as_command(cmd_node).unwrap();
        let range = cmd.short_name_range();
        items.push(Item::new(range, ItemData::BeginCommand));
    })
    .await;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::feature::FeatureTester;

    #[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_begin_command(&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_begin_command(&req, &mut actual_items).await;

        assert!(actual_items.is_empty());
    }

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

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

        assert_eq!(actual_items.len(), 1);
    }
}