diff --git a/src/buffer/clipboard.rs b/src/buffer/clipboard.rs index 4866f40..e406b32 100644 --- a/src/buffer/clipboard.rs +++ b/src/buffer/clipboard.rs @@ -16,25 +16,19 @@ impl Buffer { } } - pub fn paste(&mut self) { - // TODO: Improve performance (add all text instead iterate chars) + pub fn paste_after(&mut self) { if let Some(clipboard) = &self.clipboard { - let mut new_line = false; - for c in clipboard.text.clone().chars() { - match c { - '\n' => { - self.split_line_after(); - new_line = true; - } - ch => { - if new_line && self.cursor.x == 0 { - self.insert_char(ch); - self.move_left(); - new_line = false; - } else { - self.insert_char_after(ch); - } - } + let clipboard_text = clipboard.text.clone(); + let lines = clipboard_text.split('\n'); + let mut is_first = true; + + for line in lines { + if is_first { + self.insert_str_after(line); + is_first = false; + } else { + self.split_line_after(); + let _ = self.insert_str_at(self.cursor.y, 0, line); } } } @@ -49,7 +43,7 @@ mod test { }; #[test] - fn paste_test() { + fn paste_after_test() { let mut buffer = Buffer::new(Size::new(10, 10).to_rectangle()); buffer.lines.clear(); buffer.lines = vec![String::from("12345"), String::from("67890")]; @@ -58,7 +52,7 @@ mod test { is_line: false, text: String::from("123\n456\n789"), }); - buffer.paste(); + buffer.paste_after(); let expected = String::from("12345\n678123\n456\n78990"); assert_eq!(expected, buffer.get_content()); } diff --git a/src/buffer/maps.rs b/src/buffer/maps.rs index d9eb1b6..be023bc 100644 --- a/src/buffer/maps.rs +++ b/src/buffer/maps.rs @@ -48,7 +48,7 @@ pub fn get_default_normal_maps() -> ActionMap { map.insert("J", buffer_action!(join_lines_cursor)); map.insert("O", buffer_action!(open_new_line_previous)); map.insert("o", buffer_action!(open_new_line_next)); - map.insert("p", buffer_action!(paste)); + map.insert("p", buffer_action!(paste_after)); // modes map.insert("i", buffer_action!(enter_insert_mode)); diff --git a/src/buffer/operations.rs b/src/buffer/operations.rs index da1f6c7..740ec66 100644 --- a/src/buffer/operations.rs +++ b/src/buffer/operations.rs @@ -35,6 +35,34 @@ impl Buffer { } } + pub fn insert_str_at(&mut self, row: usize, column: usize, text: &str) -> Result<(), String> { + let line = self.get_line_mut(row)?; + if column > line.len() { + return Err(format!( + "Can not insert str at {} (line: {})", + column, + row + 1 + )); + } + line.insert_str(column, text); + self.move_cursor(row, column + text.len().checked_sub(1).unwrap_or(0)); + Ok(()) + } + + pub fn insert_str_cursor(&mut self, text: &str) { + self.insert_str_at(self.cursor.y, self.cursor.y, text) + .unwrap() + } + + pub fn insert_str_after(&mut self, text: &str) { + if self.get_current_line_text_length() == 0 { + self.insert_str_cursor(text); + } else { + self.insert_str_at(self.cursor.y, self.cursor.x + 1, text) + .unwrap(); + } + } + pub fn delete_char_from(&mut self, row: usize, column: usize) -> Result<(), String> { let line = self.get_line_mut(row)?; if line.len() == column {