Skip to content

Commit

Permalink
refactor: rename
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 28, 2023
1 parent 5e41baa commit 850eb3d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 56 deletions.
18 changes: 9 additions & 9 deletions src/buffer/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Buffer {
}
}

pub fn paste_cursor(&mut self) {
pub fn paste_at_cursor(&mut self) {
if let Some(clipboard) = &self.clipboard {
let clipboard_text = clipboard.text.clone();
let lines = clipboard_text.split('\n');
Expand All @@ -26,26 +26,26 @@ impl Buffer {
if is_first {
is_first = false;
} else {
self.split_line_after();
self.split_line_at_after_cursor();
}
self.insert_str_cursor(line);
self.insert_str_at_cursor(line);
}
}
}

pub fn paste_after(&mut self) {
pub fn paste_at_after_cursor(&mut self) {
if let Some(clipboard) = &self.clipboard {
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);
self.insert_str_at_after_cursor(line);
is_first = false;
} else {
self.split_line_after();
let _ = self.insert_str_at(self.cursor.y, 0, line);
self.split_line_at_after_cursor();
let _ = self.insert_str_to(self.cursor.y, 0, line);
}
}
}
Expand All @@ -69,7 +69,7 @@ mod test {
is_line: false,
text: String::from("123\n456\n789"),
});
buffer.paste_cursor();
buffer.paste_at_cursor();
let expected = String::from("12345\n67123\n456\n789890");
assert_eq!(expected, buffer.get_content());
}
Expand All @@ -84,7 +84,7 @@ mod test {
is_line: false,
text: String::from("123\n456\n789"),
});
buffer.paste_after();
buffer.paste_at_after_cursor();
let expected = String::from("12345\n678123\n456\n78990");
assert_eq!(expected, buffer.get_content());
}
Expand Down
19 changes: 11 additions & 8 deletions src/buffer/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ pub fn get_default_normal_maps() -> ActionMap {
map.insert("N", buffer_action!(move_to_previous_find));

// operations
map.insert("s", buffer_action!(substitute_char));
map.insert("x", buffer_action!(delete_char));
map.insert("s", buffer_action!(substitute_char_from_cursor));
map.insert("x", buffer_action!(delete_char_from_cursor));
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_cursor));
map.insert("p", buffer_action!(paste_after));
map.insert("P", buffer_action!(paste_at_cursor));
map.insert("p", buffer_action!(paste_at_after_cursor));

// modes
map.insert("i", buffer_action!(enter_insert_mode));
Expand Down Expand Up @@ -106,10 +106,13 @@ pub fn get_default_insert_maps() -> ActionMap {
let mut map: ActionMap = HashMap::new();

map.insert("esc", buffer_action!(enter_normal_mode));
map.insert("enter", buffer_action!(split_line_cursor));
map.insert("backspace", buffer_action!(delete_char_before_cursor));
map.insert("delete", buffer_action!(delete_char));
map.insert("<c-j>", buffer_action!(split_line_cursor));
map.insert("enter", buffer_action!(split_line_at_cursor));
map.insert(
"backspace",
buffer_action!(delete_previous_char_from_cursor),
);
map.insert("delete", buffer_action!(delete_char_from_cursor));
map.insert("<c-j>", buffer_action!(split_line_at_cursor));

map
}
13 changes: 0 additions & 13 deletions src/buffer/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,4 @@ pub mod tests {
assert_eq!("", buffer.get_line_visible_text(1).unwrap());
assert_eq!("", buffer.get_line_visible_text(2).unwrap());
}

#[test]
pub fn move_last_column_test() {
let mut buffer = create_buffer();
buffer.enter_insert_mode();
buffer.insert_char('1');
buffer.insert_char('2');
buffer.insert_char('3');
buffer.insert_char('4');
buffer.insert_char('4');
buffer.enter_normal_mode();
buffer.move_last_column();
}
}
51 changes: 26 additions & 25 deletions src/buffer/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ impl Buffer {
Ok(())
}

