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
|
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::{LocationLink, TextDocumentPositionParams};
pub struct LatexCommandDefinitionProvider;
impl FeatureProvider for LatexCommandDefinitionProvider {
type Params = TextDocumentPositionParams;
type Output = Vec<LocationLink>;
#[boxed]
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
let mut definitions = Vec::new();
if let SyntaxTree::Latex(tree) = &request.document().tree {
if let Some(LatexNode::Command(command)) = tree.find(request.params.position).last() {
for document in request.related_documents() {
if let SyntaxTree::Latex(tree) = &document.tree {
tree.command_definitions
.iter()
.filter(|def| def.definition.name.text() == command.name.text())
.map(|def| LocationLink {
origin_selection_range: Some(command.range()),
target_uri: document.uri.clone().into(),
target_range: def.range(),
target_selection_range: def.range(),
})
.for_each(|def| definitions.push(def));
tree.math
.operators
.iter()
.filter(|op| op.definition.name.text() == command.name.text())
.map(|op| LocationLink {
origin_selection_range: Some(command.range()),
target_uri: document.uri.clone().into(),
target_range: op.range(),
target_selection_range: op.range(),
})
.for_each(|def| definitions.push(def));
}
}
}
}
definitions
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::range::RangeExt;
use lsp_types::{Position, Range};
#[test]
fn test_command_definition() {
let links = test_feature(
LatexCommandDefinitionProvider,
FeatureSpec {
files: vec![
FeatureSpec::file("foo.tex", "\\include{bar}\n\\foo"),
FeatureSpec::file("bar.tex", "\\newcommand{\\foo}{bar}"),
FeatureSpec::file("baz.tex", "\\newcommand{\\foo}{baz}"),
],
main_file: "foo.tex",
position: Position::new(1, 3),
..FeatureSpec::default()
},
);
assert_eq!(
links,
vec![LocationLink {
origin_selection_range: Some(Range::new_simple(1, 0, 1, 4)),
target_uri: FeatureSpec::uri("bar.tex"),
target_range: Range::new_simple(0, 0, 0, 22),
target_selection_range: Range::new_simple(0, 0, 0, 22),
}]
);
}
#[test]
fn test_math_operator() {
let links = test_feature(
LatexCommandDefinitionProvider,
FeatureSpec {
files: vec![
FeatureSpec::file("foo.tex", "\\include{bar}\n\\foo"),
FeatureSpec::file("bar.tex", "\\DeclareMathOperator{\\foo}{bar}"),
FeatureSpec::file("baz.tex", "\\DeclareMathOperator{\\foo}{baz}"),
],
main_file: "foo.tex",
position: Position::new(1, 3),
..FeatureSpec::default()
},
);
assert_eq!(
links,
vec![LocationLink {
origin_selection_range: Some(Range::new_simple(1, 0, 1, 4)),
target_uri: FeatureSpec::uri("bar.tex"),
target_range: Range::new_simple(0, 0, 0, 31),
target_selection_range: Range::new_simple(0, 0, 0, 31)
}]
);
}
}
|