summaryrefslogtreecommitdiff
path: root/support/texlab/src/hover/bibtex_entry_type.rs
blob: adf833b6897c229d9cbe584db9ce9284a4603b1e (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
use crate::range::RangeExt;
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::*;

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

impl FeatureProvider for BibtexEntryTypeHoverProvider {
    type Params = TextDocumentPositionParams;
    type Output = Option<Hover>;

    #[boxed]
    async fn execute<'a>(
        &'a self,
        request: &'a FeatureRequest<TextDocumentPositionParams>,
    ) -> Option<Hover> {
        if let SyntaxTree::Bibtex(tree) = &request.document().tree {
            for entry in tree.entries() {
                if entry.ty.range().contains(request.params.position) {
                    let ty = &entry.ty.text()[1..];
                    if let Some(documentation) = LANGUAGE_DATA.entry_type_documentation(ty) {
                        return Some(Hover {
                            contents: HoverContents::Markup(MarkupContent {
                                kind: MarkupKind::Markdown,
                                value: documentation.into(),
                            }),
                            range: None,
                        });
                    }
                }
            }
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use lsp_types::Position;

    #[test]
    fn test_known_entry_type() {
        let hover = test_feature(
            BibtexEntryTypeHoverProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file("foo.bib", "@article{foo,}")],
                main_file: "foo.bib",
                position: Position::new(0, 3),
                ..FeatureSpec::default()
            },
        );
        assert_eq!(
            hover,
            Some(Hover {
                contents: HoverContents::Markup(MarkupContent {
                    kind: MarkupKind::Markdown,
                    value: LANGUAGE_DATA
                        .entry_type_documentation("article")
                        .unwrap()
                        .into(),
                }),
                range: None,
            })
        );
    }

    #[test]
    fn test_unknown_entry_type() {
        let hover = test_feature(
            BibtexEntryTypeHoverProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file("foo.bib", "@foo{bar,}")],
                main_file: "foo.bib",
                position: Position::new(0, 3),
                ..FeatureSpec::default()
            },
        );
        assert_eq!(hover, None);
    }

    #[test]
    fn test_entry_key() {
        let hover = test_feature(
            BibtexEntryTypeHoverProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file("foo.bib", "@article{foo,}")],
                main_file: "foo.bib",
                position: Position::new(0, 11),
                ..FeatureSpec::default()
            },
        );
        assert_eq!(hover, None);
    }

    #[test]
    fn test_latex() {
        let hover = test_feature(
            BibtexEntryTypeHoverProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file("foo.tex", "\\foo")],
                main_file: "foo.tex",
                position: Position::new(0, 3),
                ..FeatureSpec::default()
            },
        );
        assert_eq!(hover, None);
    }
}