aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_text_edit
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-12-12 07:52:38 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-12-12 07:52:38 +0000
commit0156a538089828340a823ed02da8970bf4f1175b (patch)
tree07c4eacaad717ea802ab26972f45223281f2c9c1 /crates/ra_text_edit
parentf655f993fe6d9faa81b0e776b9b24308d2ea1c68 (diff)
parent0527e3b283af0153cf13fa64fe73862a5b7655c8 (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')
-rw-r--r--crates/ra_text_edit/Cargo.toml12
-rw-r--r--crates/ra_text_edit/src/lib.rs29
-rw-r--r--crates/ra_text_edit/src/text_edit.rs84
-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/lib.rs b/crates/ra_text_edit/src/lib.rs
new file mode 100644
index 000000000..13e20f6fb
--- /dev/null
+++ b/crates/ra_text_edit/src/lib.rs
@@ -0,0 +1,29 @@
1mod text_edit;
2pub mod text_utils;
3
4pub use crate::text_edit::{TextEdit, TextEditBuilder};
5
6use text_unit::{TextRange, TextUnit};
7
8#[derive(Debug, Clone)]
9pub struct AtomTextEdit {
10 pub delete: TextRange,
11 pub insert: String,
12}
13
14impl AtomTextEdit {
15 pub fn replace(range: TextRange, replace_with: String) -> AtomTextEdit {
16 AtomTextEdit {
17 delete: range,
18 insert: replace_with,
19 }
20 }
21
22 pub fn delete(range: TextRange) -> AtomTextEdit {
23 AtomTextEdit::replace(range, String::new())
24 }
25
26 pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit {
27 AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text)
28 }
29}
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 @@
1use crate::AtomTextEdit;
2use crate::text_utils::contains_offset_nonstrict;
3use text_unit::{TextRange, TextUnit};
4
5#[derive(Debug, Clone)]
6pub struct TextEdit {
7 atoms: Vec<AtomTextEdit>,
8}
9
10#[derive(Debug)]
11pub struct TextEditBuilder {
12 atoms: Vec<AtomTextEdit>,
13}
14
15impl 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
43impl 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}
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}