Insert at cursor, or replace selection.
(&mut self, s: &str, cx: &Context)
| 87 | |
| 88 | /// Insert at cursor, or replace selection. |
| 89 | pub fn insert_or_replace_selection(&mut self, s: &str, cx: &Context) { |
| 90 | let range = self.selection.text_range(); |
| 91 | let start = range.start; |
| 92 | if self.selection.is_collapsed() { |
| 93 | self.buffer.insert_str(start, s); |
| 94 | } else { |
| 95 | self.buffer.replace_range(range.clone(), s); |
| 96 | } |
| 97 | self.update_layout(cx); |
| 98 | |
| 99 | let new_index = start.saturating_add(s.len()); |
| 100 | let affinity = if s.ends_with("\n") { |
| 101 | Affinity::Downstream |
| 102 | } else { |
| 103 | Affinity::Upstream |
| 104 | }; |
| 105 | self.set_selection(Cursor::from_byte_index(&self.layout, new_index, affinity).into()); |
| 106 | } |
| 107 | |
| 108 | /// Delete the selection. |
| 109 | pub fn delete_selection(&mut self, cx: &Context) { |
no test coverage detected