summaryrefslogtreecommitdiff
path: root/support/texlab/src/completion/bibtex/entry_type.rs
blob: e3b12ef00d46f076ef41abc20888b42a34b50483 (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
155
156
157
158
159
160
161
162
163
164
use crate::{
    completion::types::{Item, ItemData},
    feature::FeatureRequest,
    protocol::{CompletionParams, Position, Range, RangeExt},
    syntax::{bibtex, SyntaxNode, LANGUAGE_DATA},
    workspace::DocumentContent,
};

pub async fn complete_bibtex_entry_types<'a>(
    req: &'a FeatureRequest<CompletionParams>,
    items: &mut Vec<Item<'a>>,
) {
    if let DocumentContent::Bibtex(tree) = &req.current().content {
        let pos = req.params.text_document_position.position;
        for decl in tree.children(tree.root) {
            match &tree.graph[decl] {
                bibtex::Node::Preamble(preamble) => {
                    if contains(&preamble.ty, pos) {
                        make_items(items, preamble.ty.range());
                        return;
                    }
                }
                bibtex::Node::String(string) => {
                    if contains(&string.ty, pos) {
                        make_items(items, string.ty.range());
                        return;
                    }
                }
                bibtex::Node::Entry(entry) => {
                    if contains(&entry.ty, pos) {
                        make_items(items, entry.ty.range());
                        return;
                    }
                }
                _ => {}
            }
        }
    }
}

fn contains(ty: &bibtex::Token, pos: Position) -> bool {
    ty.range().contains(pos) && ty.start().character != pos.character
}

fn make_items(items: &mut Vec<Item>, mut range: Range) {
    range.start.character += 1;
    for ty in &LANGUAGE_DATA.entry_types {
        let item = Item::new(range, ItemData::EntryType { ty });
        items.push(item);
    }
}

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

        assert!(actual_items.is_empty());
    }

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

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

        assert!(actual_items.is_empty());
    }

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

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

        assert!(!actual_items.is_empty());
        assert_eq!(actual_items[0].range, Range::new_simple(0, 1, 0, 1));
    }

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

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

        assert!(!actual_items.is_empty());
        assert_eq!(actual_items[0].range, Range::new_simple(0, 1, 0, 4));
    }

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

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

        assert!(actual_items.is_empty());
    }

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

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

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