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
|
use crate::range::RangeExt;
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::{LocationLink, TextDocumentPositionParams};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct LatexCitationDefinitionProvider;
impl FeatureProvider for LatexCitationDefinitionProvider {
type Params = TextDocumentPositionParams;
type Output = Vec<LocationLink>;
#[boxed]
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
let mut links = Vec::new();
if let Some(reference) = Self::find_reference(&request) {
for document in request.related_documents() {
Self::find_definitions(&document, &reference, &mut links);
}
}
links
}
}
impl LatexCitationDefinitionProvider {
fn find_definitions(
document: &Document,
reference: &LatexToken,
links: &mut Vec<LocationLink>,
) {
if let SyntaxTree::Bibtex(tree) = &document.tree {
for entry in tree.entries() {
if let Some(key) = &entry.key {
if key.text() == reference.text() {
let link = LocationLink {
origin_selection_range: Some(reference.range()),
target_uri: document.uri.clone().into(),
target_range: entry.range(),
target_selection_range: key.range(),
};
links.push(link);
}
}
}
}
}
fn find_reference(request: &FeatureRequest<TextDocumentPositionParams>) -> Option<&LatexToken> {
if let SyntaxTree::Latex(tree) = &request.document().tree {
tree.citations
.iter()
.flat_map(LatexCitation::keys)
.find(|key| key.range().contains(request.params.position))
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use lsp_types::{Position, Range};
#[test]
fn test_has_definition() {
let links = test_feature(
LatexCitationDefinitionProvider,
FeatureSpec {
files: vec![
FeatureSpec::file("foo.tex", "\\addbibresource{baz.bib}\n\\cite{foo}"),
FeatureSpec::file("bar.bib", "@article{foo, bar = {baz}}"),
FeatureSpec::file("baz.bib", "@article{foo, bar = {baz}}"),
],
main_file: "foo.tex",
position: Position::new(1, 6),
..FeatureSpec::default()
},
);
assert_eq!(
links,
vec![LocationLink {
origin_selection_range: Some(Range::new_simple(1, 6, 1, 9)),
target_uri: FeatureSpec::uri("baz.bib"),
target_range: Range::new_simple(0, 0, 0, 26),
target_selection_range: Range::new_simple(0, 9, 0, 12)
}]
);
}
#[test]
fn test_no_definition_latex() {
let links = test_feature(
LatexCitationDefinitionProvider,
FeatureSpec {
files: vec![FeatureSpec::file("foo.tex", "")],
main_file: "foo.tex",
position: Position::new(0, 0),
..FeatureSpec::default()
},
);
assert!(links.is_empty());
}
#[test]
fn test_no_definition_bibtex() {
let links = test_feature(
LatexCitationDefinitionProvider,
FeatureSpec {
files: vec![FeatureSpec::file("foo.bib", "")],
main_file: "foo.bib",
position: Position::new(0, 0),
..FeatureSpec::default()
},
);
assert!(links.is_empty());
}
}
|