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
|
use crate::range::RangeExt;
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::*;
use std::sync::Arc;
pub struct LatexLabelHoverProvider;
impl FeatureProvider for LatexLabelHoverProvider {
type Params = TextDocumentPositionParams;
type Output = Option<Hover>;
#[boxed]
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
if let SyntaxTree::Latex(tree) = &request.document().tree {
let reference = Self::find_reference(tree, request.params.position)?;
let (document, definition) = Self::find_definition(&request.view, reference)?;
let workspace = Arc::clone(&request.view.workspace);
let view = DocumentView::new(workspace, document);
let outline = Outline::from(&view);
let outline_context = OutlineContext::parse(&view, &definition, &outline)?;
let markup = outline_context.documentation();
Some(Hover {
contents: HoverContents::Markup(markup),
range: Some(reference.range()),
})
} else {
None
}
}
}
impl LatexLabelHoverProvider {
fn find_reference(tree: &LatexSyntaxTree, position: Position) -> Option<&LatexToken> {
for label in &tree.structure.labels {
let names = label.names();
if names.len() == 1 && label.range().contains(position) {
return Some(&label.names()[0]);
}
for name in &names {
if name.range().contains(position) {
return Some(name);
}
}
}
None
}
fn find_definition<'a, 'b>(
view: &'a DocumentView,
reference: &'b LatexToken,
) -> Option<(Arc<Document>, &'a LatexLabel)> {
for document in &view.related_documents {
if let SyntaxTree::Latex(tree) = &document.tree {
for label in &tree.structure.labels {
if label.kind == LatexLabelKind::Definition {
for name in label.names() {
if name.text() == reference.text() {
return Some((Arc::clone(&document), label));
}
}
}
}
}
}
None
}
}
|