diff options
Diffstat (limited to 'crates/ra_editor/src/edit.rs')
-rw-r--r-- | crates/ra_editor/src/edit.rs | 83 |
1 files changed, 0 insertions, 83 deletions
diff --git a/crates/ra_editor/src/edit.rs b/crates/ra_editor/src/edit.rs deleted file mode 100644 index 372b8d14c..000000000 --- a/crates/ra_editor/src/edit.rs +++ /dev/null | |||
@@ -1,83 +0,0 @@ | |||
1 | use crate::{TextRange, TextUnit}; | ||
2 | use ra_syntax::{text_utils::contains_offset_nonstrict, AtomEdit}; | ||
3 | |||
4 | #[derive(Debug, Clone)] | ||
5 | pub struct Edit { | ||
6 | atoms: Vec<AtomEdit>, | ||
7 | } | ||
8 | |||
9 | #[derive(Debug)] | ||
10 | pub struct EditBuilder { | ||
11 | atoms: Vec<AtomEdit>, | ||
12 | } | ||
13 | |||
14 | impl EditBuilder { | ||
15 | pub fn new() -> EditBuilder { | ||
16 | EditBuilder { atoms: Vec::new() } | ||
17 | } | ||
18 | pub fn replace(&mut self, range: TextRange, replace_with: String) { | ||
19 | self.atoms.push(AtomEdit::replace(range, replace_with)) | ||
20 | } | ||
21 | pub fn delete(&mut self, range: TextRange) { | ||
22 | self.atoms.push(AtomEdit::delete(range)) | ||
23 | } | ||
24 | pub fn insert(&mut self, offset: TextUnit, text: String) { | ||
25 | self.atoms.push(AtomEdit::insert(offset, text)) | ||
26 | } | ||
27 | pub fn finish(self) -> Edit { | ||
28 | let mut atoms = self.atoms; | ||
29 | atoms.sort_by_key(|a| (a.delete.start(), a.delete.end())); | ||
30 | for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) { | ||
31 | assert!(a1.delete.end() <= a2.delete.start()) | ||
32 | } | ||
33 | Edit { atoms } | ||
34 | } | ||
35 | pub fn invalidates_offset(&self, offset: TextUnit) -> bool { | ||
36 | self.atoms | ||
37 | .iter() | ||
38 | .any(|atom| contains_offset_nonstrict(atom.delete, offset)) | ||
39 | } | ||
40 | } | ||
41 | |||
42 | impl Edit { | ||
43 | pub fn into_atoms(self) -> Vec<AtomEdit> { | ||
44 | self.atoms | ||
45 | } | ||
46 | |||
47 | pub fn apply(&self, text: &str) -> String { | ||
48 | let mut total_len = text.len(); | ||
49 | for atom in self.atoms.iter() { | ||
50 | total_len += atom.insert.len(); | ||
51 | total_len -= u32::from(atom.delete.end() - atom.delete.start()) as usize; | ||
52 | } | ||
53 | let mut buf = String::with_capacity(total_len); | ||
54 | let mut prev = 0; | ||
55 | for atom in self.atoms.iter() { | ||
56 | let start = u32::from(atom.delete.start()) as usize; | ||
57 | let end = u32::from(atom.delete.end()) as usize; | ||
58 | if start > prev { | ||
59 | buf.push_str(&text[prev..start]); | ||
60 | } | ||
61 | buf.push_str(&atom.insert); | ||
62 | prev = end; | ||
63 | } | ||
64 | buf.push_str(&text[prev..text.len()]); | ||
65 | assert_eq!(buf.len(), total_len); | ||
66 | buf | ||
67 | } | ||
68 | |||
69 | pub fn apply_to_offset(&self, offset: TextUnit) -> Option<TextUnit> { | ||
70 | let mut res = offset; | ||
71 | for atom in self.atoms.iter() { | ||
72 | if atom.delete.start() >= offset { | ||
73 | break; | ||
74 | } | ||
75 | if offset < atom.delete.end() { | ||
76 | return None; | ||
77 | } | ||
78 | res += TextUnit::of_str(&atom.insert); | ||
79 | res -= atom.delete.len(); | ||
80 | } | ||
81 | Some(res) | ||
82 | } | ||
83 | } | ||