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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
use crate::{
feature::{FeatureProvider, FeatureRequest},
protocol::{
Position, Range, RangeExt, RenameParams, TextDocumentPositionParams, TextEdit,
WorkspaceEdit,
},
syntax::{Span, SyntaxNode},
workspace::DocumentContent,
};
use async_trait::async_trait;
use std::collections::HashMap;
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct LatexLabelPrepareRenameProvider;
#[async_trait]
impl FeatureProvider for LatexLabelPrepareRenameProvider {
type Params = TextDocumentPositionParams;
type Output = Option<Range>;
async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
let pos = req.params.position;
find_label(&req.current().content, pos).map(Span::range)
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct LatexLabelRenameProvider;
#[async_trait]
impl FeatureProvider for LatexLabelRenameProvider {
type Params = RenameParams;
type Output = Option<WorkspaceEdit>;
async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
let pos = req.params.text_document_position.position;
let name = find_label(&req.current().content, pos)?;
let mut changes = HashMap::new();
for doc in req.related() {
if let DocumentContent::Latex(table) = &doc.content {
let edits = table
.labels
.iter()
.flat_map(|label| label.names(&table))
.filter(|label| label.text() == name.text)
.map(|label| TextEdit::new(label.range(), req.params.new_name.clone()))
.collect();
changes.insert(doc.uri.clone().into(), edits);
}
}
Some(WorkspaceEdit::new(changes))
}
}
fn find_label(content: &DocumentContent, pos: Position) -> Option<&Span> {
if let DocumentContent::Latex(table) = content {
table
.labels
.iter()
.flat_map(|label| label.names(&table))
.find(|label| label.range().contains(pos))
.map(|label| &label.span)
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::feature::FeatureTester;
use indoc::indoc;
#[tokio::test]
async fn label() {
let actual_edit = FeatureTester::new()
.file(
"foo.tex",
indoc!(
r#"
\label{foo}
\include{bar}
"#
),
)
.file("bar.tex", r#"\ref{foo}"#)
.file("baz.tex", r#"\ref{foo}"#)
.main("foo.tex")
.position(0, 7)
.new_name("bar")
.test_rename(LatexLabelRenameProvider)
.await
.unwrap();
let mut expected_changes = HashMap::new();
expected_changes.insert(
FeatureTester::uri("foo.tex").into(),
vec![TextEdit::new(Range::new_simple(0, 7, 0, 10), "bar".into())],
);
expected_changes.insert(
FeatureTester::uri("bar.tex").into(),
vec![TextEdit::new(Range::new_simple(0, 5, 0, 8), "bar".into())],
);
assert_eq!(actual_edit, WorkspaceEdit::new(expected_changes));
}
#[tokio::test]
async fn command_args() {
let actual_edit = FeatureTester::new()
.file("main.tex", r#"\foo{bar}"#)
.main("main.tex")
.position(0, 5)
.new_name("baz")
.test_rename(LatexLabelRenameProvider)
.await;
assert_eq!(actual_edit, None);
}
#[tokio::test]
async fn empty_latex_document() {
let actual_edit = FeatureTester::new()
.file("main.tex", "")
.main("main.tex")
.position(0, 0)
.new_name("")
.test_rename(LatexLabelRenameProvider)
.await;
assert_eq!(actual_edit, None);
}
#[tokio::test]
async fn empty_bibtex_document() {
let actual_edit = FeatureTester::new()
.file("main.bib", "")
.main("main.bib")
.position(0, 0)
.new_name("")
.test_rename(LatexLabelRenameProvider)
.await;
assert_eq!(actual_edit, None);
}
}
|