aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-12-27 12:19:19 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-12-27 12:19:19 +0000
commite422c2e2f4117cf977d28a40a9c8e4dc4cfee811 (patch)
tree9ea1dc365e420c286834b40923deb95a0ca291b9 /crates
parent55ab0c602e391537f5e1a84a617fdd817e6a4200 (diff)
parent1cda43aafd623b400f5916b1d3727b56c136081b (diff)
Merge #325
325: implement translate_offset_with_edit r=matklad a=vemoo - Implement `translate_offset_with_edit` to resolve #105 - Add proptest impls for text, offsets and edits and use them in tests for `translate_offset_with_edit` and `LineIndex` - Added benchmark for `translate_offset_with_edit` Co-authored-by: Bernardo <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_editor/Cargo.toml1
-rw-r--r--crates/ra_editor/src/lib.rs2
-rw-r--r--crates/ra_editor/src/line_index.rs283
-rw-r--r--crates/ra_editor/src/line_index_utils.rs363
-rw-r--r--crates/ra_lsp_server/src/conv.rs47
-rw-r--r--crates/ra_text_edit/Cargo.toml1
-rw-r--r--crates/ra_text_edit/src/lib.rs3
-rw-r--r--crates/ra_text_edit/src/test_utils.rs85
-rw-r--r--crates/ra_text_edit/src/text_edit.rs15
9 files changed, 650 insertions, 150 deletions
diff --git a/crates/ra_editor/Cargo.toml b/crates/ra_editor/Cargo.toml
index c29be1350..f39fe4af6 100644
--- a/crates/ra_editor/Cargo.toml
+++ b/crates/ra_editor/Cargo.toml
@@ -16,3 +16,4 @@ ra_text_edit = { path = "../ra_text_edit" }
16 16
17[dev-dependencies] 17[dev-dependencies]
18test_utils = { path = "../test_utils" } 18test_utils = { path = "../test_utils" }
19proptest = "0.8.7"
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs
index a8c68e79e..d9b89155b 100644
--- a/crates/ra_editor/src/lib.rs
+++ b/crates/ra_editor/src/lib.rs
@@ -2,6 +2,7 @@ mod code_actions;
2mod extend_selection; 2mod extend_selection;
3mod folding_ranges; 3mod folding_ranges;
4mod line_index; 4mod line_index;
5mod line_index_utils;
5mod symbols; 6mod symbols;
6#[cfg(test)] 7#[cfg(test)]
7mod test_utils; 8mod test_utils;
@@ -12,6 +13,7 @@ pub use self::{
12 extend_selection::extend_selection, 13 extend_selection::extend_selection,
13 folding_ranges::{folding_ranges, Fold, FoldKind}, 14 folding_ranges::{folding_ranges, Fold, FoldKind},
14 line_index::{LineCol, LineIndex}, 15 line_index::{LineCol, LineIndex},
16 line_index_utils::translate_offset_with_edit,
15 symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, 17 symbols::{file_structure, file_symbols, FileSymbol, StructureNode},
16 typing::{join_lines, on_enter, on_eq_typed}, 18 typing::{join_lines, on_enter, on_eq_typed},
17}; 19};
diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs
index aab7e4081..898fee7e0 100644
--- a/crates/ra_editor/src/line_index.rs
+++ b/crates/ra_editor/src/line_index.rs
@@ -4,8 +4,8 @@ use superslice::Ext;
4 4
5#[derive(Clone, Debug, PartialEq, Eq)] 5#[derive(Clone, Debug, PartialEq, Eq)]
6pub struct LineIndex { 6pub struct LineIndex {
7 newlines: Vec<TextUnit>, 7 pub(crate) newlines: Vec<TextUnit>,
8 utf16_lines: FxHashMap<u32, Vec<Utf16Char>>, 8 pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>,
9} 9}
10 10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -15,9 +15,9 @@ pub struct LineCol {
15} 15}
16 16
17#[derive(Clone, Debug, Hash, PartialEq, Eq)] 17#[derive(Clone, Debug, Hash, PartialEq, Eq)]
18struct Utf16Char { 18pub(crate) struct Utf16Char {
19 start: TextUnit, 19 pub(crate) start: TextUnit,
20 end: TextUnit, 20 pub(crate) end: TextUnit,
21} 21}
22 22
23impl Utf16Char { 23impl Utf16Char {
@@ -62,6 +62,12 @@ impl LineIndex {
62 62
63 curr_col += char_len; 63 curr_col += char_len;
64 } 64 }
65
66 // Save any utf-16 characters seen in the last line
67 if utf16_chars.len() > 0 {
68 utf16_lines.insert(line, utf16_chars);
69 }
70
65 LineIndex { 71 LineIndex {
66 newlines, 72 newlines,
67 utf16_lines, 73 utf16_lines,
@@ -122,111 +128,179 @@ impl LineIndex {
122 } 128 }
123} 129}
124 130
125#[test] 131#[cfg(test)]
126fn test_line_index() { 132/// Simple reference implementation to use in proptests
127 let text = "hello\nworld"; 133pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol {
128 let index = LineIndex::new(text); 134 let mut res = LineCol {
129 assert_eq!( 135 line: 0,
130 index.line_col(0.into()), 136 col_utf16: 0,
131 LineCol { 137 };
132 line: 0, 138 for (i, c) in text.char_indices() {
133 col_utf16: 0 139 if i + c.len_utf8() > offset.to_usize() {
134 } 140 // if it's an invalid offset, inside a multibyte char
135 ); 141 // return as if it was at the start of the char
136 assert_eq!( 142 break;
137 index.line_col(1.into()),
138 LineCol {
139 line: 0,
140 col_utf16: 1
141 }
142 );
143 assert_eq!(
144 index.line_col(5.into()),
145 LineCol {
146 line: 0,
147 col_utf16: 5
148 }
149 );
150 assert_eq!(
151 index.line_col(6.into()),
152 LineCol {
153 line: 1,
154 col_utf16: 0
155 }
156 );
157 assert_eq!(
158 index.line_col(7.into()),
159 LineCol {
160 line: 1,
161 col_utf16: 1
162 }
163 );
164 assert_eq!(
165 index.line_col(8.into()),
166 LineCol {
167 line: 1,
168 col_utf16: 2
169 }
170 );
171 assert_eq!(
172 index.line_col(10.into()),
173 LineCol {
174 line: 1,
175 col_utf16: 4
176 }
177 );
178 assert_eq!(
179 index.line_col(11.into()),
180 LineCol {
181 line: 1,
182 col_utf16: 5
183 } 143 }
184 ); 144 if c == '\n' {
185 assert_eq!( 145 res.line += 1;
186 index.line_col(12.into()), 146 res.col_utf16 = 0;
187 LineCol { 147 } else {
188 line: 1, 148 res.col_utf16 += 1;
189 col_utf16: 6
190 } 149 }
191 ); 150 }
151 res
152}
192 153
193 let text = "\nhello\nworld"; 154#[cfg(test)]
194 let index = LineIndex::new(text); 155mod test_line_index {
195 assert_eq!( 156 use super::*;
196 index.line_col(0.into()), 157 use proptest::{prelude::*, proptest, proptest_helper};
197 LineCol { 158 use ra_text_edit::test_utils::{arb_text, arb_offset};
159
160 #[test]
161 fn test_line_index() {
162 let text = "hello\nworld";
163 let index = LineIndex::new(text);
164 assert_eq!(
165 index.line_col(0.into()),
166 LineCol {
167 line: 0,
168 col_utf16: 0
169 }
170 );
171 assert_eq!(
172 index.line_col(1.into()),
173 LineCol {
174 line: 0,
175 col_utf16: 1
176 }
177 );
178 assert_eq!(
179 index.line_col(5.into()),
180 LineCol {
181 line: 0,
182 col_utf16: 5
183 }
184 );
185 assert_eq!(
186 index.line_col(6.into()),
187 LineCol {
188 line: 1,
189 col_utf16: 0
190 }
191 );
192 assert_eq!(
193 index.line_col(7.into()),
194 LineCol {
195 line: 1,
196 col_utf16: 1
197 }
198 );
199 assert_eq!(
200 index.line_col(8.into()),
201 LineCol {
202 line: 1,
203 col_utf16: 2
204 }
205 );
206 assert_eq!(
207 index.line_col(10.into()),
208 LineCol {
209 line: 1,
210 col_utf16: 4
211 }
212 );
213 assert_eq!(
214 index.line_col(11.into()),
215 LineCol {
216 line: 1,
217 col_utf16: 5
218 }
219 );
220 assert_eq!(
221 index.line_col(12.into()),
222 LineCol {
223 line: 1,
224 col_utf16: 6
225 }
226 );
227
228 let text = "\nhello\nworld";
229 let index = LineIndex::new(text);
230 assert_eq!(
231 index.line_col(0.into()),
232 LineCol {
233 line: 0,
234 col_utf16: 0
235 }
236 );
237 assert_eq!(
238 index.line_col(1.into()),
239 LineCol {
240 line: 1,
241 col_utf16: 0
242 }
243 );
244 assert_eq!(
245 index.line_col(2.into()),
246 LineCol {
247 line: 1,
248 col_utf16: 1
249 }
250 );
251 assert_eq!(
252 index.line_col(6.into()),
253 LineCol {
254 line: 1,
255 col_utf16: 5
256 }
257 );
258 assert_eq!(
259 index.line_col(7.into()),
260 LineCol {
261 line: 2,
262 col_utf16: 0
263 }
264 );
265 }
266
267 fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> {
268 arb_text()
269 .prop_flat_map(|text| (arb_offset(&text), Just(text)))
270 .boxed()
271 }
272
273 fn to_line_col(text: &str, offset: TextUnit) -> LineCol {
274 let mut res = LineCol {
198 line: 0, 275 line: 0,
199 col_utf16: 0 276 col_utf16: 0,
200 } 277 };
201 ); 278 for (i, c) in text.char_indices() {
202 assert_eq!( 279 if i + c.len_utf8() > offset.to_usize() {
203 index.line_col(1.into()), 280 // if it's an invalid offset, inside a multibyte char
204 LineCol { 281 // return as if it was at the start of the char
205 line: 1, 282 break;
206 col_utf16: 0 283 }
207 } 284 if c == '\n' {
208 ); 285 res.line += 1;
209 assert_eq!( 286 res.col_utf16 = 0;
210 index.line_col(2.into()), 287 } else {
211 LineCol { 288 res.col_utf16 += 1;
212 line: 1, 289 }
213 col_utf16: 1
214 }
215 );
216 assert_eq!(
217 index.line_col(6.into()),
218 LineCol {
219 line: 1,
220 col_utf16: 5
221 } 290 }
222 ); 291 res
223 assert_eq!( 292 }
224 index.line_col(7.into()), 293
225 LineCol { 294 proptest! {
226 line: 2, 295 #[test]
227 col_utf16: 0 296 fn test_line_index_proptest((offset, text) in arb_text_with_offset()) {
297 let expected = to_line_col(&text, offset);
298 let line_index = LineIndex::new(&text);
299 let actual = line_index.line_col(offset);
300
301 assert_eq!(actual, expected);
228 } 302 }
229 ); 303 }
230} 304}
231 305
232#[cfg(test)] 306#[cfg(test)]
@@ -321,4 +395,5 @@ const C: char = \"メ メ\";
321 395
322 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); 396 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15));
323 } 397 }
398
324} 399}
diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs
new file mode 100644
index 000000000..ec3269bbb
--- /dev/null
+++ b/crates/ra_editor/src/line_index_utils.rs
@@ -0,0 +1,363 @@
1use ra_text_edit::{AtomTextEdit, TextEdit};
2use ra_syntax::{TextUnit, TextRange};
3use crate::{LineIndex, LineCol, line_index::Utf16Char};
4
5#[derive(Debug, Clone)]
6enum Step {
7 Newline(TextUnit),
8 Utf16Char(TextRange),
9}
10
11#[derive(Debug)]
12struct LineIndexStepIter<'a> {
13 line_index: &'a LineIndex,
14 next_newline_idx: usize,
15 utf16_chars: Option<(TextUnit, std::slice::Iter<'a, Utf16Char>)>,
16}
17
18impl<'a> LineIndexStepIter<'a> {
19 fn from(line_index: &LineIndex) -> LineIndexStepIter {
20 let mut x = LineIndexStepIter {
21 line_index,
22 next_newline_idx: 0,
23 utf16_chars: None,
24 };
25 // skip first newline since it's not real
26 x.next();
27 x
28 }
29}
30
31impl<'a> Iterator for LineIndexStepIter<'a> {
32 type Item = Step;
33 fn next(&mut self) -> Option<Step> {
34 self.utf16_chars
35 .as_mut()
36 .and_then(|(newline, x)| {
37 let x = x.next()?;
38 Some(Step::Utf16Char(TextRange::from_to(
39 *newline + x.start,
40 *newline + x.end,
41 )))
42 })
43 .or_else(|| {
44 let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?;
45 self.utf16_chars = self
46 .line_index
47 .utf16_lines
48 .get(&(self.next_newline_idx as u32))
49 .map(|x| (next_newline, x.iter()));
50 self.next_newline_idx += 1;
51 Some(Step::Newline(next_newline))
52 })
53 }
54}
55
56#[derive(Debug)]
57struct OffsetStepIter<'a> {
58 text: &'a str,
59 offset: TextUnit,
60}
61
62impl<'a> Iterator for OffsetStepIter<'a> {
63 type Item = Step;
64 fn next(&mut self) -> Option<Step> {
65 let (next, next_offset) = self
66 .text
67 .char_indices()
68 .filter_map(|(i, c)| {
69 if c == '\n' {
70 let next_offset = self.offset + TextUnit::from_usize(i + 1);
71 let next = Step::Newline(next_offset);
72 Some((next, next_offset))
73 } else {
74 let char_len = TextUnit::of_char(c);
75 if char_len.to_usize() > 1 {
76 let start = self.offset + TextUnit::from_usize(i);
77 let end = start + char_len;
78 let next = Step::Utf16Char(TextRange::from_to(start, end));
79 let next_offset = end;
80 Some((next, next_offset))
81 } else {
82 None
83 }
84 }
85 })
86 .next()?;
87 let next_idx = (next_offset - self.offset).to_usize();
88 self.text = &self.text[next_idx..];
89 self.offset = next_offset;
90 Some(next)
91 }
92}
93
94#[derive(Debug)]
95enum NextSteps<'a> {
96 Use,
97 ReplaceMany(OffsetStepIter<'a>),
98 AddMany(OffsetStepIter<'a>),
99}
100
101#[derive(Debug)]
102struct TranslatedEdit<'a> {
103 delete: TextRange,
104 insert: &'a str,
105 diff: i64,
106}
107
108struct Edits<'a> {
109 edits: &'a [AtomTextEdit],
110 current: Option<TranslatedEdit<'a>>,
111 acc_diff: i64,
112}
113
114impl<'a> Edits<'a> {
115 fn from_text_edit(text_edit: &'a TextEdit) -> Edits<'a> {
116 let mut x = Edits {
117 edits: text_edit.as_atoms(),
118 current: None,
119 acc_diff: 0,
120 };
121 x.advance_edit();
122 x
123 }
124 fn advance_edit(&mut self) {
125 self.acc_diff += self.current.as_ref().map_or(0, |x| x.diff);
126 match self.edits.split_first() {
127 Some((next, rest)) => {
128 let delete = self.translate_range(next.delete);
129 let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64;
130 self.current = Some(TranslatedEdit {
131 delete,
132 insert: &next.insert,
133 diff,
134 });
135 self.edits = rest;
136 }
137 None => {
138 self.current = None;
139 }
140 }
141 }
142
143 fn next_inserted_steps(&mut self) -> Option<OffsetStepIter<'a>> {
144 let cur = self.current.as_ref()?;
145 let res = Some(OffsetStepIter {
146 offset: cur.delete.start(),
147 text: &cur.insert,
148 });
149 self.advance_edit();
150 res
151 }
152
153 fn next_steps(&mut self, step: &Step) -> NextSteps {
154 let step_pos = match step {
155 &Step::Newline(n) => n,
156 &Step::Utf16Char(r) => r.end(),
157 };
158 let res = match &mut self.current {
159 Some(edit) => {
160 if step_pos <= edit.delete.start() {
161 NextSteps::Use
162 } else if step_pos <= edit.delete.end() {
163 let iter = OffsetStepIter {
164 offset: edit.delete.start(),
165 text: &edit.insert,
166 };
167 // empty slice to avoid returning steps again
168 edit.insert = &edit.insert[edit.insert.len()..];
169 NextSteps::ReplaceMany(iter)
170 } else {
171 let iter = OffsetStepIter {
172 offset: edit.delete.start(),
173 text: &edit.insert,
174 };
175 // empty slice to avoid returning steps again
176 edit.insert = &edit.insert[edit.insert.len()..];
177 self.advance_edit();
178 NextSteps::AddMany(iter)
179 }
180 }
181 None => NextSteps::Use,
182 };
183 res
184 }
185
186 fn translate_range(&self, range: TextRange) -> TextRange {
187 if self.acc_diff == 0 {
188 range
189 } else {
190 let start = self.translate(range.start());
191 let end = self.translate(range.end());
192 TextRange::from_to(start, end)
193 }
194 }
195
196 fn translate(&self, x: TextUnit) -> TextUnit {
197 if self.acc_diff == 0 {
198 x
199 } else {
200 TextUnit::from((x.to_usize() as i64 + self.acc_diff) as u32)
201 }
202 }
203
204 fn translate_step(&self, x: &Step) -> Step {
205 if self.acc_diff == 0 {
206 x.clone()
207 } else {
208 match x {
209 &Step::Newline(n) => Step::Newline(self.translate(n)),
210 &Step::Utf16Char(r) => Step::Utf16Char(self.translate_range(r)),
211 }
212 }
213 }
214}
215
216#[derive(Debug)]
217struct RunningLineCol {
218 line: u32,
219 last_newline: TextUnit,
220 col_adjust: TextUnit,
221}
222
223impl RunningLineCol {
224 fn new() -> RunningLineCol {
225 RunningLineCol {
226 line: 0,
227 last_newline: TextUnit::from(0),
228 col_adjust: TextUnit::from(0),
229 }
230 }
231
232 fn to_line_col(&self, offset: TextUnit) -> LineCol {
233 LineCol {
234 line: self.line,
235 col_utf16: ((offset - self.last_newline) - self.col_adjust).into(),
236 }
237 }
238
239 fn add_line(&mut self, newline: TextUnit) {
240 self.line += 1;
241 self.last_newline = newline;
242 self.col_adjust = TextUnit::from(0);
243 }
244
245 fn adjust_col(&mut self, range: &TextRange) {
246 self.col_adjust += range.len() - TextUnit::from(1);
247 }
248}
249
250pub fn translate_offset_with_edit(
251 line_index: &LineIndex,
252 offset: TextUnit,
253 text_edit: &TextEdit,
254) -> LineCol {
255 let mut state = Edits::from_text_edit(&text_edit);
256
257 let mut res = RunningLineCol::new();
258
259 macro_rules! test_step {
260 ($x:ident) => {
261 match &$x {
262 Step::Newline(n) => {
263 if offset < *n {
264 return res.to_line_col(offset);
265 } else {
266 res.add_line(*n);
267 }
268 }
269 Step::Utf16Char(x) => {
270 if offset < x.end() {
271 // if the offset is inside a multibyte char it's invalid
272 // clamp it to the start of the char
273 let clamp = offset.min(x.start());
274 return res.to_line_col(clamp);
275 } else {
276 res.adjust_col(x);
277 }
278 }
279 }
280 };
281 }
282
283 for orig_step in LineIndexStepIter::from(line_index) {
284 loop {
285 let translated_step = state.translate_step(&orig_step);
286 match state.next_steps(&translated_step) {
287 NextSteps::Use => {
288 test_step!(translated_step);
289 break;
290 }
291 NextSteps::ReplaceMany(ns) => {
292 for n in ns {
293 test_step!(n);
294 }
295 break;
296 }
297 NextSteps::AddMany(ns) => {
298 for n in ns {
299 test_step!(n);
300 }
301 }
302 }
303 }
304 }
305
306 loop {
307 match state.next_inserted_steps() {
308 None => break,
309 Some(ns) => {
310 for n in ns {
311 test_step!(n);
312 }
313 }
314 }
315 }
316
317 res.to_line_col(offset)
318}
319
320#[cfg(test)]
321mod test {
322 use super::*;
323 use proptest::{prelude::*, proptest, proptest_helper};
324 use crate::line_index;
325 use ra_text_edit::test_utils::{arb_offset, arb_text_with_edit};
326 use ra_text_edit::TextEdit;
327
328 #[derive(Debug)]
329 struct ArbTextWithEditAndOffset {
330 text: String,
331 edit: TextEdit,
332 edited_text: String,
333 offset: TextUnit,
334 }
335
336 fn arb_text_with_edit_and_offset() -> BoxedStrategy<ArbTextWithEditAndOffset> {
337 arb_text_with_edit()
338 .prop_flat_map(|x| {
339 let edited_text = x.edit.apply(&x.text);
340 let arb_offset = arb_offset(&edited_text);
341 (Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| {
342 ArbTextWithEditAndOffset {
343 text: x.text,
344 edit: x.edit,
345 edited_text,
346 offset,
347 }
348 })
349 })
350 .boxed()
351 }
352
353 proptest! {
354 #[test]
355 fn test_translate_offset_with_edit(x in arb_text_with_edit_and_offset()) {
356 let expected = line_index::to_line_col(&x.edited_text, x.offset);
357 let line_index = LineIndex::new(&x.text);
358 let actual = translate_offset_with_edit(&line_index, x.offset, &x.edit);
359
360 assert_eq!(actual, expected);
361 }
362 }
363}
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs
index c0e4e3a36..d3670104e 100644
--- a/crates/ra_lsp_server/src/conv.rs
+++ b/crates/ra_lsp_server/src/conv.rs
@@ -3,7 +3,7 @@ use languageserver_types::{
3 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat, 3 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat,
4}; 4};
5use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText}; 5use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText};
6use ra_editor::{LineCol, LineIndex}; 6use ra_editor::{LineCol, LineIndex, translate_offset_with_edit};
7use ra_text_edit::{AtomTextEdit, TextEdit}; 7use ra_text_edit::{AtomTextEdit, TextEdit};
8use ra_syntax::{SyntaxKind, TextRange, TextUnit}; 8use ra_syntax::{SyntaxKind, TextRange, TextUnit};
9 9
@@ -238,13 +238,15 @@ impl TryConvWith for SourceChange {
238 None => None, 238 None => None,
239 Some(pos) => { 239 Some(pos) => {
240 let line_index = world.analysis().file_line_index(pos.file_id); 240 let line_index = world.analysis().file_line_index(pos.file_id);
241 let edits = self 241 let edit = self
242 .source_file_edits 242 .source_file_edits
243 .iter() 243 .iter()
244 .find(|it| it.file_id == pos.file_id) 244 .find(|it| it.file_id == pos.file_id)
245 .map(|it| it.edit.as_atoms()) 245 .map(|it| &it.edit);
246 .unwrap_or(&[]); 246 let line_col = match edit {
247 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits); 247 Some(edit) => translate_offset_with_edit(&*line_index, pos.offset, edit),
248 None => line_index.line_col(pos.offset),
249 };
248 let position = 250 let position =
249 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16)); 251 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16));
250 Some(TextDocumentPositionParams { 252 Some(TextDocumentPositionParams {
@@ -264,41 +266,6 @@ impl TryConvWith for SourceChange {
264 } 266 }
265} 267}
266 268
267// HACK: we should translate offset to line/column using linde_index *with edits applied*.
268// A naive version of this function would be to apply `edits` to the original text,
269// construct a new line index and use that, but it would be slow.
270//
271// Writing fast & correct version is issue #105, let's use a quick hack in the meantime
272fn translate_offset_with_edit(
273 pre_edit_index: &LineIndex,
274 offset: TextUnit,
275 edits: &[AtomTextEdit],
276) -> LineCol {
277 let fallback = pre_edit_index.line_col(offset);
278 let edit = match edits.first() {
279 None => return fallback,
280 Some(edit) => edit,
281 };
282 let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert);
283 if !(edit.delete.start() <= offset && offset <= end_offset) {
284 return fallback;
285 }
286 let rel_offset = offset - edit.delete.start();
287 let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset);
288 let edit_line_col = pre_edit_index.line_col(edit.delete.start());
289 if in_edit_line_col.line == 0 {
290 LineCol {
291 line: edit_line_col.line,
292 col_utf16: edit_line_col.col_utf16 + in_edit_line_col.col_utf16,
293 }
294 } else {
295 LineCol {
296 line: edit_line_col.line + in_edit_line_col.line,
297 col_utf16: in_edit_line_col.col_utf16,
298 }
299 }
300}
301
302impl TryConvWith for SourceFileEdit { 269impl TryConvWith for SourceFileEdit {
303 type Ctx = ServerWorld; 270 type Ctx = ServerWorld;
304 type Output = TextDocumentEdit; 271 type Output = TextDocumentEdit;
diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml
index 3c4157a4e..e0db49688 100644
--- a/crates/ra_text_edit/Cargo.toml
+++ b/crates/ra_text_edit/Cargo.toml
@@ -7,6 +7,7 @@ publish = false
7 7
8[dependencies] 8[dependencies]
9text_unit = "0.1.5" 9text_unit = "0.1.5"
10proptest = "0.8.7"
10 11
11[dev-dependencies] 12[dev-dependencies]
12test_utils = { path = "../test_utils" } 13test_utils = { path = "../test_utils" }
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs
index 13e20f6fb..8acf10448 100644
--- a/crates/ra_text_edit/src/lib.rs
+++ b/crates/ra_text_edit/src/lib.rs
@@ -1,12 +1,15 @@
1mod text_edit; 1mod text_edit;
2pub mod text_utils; 2pub mod text_utils;
3pub mod test_utils;
3 4
4pub use crate::text_edit::{TextEdit, TextEditBuilder}; 5pub use crate::text_edit::{TextEdit, TextEditBuilder};
5 6
6use text_unit::{TextRange, TextUnit}; 7use text_unit::{TextRange, TextUnit};
7 8
9/// Must not overlap with other `AtomTextEdit`s
8#[derive(Debug, Clone)] 10#[derive(Debug, Clone)]
9pub struct AtomTextEdit { 11pub struct AtomTextEdit {
12 /// Refers to offsets in the original text
10 pub delete: TextRange, 13 pub delete: TextRange,
11 pub insert: String, 14 pub insert: String,
12} 15}
diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs
new file mode 100644
index 000000000..745f21c93
--- /dev/null
+++ b/crates/ra_text_edit/src/test_utils.rs
@@ -0,0 +1,85 @@
1use proptest::prelude::*;
2use text_unit::{TextUnit, TextRange};
3use crate::{AtomTextEdit, TextEdit};
4
5pub fn arb_text() -> proptest::string::RegexGeneratorStrategy<String> {
6 // generate multiple newlines
7 proptest::string::string_regex("(.*\n?)*").unwrap()
8}
9
10fn text_offsets(text: &str) -> Vec<TextUnit> {
11 text.char_indices()
12 .map(|(i, _)| TextUnit::from_usize(i))
13 .collect()
14}
15
16pub fn arb_offset(text: &str) -> BoxedStrategy<TextUnit> {
17 let offsets = text_offsets(text);
18 // this is necessary to avoid "Uniform::new called with `low >= high`" panic
19 if offsets.is_empty() {
20 Just(TextUnit::from(0)).boxed()
21 } else {
22 prop::sample::select(offsets).boxed()
23 }
24}
25
26pub fn arb_text_edit(text: &str) -> BoxedStrategy<TextEdit> {
27 if text.is_empty() {
28 // only valid edits
29 return Just(vec![])
30 .boxed()
31 .prop_union(
32 arb_text()
33 .prop_map(|text| vec![AtomTextEdit::insert(TextUnit::from(0), text)])
34 .boxed(),
35 )
36 .prop_map(TextEdit::from_atoms)
37 .boxed();
38 }
39
40 let offsets = text_offsets(text);
41 let max_cuts = 7.min(offsets.len());
42
43 proptest::sample::subsequence(offsets, 0..max_cuts)
44 .prop_flat_map(|cuts| {
45 let strategies: Vec<_> = cuts
46 .chunks(2)
47 .map(|chunk| match chunk {
48 &[from, to] => {
49 let range = TextRange::from_to(from, to);
50 Just(AtomTextEdit::delete(range))
51 .boxed()
52 .prop_union(
53 arb_text()
54 .prop_map(move |text| AtomTextEdit::replace(range, text))
55 .boxed(),
56 )
57 .boxed()
58 }
59 &[x] => arb_text()
60 .prop_map(move |text| AtomTextEdit::insert(x, text))
61 .boxed(),
62 _ => unreachable!(),
63 })
64 .collect();
65 strategies
66 })
67 .prop_map(TextEdit::from_atoms)
68 .boxed()
69}
70
71#[derive(Debug, Clone)]
72pub struct ArbTextWithEdit {
73 pub text: String,
74 pub edit: TextEdit,
75}
76
77pub fn arb_text_with_edit() -> BoxedStrategy<ArbTextWithEdit> {
78 let text = arb_text();
79 text.prop_flat_map(|s| {
80 let edit = arb_text_edit(&s);
81 (Just(s), edit)
82 })
83 .prop_map(|(text, edit)| ArbTextWithEdit { text, edit })
84 .boxed()
85}
diff --git a/crates/ra_text_edit/src/text_edit.rs b/crates/ra_text_edit/src/text_edit.rs
index 392968d63..0881f3e1c 100644
--- a/crates/ra_text_edit/src/text_edit.rs
+++ b/crates/ra_text_edit/src/text_edit.rs
@@ -26,12 +26,7 @@ impl TextEditBuilder {
26 self.atoms.push(AtomTextEdit::insert(offset, text)) 26 self.atoms.push(AtomTextEdit::insert(offset, text))
27 } 27 }
28 pub fn finish(self) -> TextEdit { 28 pub fn finish(self) -> TextEdit {
29 let mut atoms = self.atoms; 29 TextEdit::from_atoms(self.atoms)
30 atoms.sort_by_key(|a| (a.delete.start(), a.delete.end()));
31 for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
32 assert!(a1.delete.end() <= a2.delete.start())
33 }
34 TextEdit { atoms }
35 } 30 }
36 pub fn invalidates_offset(&self, offset: TextUnit) -> bool { 31 pub fn invalidates_offset(&self, offset: TextUnit) -> bool {
37 self.atoms 32 self.atoms
@@ -41,6 +36,14 @@ impl TextEditBuilder {
41} 36}
42 37
43impl TextEdit { 38impl TextEdit {
39 pub(crate) fn from_atoms(mut atoms: Vec<AtomTextEdit>) -> TextEdit {
40 atoms.sort_by_key(|a| (a.delete.start(), a.delete.end()));
41 for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
42 assert!(a1.delete.end() <= a2.delete.start())
43 }
44 TextEdit { atoms }
45 }
46
44 pub fn as_atoms(&self) -> &[AtomTextEdit] { 47 pub fn as_atoms(&self) -> &[AtomTextEdit] {
45 &self.atoms 48 &self.atoms
46 } 49 }