pub fn insert_char(&mut self, ch: char) {
pub fn insert_char_at_cursor(&mut self, ch: char) {
let row = self.cursor.y;
let column = self.cursor.x;
self.insert_char_to(row, column, ch).unwrap();
self.move_cursor(row, column + 1);
}

pub fn insert_char_after(&mut self, ch: char) {
pub fn insert_char_at_after_cursor(&mut self, ch: char) {
if self.get_current_line_text_length() == 0 {
self.insert_char(ch);
self.insert_char_at_cursor(ch);
} else {
let row = self.cursor.y;
let column = self.cursor.x + 1;
Expand All @@ -35,7 +35,7 @@ impl Buffer {
}
}

pub fn insert_str_at(&mut self, row: usize, column: usize, text: &str) -> Result<(), String> {
pub fn insert_str_to(&mut self, row: usize, column: usize, text: &str) -> Result<(), String> {
let line = self.get_line_mut(row)?;
if column > line.len() {
return Err(format!(
Expand All @@ -49,16 +49,16 @@ impl Buffer {
Ok(())
}

pub fn insert_str_cursor(&mut self, text: &str) {
self.insert_str_at(self.cursor.y, self.cursor.x, text)
pub fn insert_str_at_cursor(&mut self, text: &str) {
self.insert_str_to(self.cursor.y, self.cursor.x, text)
.unwrap()
}

pub fn insert_str_after(&mut self, text: &str) {
pub fn insert_str_at_after_cursor(&mut self, text: &str) {
if self.get_current_line_text_length() == 0 {
self.insert_str_cursor(text);
self.insert_str_at_cursor(text);
} else {
self.insert_str_at(self.cursor.y, self.cursor.x + 1, text)
self.insert_str_to(self.cursor.y, self.cursor.x + 1, text)
.unwrap();
}
}
Expand All @@ -80,11 +80,11 @@ impl Buffer {
Ok(())
}

pub fn delete_char(&mut self) {
pub fn delete_char_from_cursor(&mut self) {
self.delete_char_from(self.cursor.y, self.cursor.x).unwrap();
}

pub fn delete_char_before(&mut self, row: usize, column: usize) -> Result<(), String> {
pub fn delete_previous_char_from(&mut self, row: usize, column: usize) -> Result<(), String> {
if row == 0 && column == 0 {
return Ok(());
}
Expand All @@ -95,42 +95,42 @@ impl Buffer {
self.join_lines(row - 1, row)?;
} else {
self.move_left();
self.delete_char();
self.delete_char_from_cursor();
}

Ok(())
}

pub fn delete_char_before_cursor(&mut self) {
self.delete_char_before(self.cursor.y, self.cursor.x)
pub fn delete_previous_char_from_cursor(&mut self) {
self.delete_previous_char_from(self.cursor.y, self.cursor.x)
.unwrap();
}

pub fn substitute_char(&mut self) {
self.delete_char();
pub fn substitute_char_from_cursor(&mut self) {
self.delete_char_from_cursor();
self.enter_insert_mode();
}

fn insert_line(&mut self, row: usize) {
fn insert_line_at(&mut self, row: usize) {
self.lines.insert(row, String::new());
self.set_size(self.area.clone());
}

pub fn open_new_line_previous(&mut self) {
let row = self.cursor.y;
self.insert_line(row);
self.insert_line_at(row);
self.move_cursor(row, 0);
self.enter_insert_mode();
}

pub fn open_new_line_next(&mut self) {
let row = self.cursor.y + 1;
self.insert_line(row);
self.insert_line_at(row);
self.move_cursor(row, 0);
self.enter_insert_mode();
}

pub fn split_line(&mut self, row: usize, column: usize) -> Result<(), String> {
pub fn split_line_at(&mut self, row: usize, column: usize) -> Result<(), String> {
let line = self.get_line(row)?;
if column > line.len() {
return Err(format!("No column at {} (line: {})", column, row + 1));
Expand All @@ -149,15 +149,16 @@ impl Buffer {
Ok(())
}

pub fn split_line_cursor(&mut self) {
self.split_line(self.cursor.y, self.cursor.x).unwrap();
pub fn split_line_at_cursor(&mut self) {
self.split_line_at(self.cursor.y, self.cursor.x).unwrap();
}

pub fn split_line_after(&mut self) {
pub fn split_line_at_after_cursor(&mut self) {
if self.get_current_line_text_length() == 0 {
self.split_line_cursor();
self.split_line_at_cursor();
} else {
self.split_line(self.cursor.y, self.cursor.x + 1).unwrap();
self.split_line_at(self.cursor.y, self.cursor.x + 1)
.unwrap();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Editor {
None => {
if !key.ctrl && !key.win && !key.alt && key.code.len() == 1 {
let ch = key.code.chars().nth(0).unwrap();
buffer.insert_char(ch);
buffer.insert_char_at_cursor(ch);
}
}
},
Expand Down

0 comments on commit 850eb3d

Please sign in to comment.