aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_text_edit/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-05 22:23:29 +0100
committerAleksey Kladov <[email protected]>2020-05-05 22:23:29 +0100
commit27c7ef6d65ffa6a642768377d3f0ba85ac8564bf (patch)
treeeb205f315338931e2a8ea6da63d9ea0ff06199c2 /crates/ra_text_edit/src
parent4a6fa8f0dfcebbb4ea80394e5e4ca21f076f58f2 (diff)
Use more natural signature for Edit::apply
Diffstat (limited to 'crates/ra_text_edit/src')
-rw-r--r--crates/ra_text_edit/src/lib.rs21
1 files changed, 16 insertions, 5 deletions
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs
index c41bf324b..7138bbc65 100644
--- a/crates/ra_text_edit/src/lib.rs
+++ b/crates/ra_text_edit/src/lib.rs
@@ -37,11 +37,10 @@ impl Indel {
37 Indel { delete: range, insert: replace_with } 37 Indel { delete: range, insert: replace_with }
38 } 38 }
39 39
40 pub fn apply(&self, mut text: String) -> String { 40 pub fn apply(&self, text: &mut String) {
41 let start: usize = self.delete.start().into(); 41 let start: usize = self.delete.start().into();
42 let end: usize = self.delete.end().into(); 42 let end: usize = self.delete.end().into();
43 text.replace_range(start..end, &self.insert); 43 text.replace_range(start..end, &self.insert);
44 text
45 } 44 }
46} 45}
47 46
@@ -76,8 +75,17 @@ impl TextEdit {
76 &self.indels 75 &self.indels
77 } 76 }
78 77
79 pub fn apply(&self, text: &str) -> String { 78 pub fn apply(&self, text: &mut String) {
80 let mut total_len = TextSize::of(text); 79 match self.indels.len() {
80 0 => return,
81 1 => {
82 self.indels[0].apply(text);
83 return;
84 }
85 _ => (),
86 }
87
88 let mut total_len = TextSize::of(&*text);
81 for indel in self.indels.iter() { 89 for indel in self.indels.iter() {
82 total_len += TextSize::of(&indel.insert); 90 total_len += TextSize::of(&indel.insert);
83 total_len -= indel.delete.end() - indel.delete.start(); 91 total_len -= indel.delete.end() - indel.delete.start();
@@ -95,7 +103,10 @@ impl TextEdit {
95 } 103 }
96 buf.push_str(&text[prev..text.len()]); 104 buf.push_str(&text[prev..text.len()]);
97 assert_eq!(TextSize::of(&buf), total_len); 105 assert_eq!(TextSize::of(&buf), total_len);
98 buf 106
107 // FIXME: figure out a way to mutate the text in-place or reuse the
108 // memory in some other way
109 *text = buf
99 } 110 }
100 111
101 pub fn apply_to_offset(&self, offset: TextSize) -> Option<TextSize> { 112 pub fn apply_to_offset(&self, offset: TextSize) -> Option<TextSize> {