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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
use ra_text_edit::AtomTextEdit;
use ra_syntax::{TextUnit, TextRange};
use crate::{LineIndex, LineCol};
use superslice::Ext;
#[derive(Debug)]
struct OffsetNewlineIter<'a> {
text: &'a str,
offset: TextUnit,
}
impl<'a> Iterator for OffsetNewlineIter<'a> {
type Item = TextUnit;
fn next(&mut self) -> Option<TextUnit> {
let next_idx = self
.text
.char_indices()
.filter_map(|(i, c)| if c == '\n' { Some(i + 1) } else { None })
.next()?;
let next = self.offset + TextUnit::from_usize(next_idx);
self.text = &self.text[next_idx..];
self.offset = next;
Some(next)
}
}
#[derive(Debug)]
enum NextNewlines<'a> {
Use,
ReplaceMany(OffsetNewlineIter<'a>),
AddMany(OffsetNewlineIter<'a>),
}
#[derive(Debug)]
struct TranslatedEdit<'a> {
delete: TextRange,
insert: &'a str,
diff: i64,
}
struct Edits<'a, 'b> {
edits: &'b [&'a AtomTextEdit],
current: Option<TranslatedEdit<'a>>,
acc_diff: i64,
}
impl<'a, 'b> Edits<'a, 'b> {
fn new(sorted_edits: &'b [&'a AtomTextEdit]) -> Edits<'a, 'b> {
let mut x = Edits {
edits: sorted_edits,
current: None,
acc_diff: 0,
};
x.advance_edit();
x
}
fn advance_edit(&mut self) {
self.acc_diff += self.current.as_ref().map_or(0, |x| x.diff);
match self.edits.split_first() {
Some((next, rest)) => {
let delete = self.translate_range(next.delete);
let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64;
self.current = Some(TranslatedEdit {
delete,
insert: &next.insert,
diff,
});
self.edits = rest;
}
None => {
self.current = None;
}
}
}
fn next_inserted_newlines(&mut self) -> Option<OffsetNewlineIter<'a>> {
let cur = self.current.as_ref()?;
let res = Some(OffsetNewlineIter {
offset: cur.delete.start(),
text: &cur.insert,
});
self.advance_edit();
res
}
fn next_newlines(&mut self, candidate: TextUnit) -> NextNewlines {
let res = match &mut self.current {
Some(edit) => {
if candidate <= edit.delete.start() {
NextNewlines::Use
} else if candidate <= edit.delete.end() {
let iter = OffsetNewlineIter {
offset: edit.delete.start(),
text: &edit.insert,
};
// empty slice
edit.insert = &edit.insert[edit.insert.len()..];
NextNewlines::ReplaceMany(iter)
} else {
let iter = OffsetNewlineIter {
offset: edit.delete.start(),
text: &edit.insert,
};
// empty slice
edit.insert = &edit.insert[edit.insert.len()..];
self.advance_edit();
NextNewlines::AddMany(iter)
}
}
None => NextNewlines::Use,
};
res
}
fn translate_range(&self, range: TextRange) -> TextRange {
if self.acc_diff == 0 {
range
} else {
let start = self.translate(range.start());
let end = self.translate(range.end());
TextRange::from_to(start, end)
}
}
fn translate(&self, x: TextUnit) -> TextUnit {
if self.acc_diff == 0 {
x
} else {
TextUnit::from((x.to_usize() as i64 + self.acc_diff) as u32)
}
}
}
pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 {
let mut sorted_edits: Vec<&AtomTextEdit> = Vec::with_capacity(edits.len());
for edit in edits {
let insert_index =
sorted_edits.upper_bound_by_key(&edit.delete.start(), |x| x.delete.start());
sorted_edits.insert(insert_index, &edit);
}
let mut state = Edits::new(&sorted_edits);
let mut lines: u32 = 0;
macro_rules! test_newline {
($x:ident) => {
if offset < $x {
return lines;
} else {
lines += 1;
}
};
}
for &orig_newline in line_index.newlines() {
loop {
let translated_newline = state.translate(orig_newline);
match state.next_newlines(translated_newline) {
NextNewlines::Use => {
test_newline!(translated_newline);
break;
}
NextNewlines::ReplaceMany(ns) => {
for n in ns {
test_newline!(n);
}
break;
}
NextNewlines::AddMany(ns) => {
for n in ns {
test_newline!(n);
}
}
}
}
}
loop {
match state.next_inserted_newlines() {
None => break,
Some(ns) => {
for n in ns {
test_newline!(n);
}
}
}
}
lines
}
// for bench
pub fn translate_after_edit(
pre_edit_text: &str,
offset: TextUnit,
edits: Vec<AtomTextEdit>,
) -> LineCol {
let text = edit_text(pre_edit_text, edits);
let line_index = LineIndex::new(&text);
line_index.line_col(offset)
}
fn edit_text(pre_edit_text: &str, mut edits: Vec<AtomTextEdit>) -> String {
// apply edits ordered from last to first
// since they should not overlap we can just use start()
edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize));
let mut text = pre_edit_text.to_owned();
for edit in &edits {
let range = edit.delete.start().to_usize()..edit.delete.end().to_usize();
text.replace_range(range, &edit.insert);
}
text
}
#[cfg(test)]
mod test {
use proptest::{prelude::*, proptest, proptest_helper};
use super::*;
use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits};
#[derive(Debug)]
struct ArbTextWithOffsetAndEdits {
text: String,
offset: TextUnit,
edits: Vec<AtomTextEdit>,
}
fn arb_text_with_offset_and_edits() -> BoxedStrategy<ArbTextWithOffsetAndEdits> {
arb_text()
.prop_flat_map(|text| {
(arb_offset(&text), arb_edits(&text), Just(text)).prop_map(
|(offset, edits, text)| ArbTextWithOffsetAndEdits {
text,
offset,
edits,
},
)
})
.boxed()
}
proptest! {
#[test]
fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) {
let line_index = LineIndex::new(&x.text);
let expected = translate_after_edit(&x.text, x.offset, x.edits.clone());
let actual_lines = count_newlines(x.offset, &line_index, &x.edits);
assert_eq!(actual_lines, expected.line);
}
}
}
|