summaryrefslogtreecommitdiff
path: root/support/texlab/src/symbol/bibtex_string.rs
blob: 8f1f436048a0697433a8ac0ffa9c2086ef30254b (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
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 BibtexStringSymbolProvider;

impl FeatureProvider for BibtexStringSymbolProvider {
    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 child in &tree.root.children {
                if let BibtexDeclaration::String(string) = &child {
                    if let Some(name) = &string.name {
                        symbols.push(LatexSymbol {
                            name: name.text().to_owned(),
                            label: None,
                            kind: LatexSymbolKind::String,
                            deprecated: false,
                            full_range: string.range(),
                            selection_range: name.range(),
                            children: Vec::new(),
                        });
                    }
                }
            }
        }
        symbols
    }
}

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

    #[test]
    fn test_valid() {
        let symbols = test_feature(
            BibtexStringSymbolProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file("foo.bib", "@string{key = \"value\"}")],
                main_file: "foo.bib",
                ..FeatureSpec::default()
            },
        );
        assert_eq!(
            symbols,
            vec![LatexSymbol {
                name: "key".into(),
                label: None,
                kind: LatexSymbolKind::String,
                deprecated: false,
                full_range: Range::new_simple(0, 0, 0, 22),
                selection_range: Range::new_simple(0, 8, 0, 11),
                children: Vec::new(),
            }]
        );
    }

    #[test]
    fn test_invalid() {
        let symbols = test_feature(
            BibtexStringSymbolProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file("foo.bib", "@string{}")],
                main_file: "foo.bib",
                ..FeatureSpec::default()
            },
        );
        assert_eq!(symbols, Vec::new());
    }
}