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