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
|
use crate::{
feature::{FeatureProvider, FeatureRequest},
protocol::{
Hover, HoverContents, MarkupContent, MarkupKind, RangeExt, TextDocumentPositionParams,
},
syntax::{SyntaxNode, LANGUAGE_DATA},
};
use async_trait::async_trait;
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct BibtexEntryTypeHoverProvider;
#[async_trait]
impl FeatureProvider for BibtexEntryTypeHoverProvider {
type Params = TextDocumentPositionParams;
type Output = Option<Hover>;
async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
let tree = req.current().content.as_bibtex()?;
for entry in tree
.children(tree.root)
.filter_map(|node| tree.as_entry(node))
{
if entry.ty.range().contains(req.params.position) {
let ty = &entry.ty.text()[1..];
let docs = LANGUAGE_DATA.entry_type_documentation(ty)?;
return Some(Hover {
contents: HoverContents::Markup(MarkupContent {
kind: MarkupKind::Markdown,
value: docs.into(),
}),
range: Some(entry.ty.range()),
});
}
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{feature::FeatureTester, protocol::Range};
#[tokio::test]
async fn empty_latex_document() {
let actual_hover = FeatureTester::new()
.file("main.tex", "")
.main("main.tex")
.position(0, 0)
.test_position(BibtexEntryTypeHoverProvider)
.await;
assert_eq!(actual_hover, None);
}
#[tokio::test]
async fn empty_bibtex_document() {
let actual_hover = FeatureTester::new()
.file("main.bib", "")
.main("main.bib")
.position(0, 0)
.test_position(BibtexEntryTypeHoverProvider)
.await;
assert_eq!(actual_hover, None);
}
#[tokio::test]
async fn known_entry_type() {
let actual_hover = FeatureTester::new()
.file("main.bib", "@article{foo,}")
.main("main.bib")
.position(0, 3)
.test_position(BibtexEntryTypeHoverProvider)
.await
.unwrap();
let expected_hover = Hover {
contents: HoverContents::Markup(MarkupContent {
kind: MarkupKind::Markdown,
value: LANGUAGE_DATA
.entry_type_documentation("article")
.unwrap()
.into(),
}),
range: Some(Range::new_simple(0, 0, 0, 8)),
};
assert_eq!(actual_hover, expected_hover);
}
#[tokio::test]
async fn unknown_entry_type() {
let actual_hover = FeatureTester::new()
.file("main.bib", "@foo{bar,}")
.main("main.bib")
.position(0, 3)
.test_position(BibtexEntryTypeHoverProvider)
.await;
assert_eq!(actual_hover, None);
}
#[tokio::test]
async fn entry_key() {
let actual_hover = FeatureTester::new()
.file("main.bib", "@article{foo,}")
.main("main.bib")
.position(0, 11)
.test_position(BibtexEntryTypeHoverProvider)
.await;
assert_eq!(actual_hover, None);
}
}
|