Skip to content

Commit

Permalink
refactor: improve performance on paste and add insert str api
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 28, 2023
1 parent 49d2da2 commit 53f3677
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
34 changes: 14 additions & 20 deletions src/buffer/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand All @@ -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")];
Expand All @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion src/buffer/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
28 changes: 28 additions & 0 deletions src/buffer/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 53f3677

Please sign in to comment.