aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_text_edit/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-04-24 22:40:41 +0100
committerAleksey Kladov <[email protected]>2020-04-25 10:59:18 +0100
commitb1d5817dd18b7b5fc102a63b084b1ee7ff4f9996 (patch)
treee5d136c5ba4a6ba96aeeb423e6e3f64ca7cea3f9 /crates/ra_text_edit/src
parent27a7718880d93f55f905da606d108d3b3c682ab4 (diff)
Convert code to text-size
Diffstat (limited to 'crates/ra_text_edit/src')
-rw-r--r--crates/ra_text_edit/src/lib.rs10
-rw-r--r--crates/ra_text_edit/src/text_edit.rs25
2 files changed, 18 insertions, 17 deletions
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs
index f6769e6a6..e656260c7 100644
--- a/crates/ra_text_edit/src/lib.rs
+++ b/crates/ra_text_edit/src/lib.rs
@@ -2,7 +2,7 @@
2 2
3mod text_edit; 3mod text_edit;
4 4
5use text_unit::{TextRange, TextUnit}; 5use text_size::{TextRange, TextSize};
6 6
7pub use crate::text_edit::{TextEdit, TextEditBuilder}; 7pub use crate::text_edit::{TextEdit, TextEditBuilder};
8 8
@@ -23,13 +23,13 @@ impl AtomTextEdit {
23 AtomTextEdit::replace(range, String::new()) 23 AtomTextEdit::replace(range, String::new())
24 } 24 }
25 25
26 pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit { 26 pub fn insert(offset: TextSize, text: String) -> AtomTextEdit {
27 AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text) 27 AtomTextEdit::replace(TextRange::empty(offset), text)
28 } 28 }
29 29
30 pub fn apply(&self, mut text: String) -> String { 30 pub fn apply(&self, mut text: String) -> String {
31 let start = self.delete.start().to_usize(); 31 let start: usize = self.delete.start().into();
32 let end = self.delete.end().to_usize(); 32 let end: usize = self.delete.end().into();
33 text.replace_range(start..end, &self.insert); 33 text.replace_range(start..end, &self.insert);
34 text 34 text
35 } 35 }
diff --git a/crates/ra_text_edit/src/text_edit.rs b/crates/ra_text_edit/src/text_edit.rs
index 5c37a08a8..db69a7e7b 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// TODO: fix Cargo.toml
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)