diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-12 07:52:38 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-12 07:52:38 +0000 |
commit | 0156a538089828340a823ed02da8970bf4f1175b (patch) | |
tree | 07c4eacaad717ea802ab26972f45223281f2c9c1 /crates/ra_text_edit/src/text_edit.rs | |
parent | f655f993fe6d9faa81b0e776b9b24308d2ea1c68 (diff) | |
parent | 0527e3b283af0153cf13fa64fe73862a5b7655c8 (diff) |
Merge #276
276: Extract and rename AtomEdit and Edit into ra_text_edit r=matklad a=vemoo
As discused in #105
Co-authored-by: Bernardo <[email protected]>
Diffstat (limited to 'crates/ra_text_edit/src/text_edit.rs')
-rw-r--r-- | crates/ra_text_edit/src/text_edit.rs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/crates/ra_text_edit/src/text_edit.rs b/crates/ra_text_edit/src/text_edit.rs new file mode 100644 index 000000000..fb46f046d --- /dev/null +++ b/crates/ra_text_edit/src/text_edit.rs | |||
@@ -0,0 +1,84 @@ | |||
1 | use crate::AtomTextEdit; | ||
2 | use crate::text_utils::contains_offset_nonstrict; | ||
3 | use text_unit::{TextRange, TextUnit}; | ||
4 | |||
5 | #[derive(Debug, Clone)] | ||
6 | pub struct TextEdit { | ||
7 | atoms: Vec<AtomTextEdit>, | ||
8 | } | ||
9 | |||
10 | #[derive(Debug)] | ||
11 | pub struct TextEditBuilder { | ||
12 | atoms: Vec<AtomTextEdit>, | ||
13 | } | ||
14 | |||
15 | impl TextEditBuilder { | ||
16 | pub fn new() -> TextEditBuilder { | ||
17 | TextEditBuilder { atoms: Vec::new() } | ||
18 | } | ||
19 | pub fn replace(&mut self, range: TextRange, replace_with: String) { | ||
20 | self.atoms.push(AtomTextEdit::replace(range, replace_with)) | ||
21 | } | ||
22 | pub fn delete(&mut self, range: TextRange) { | ||
23 | self.atoms.push(AtomTextEdit::delete(range)) | ||
24 | } | ||
25 | pub fn insert(&mut self, offset: TextUnit, text: String) { | ||
26 | self.atoms.push(AtomTextEdit::insert(offset, text)) | ||
27 | } | ||
28 | pub fn finish(self) -> TextEdit { | ||
29 | let mut 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 | } | ||
36 | pub fn invalidates_offset(&self, offset: TextUnit) -> bool { | ||
37 | self.atoms | ||
38 | .iter() | ||
39 | .any(|atom| contains_offset_nonstrict(atom.delete, offset)) | ||
40 | } | ||
41 | } | ||
42 | |||
43 | impl TextEdit { | ||
44 | pub fn into_atoms(self) -> Vec<AtomTextEdit> { | ||
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 | } | ||