aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_text_edit/src/text_edit.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_text_edit/src/text_edit.rs')
-rw-r--r--crates/ra_text_edit/src/text_edit.rs25
1 files changed, 13 insertions, 12 deletions
diff --git a/crates/ra_text_edit/src/text_edit.rs b/crates/ra_text_edit/src/text_edit.rs
index 5c37a08a8..eabab4b4d 100644
--- a/crates/ra_text_edit/src/text_edit.rs
+++ b/crates/ra_text_edit/src/text_edit.rs
@@ -1,7 +1,8 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use crate::AtomTextEdit; 3use crate::AtomTextEdit;
4use text_unit::{TextRange, TextUnit}; 4
5use text_size::{TextRange, TextSize};
5 6
6#[derive(Debug, Clone)] 7#[derive(Debug, Clone)]
7pub struct TextEdit { 8pub struct TextEdit {
@@ -20,19 +21,19 @@ impl TextEditBuilder {
20 pub fn delete(&mut self, range: TextRange) { 21 pub fn delete(&mut self, range: TextRange) {
21 self.atoms.push(AtomTextEdit::delete(range)) 22 self.atoms.push(AtomTextEdit::delete(range))
22 } 23 }
23 pub fn insert(&mut self, offset: TextUnit, text: String) { 24 pub fn insert(&mut self, offset: TextSize, text: String) {
24 self.atoms.push(AtomTextEdit::insert(offset, text)) 25 self.atoms.push(AtomTextEdit::insert(offset, text))
25 } 26 }
26 pub fn finish(self) -> TextEdit { 27 pub fn finish(self) -> TextEdit {
27 TextEdit::from_atoms(self.atoms) 28 TextEdit::from_atoms(self.atoms)
28 } 29 }
29 pub fn invalidates_offset(&self, offset: TextUnit) -> bool { 30 pub fn invalidates_offset(&self, offset: TextSize) -> bool {
30 self.atoms.iter().any(|atom| atom.delete.contains_inclusive(offset)) 31 self.atoms.iter().any(|atom| atom.delete.contains_inclusive(offset))
31 } 32 }
32} 33}
33 34
34impl TextEdit { 35impl TextEdit {
35 pub fn insert(offset: TextUnit, text: String) -> TextEdit { 36 pub fn insert(offset: TextSize, text: String) -> TextEdit {
36 let mut builder = TextEditBuilder::default(); 37 let mut builder = TextEditBuilder::default();
37 builder.insert(offset, text); 38 builder.insert(offset, text);
38 builder.finish() 39 builder.finish()
@@ -63,16 +64,16 @@ impl TextEdit {
63 } 64 }
64 65
65 pub fn apply(&self, text: &str) -> String { 66 pub fn apply(&self, text: &str) -> String {
66 let mut total_len = TextUnit::of_str(text); 67 let mut total_len = TextSize::of(text);
67 for atom in self.atoms.iter() { 68 for atom in self.atoms.iter() {
68 total_len += TextUnit::of_str(&atom.insert); 69 total_len += TextSize::of(&atom.insert);
69 total_len -= atom.delete.end() - atom.delete.start(); 70 total_len -= atom.delete.end() - atom.delete.start();
70 } 71 }
71 let mut buf = String::with_capacity(total_len.to_usize()); 72 let mut buf = String::with_capacity(total_len.into());
72 let mut prev = 0; 73 let mut prev = 0;
73 for atom in self.atoms.iter() { 74 for atom in self.atoms.iter() {
74 let start = atom.delete.start().to_usize(); 75 let start: usize = atom.delete.start().into();
75 let end = atom.delete.end().to_usize(); 76 let end: usize = atom.delete.end().into();
76 if start > prev { 77 if start > prev {
77 buf.push_str(&text[prev..start]); 78 buf.push_str(&text[prev..start]);
78 } 79 }
@@ -80,11 +81,11 @@ impl TextEdit {
80 prev = end; 81 prev = end;
81 } 82 }
82 buf.push_str(&text[prev..text.len()]); 83 buf.push_str(&text[prev..text.len()]);
83 assert_eq!(TextUnit::of_str(&buf), total_len); 84 assert_eq!(TextSize::of(&buf), total_len);
84 buf 85 buf
85 } 86 }
86 87
87 pub fn apply_to_offset(&self, offset: TextUnit) -> Option<TextUnit> { 88 pub fn apply_to_offset(&self, offset: TextSize) -> Option<TextSize> {
88 let mut res = offset; 89 let mut res = offset;
89 for atom in self.atoms.iter() { 90 for atom in self.atoms.iter() {
90 if atom.delete.start() >= offset { 91 if atom.delete.start() >= offset {
@@ -93,7 +94,7 @@ impl TextEdit {
93 if offset < atom.delete.end() { 94 if offset < atom.delete.end() {
94 return None; 95 return None;
95 } 96 }
96 res += TextUnit::of_str(&atom.insert); 97 res += TextSize::of(&atom.insert);
97 res -= atom.delete.len(); 98 res -= atom.delete.len();
98 } 99 }
99 Some(res) 100 Some(res)