summaryrefslogtreecommitdiff
path: root/support/texlab/src/symbol/bibtex_entry.rs
blob: f19c2fb734c83070a5d84bea60a613a3fb26d589 (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
use super::{LatexSymbol, LatexSymbolKind};
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::*;

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

impl FeatureProvider for BibtexEntrySymbolProvider {
    type Params = DocumentSymbolParams;
    type Output = Vec<LatexSymbol>;

    #[boxed]
    async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
        let mut symbols = Vec::new();
        if let SyntaxTree::Bibtex(tree) = &request.document().tree {
            for entry in tree
                .entries()
                .iter()
                .filter(|entry| !entry.is_comment())
                .filter(|entry| entry.key.is_some())
            {
                let category = LANGUAGE_DATA
                    .find_entry_type(&entry.ty.text()[1..])
                    .map(|ty| ty.category)
                    .unwrap_or(BibtexEntryTypeCategory::Misc);

                let key = entry.key.as_ref().unwrap();
                let symbol = LatexSymbol {
                    name: key.text().to_owned(),
                    label: None,
                    kind: LatexSymbolKind::Entry(category),
                    deprecated: false,
                    full_range: entry.range(),
                    selection_range: key.range(),
                    children: Self::field_symbols(&entry),
                };
                symbols.push(symbol);
            }
        }
        symbols
    }
}

impl BibtexEntrySymbolProvider {
    fn field_symbols(entry: &BibtexEntry) -> Vec<LatexSymbol> {
        let mut symbols = Vec::new();
        for field in &entry.fields {
            let symbol = LatexSymbol {
                name: field.name.text().to_owned(),
                label: None,
                kind: LatexSymbolKind::Field,
                deprecated: false,
                full_range: field.range(),
                selection_range: field.name.range(),
                children: Vec::new(),
            };
            symbols.push(symbol);
        }
        symbols
    }
}

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

    #[test]
    fn test_entry() {
        let symbols = test_feature(
            BibtexEntrySymbolProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file(
                    "foo.bib",
                    "@article{key, foo = bar, baz = qux}",
                )],
                main_file: "foo.bib",
                ..FeatureSpec::default()
            },
        );
        assert_eq!(
            symbols,
            vec![LatexSymbol {
                name: "key".into(),
                label: None,
                kind: LatexSymbolKind::Entry(BibtexEntryTypeCategory::Article),
                deprecated: false,
                full_range: Range::new_simple(0, 0, 0, 35),
                selection_range: Range::new_simple(0, 9, 0, 12),
                children: vec![
                    LatexSymbol {
                        name: "foo".into(),
                        label: None,
                        kind: LatexSymbolKind::Field,
                        deprecated: false,
                        full_range: Range::new_simple(0, 14, 0, 24),
                        selection_range: Range::new_simple(0, 14, 0, 17),
                        children: Vec::new(),
                    },
                    LatexSymbol {
                        name: "baz".into(),
                        label: None,
                        kind: LatexSymbolKind::Field,
                        deprecated: false,
                        full_range: Range::new_simple(0, 25, 0, 34),
                        selection_range: Range::new_simple(0, 25, 0, 28),
                        children: Vec::new(),
                    },
                ],
            }]
        );
    }

    #[test]
    fn test_comment() {
        let symbols = test_feature(
            BibtexEntrySymbolProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file(
                    "foo.bib",
                    "@comment{key, foo = bar, baz = qux}",
                )],
                main_file: "foo.bib",
                ..FeatureSpec::default()
            },
        );
        assert_eq!(symbols, Vec::new());
    }

    #[test]
    fn test_latex() {
        let symbols = test_feature(
            BibtexEntrySymbolProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file(
                    "foo.tex",
                    "@article{key, foo = bar, baz = qux}",
                )],
                main_file: "foo.tex",
                ..FeatureSpec::default()
            },
        );
        assert_eq!(symbols, Vec::new());
    }
}