aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_text_edit
diff options
context:
space:
mode:
authorBernardo <[email protected]>2018-12-10 21:09:12 +0000
committerBernardo <[email protected]>2018-12-10 21:09:12 +0000
commit7344d28768c43d8955bf23c183d606be08f27c64 (patch)
treef0773b4db640dac303f1f683115c780ac3eb0b9c /crates/ra_text_edit
parentf655f993fe6d9faa81b0e776b9b24308d2ea1c68 (diff)
extract AtomEdit and Edit into new ra_text_edit crate
Diffstat (limited to 'crates/ra_text_edit')
-rw-r--r--crates/ra_text_edit/Cargo.toml12
-rw-r--r--crates/ra_text_edit/src/edit.rs84
-rw-r--r--crates/ra_text_edit/src/lib.rs29
-rw-r--r--crates/ra_text_edit/src/text_utils.rs5
4 files changed, 130 insertions, 0 deletions
diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml
new file mode 100644
index 000000000..3c4157a4e
--- /dev/null
+++ b/crates/ra_text_edit/Cargo.toml
@@ -0,0 +1,12 @@
1[package]
2edition = "2018"
3name = "ra_text_edit"
4version = "0.1.0"
5authors = ["Aleksey Kladov <[email protected]>"]
6publish = false
7
8[dependencies]
9text_unit = "0.1.5"
10
11[dev-dependencies]
12test_utils = { path = "../test_utils" }
diff --git a/crates/ra_text_edit/src/edit.rs b/crates/ra_text_edit/src/edit.rs
new file mode 100644
index 000000000..560cf2bbc
--- /dev/null
+++ b/crates/ra_text_edit/src/edit.rs
@@ -0,0 +1,84 @@
1use crate::AtomEdit;
2use crate::text_utils::contains_offset_nonstrict;
3use text_unit::{TextRange, TextUnit};
4
5#[derive(Debug, Clone)]
6pub struct Edit {
7 atoms: Vec<AtomEdit>,
8}
9
10#[derive(Debug)]
11pub struct EditBuilder {
12 atoms: Vec<AtomEdit>,
13}
14
15impl EditBuilder {
16 pub fn new() -> EditBuilder {
17 EditBuilder { atoms: Vec::new() }
18 }
19 pub fn replace(&mut self, range: TextRange, replace_with: String) {
20 self.atoms.push(AtomEdit::replace(range, replace_with))
21 }
22 pub fn delete(&mut self, range: TextRange) {
23 self.atoms.push(AtomEdit::delete(range))
24 }
25 pub fn insert(&mut self, offset: TextUnit, text: String) {
26 self.atoms.push(AtomEdit::insert(offset, text))
27 }
28 pub fn finish(self) -> Edit {
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 Edit { 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
43impl 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}
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs
new file mode 100644
index 000000000..789471e8a
--- /dev/null
+++ b/crates/ra_text_edit/src/lib.rs
@@ -0,0 +1,29 @@
1mod edit;
2pub mod text_utils;
3
4pub use crate::edit::{Edit, EditBuilder};
5
6use text_unit::{TextRange, TextUnit};
7
8#[derive(Debug, Clone)]
9pub struct AtomEdit {
10 pub delete: TextRange,
11 pub insert: String,
12}
13
14impl AtomEdit {
15 pub fn replace(range: TextRange, replace_with: String) -> AtomEdit {
16 AtomEdit {
17 delete: range,
18 insert: replace_with,
19 }
20 }
21
22 pub fn delete(range: TextRange) -> AtomEdit {
23 AtomEdit::replace(range, String::new())
24 }
25
26 pub fn insert(offset: TextUnit, text: String) -> AtomEdit {
27 AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text)
28 }
29}
diff --git a/crates/ra_text_edit/src/text_utils.rs b/crates/ra_text_edit/src/text_utils.rs
new file mode 100644
index 000000000..e3b4dc4fe
--- /dev/null
+++ b/crates/ra_text_edit/src/text_utils.rs
@@ -0,0 +1,5 @@
1use text_unit::{TextRange, TextUnit};
2
3pub fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool {
4 range.start() <= offset && offset <= range.end()
5}