diff --git a/src/action.rs b/src/action.rs index e57b036..c6b4b35 100644 --- a/src/action.rs +++ b/src/action.rs @@ -176,158 +176,78 @@ impl Buffer { self.partial_action = Some(PartialAction::Space); } - // TODO: all these move/extend-cursor operations could be DRYed together - fn move_byte_up(&mut self, window_size: WindowSize) { - self.primary_cursor.move_byte_up(); + fn change_all_cursors(&mut self, transform: impl Fn(&mut Cursor)) { + transform(&mut self.primary_cursor); for cursor in &mut self.cursors { - cursor.move_byte_up(); + transform(cursor); } self.cursors.sort_by_key(|cursor| cursor.head); self.combine_cursors_if_overlapping(); + } + + fn move_byte_up(&mut self, window_size: WindowSize) { + self.change_all_cursors(Cursor::move_byte_up); self.clamp_screen_to_primary_cursor(window_size); } fn move_byte_down(&mut self, window_size: WindowSize) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.move_byte_down(max_contents_index); - - for cursor in &mut self.cursors { - cursor.move_byte_down(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.move_byte_down(max_contents_index)); self.clamp_screen_to_primary_cursor(window_size); } fn move_byte_left(&mut self, window_size: WindowSize) { - self.primary_cursor.move_byte_left(); - - for cursor in &mut self.cursors { - cursor.move_byte_left(); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(Cursor::move_byte_left); self.clamp_screen_to_primary_cursor(window_size); } fn move_byte_right(&mut self, window_size: WindowSize) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.move_byte_right(max_contents_index); - - for cursor in &mut self.cursors { - cursor.move_byte_right(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.move_byte_right(max_contents_index)); self.clamp_screen_to_primary_cursor(window_size); } fn extend_byte_up(&mut self, window_size: WindowSize) { - self.primary_cursor.extend_byte_up(); - - for cursor in &mut self.cursors { - cursor.extend_byte_up(); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(Cursor::extend_byte_up); self.clamp_screen_to_primary_cursor(window_size); } fn extend_byte_down(&mut self, window_size: WindowSize) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.extend_byte_down(max_contents_index); - - for cursor in &mut self.cursors { - cursor.extend_byte_down(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.extend_byte_down(max_contents_index)); self.clamp_screen_to_primary_cursor(window_size); } fn extend_byte_left(&mut self, window_size: WindowSize) { - self.primary_cursor.extend_byte_left(); - - for cursor in &mut self.cursors { - cursor.extend_byte_left(); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(Cursor::extend_byte_left); self.clamp_screen_to_primary_cursor(window_size); } fn extend_byte_right(&mut self, window_size: WindowSize) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.extend_byte_right(max_contents_index); - - for cursor in &mut self.cursors { - cursor.extend_byte_right(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.extend_byte_right(max_contents_index)); self.clamp_screen_to_primary_cursor(window_size); } fn goto_line_start(&mut self) { - self.primary_cursor.goto_line_start(); - - for cursor in &mut self.cursors { - cursor.goto_line_start(); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(Cursor::goto_line_start); } fn goto_line_end(&mut self) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.goto_line_end(max_contents_index); - - for cursor in &mut self.cursors { - cursor.goto_line_end(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.goto_line_end(max_contents_index)); } fn goto_file_start(&mut self, window_size: WindowSize) { - self.primary_cursor.goto_file_start(); - - for cursor in &mut self.cursors { - cursor.goto_file_start(); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(Cursor::goto_file_start); self.clamp_screen_to_primary_cursor(window_size); } fn goto_file_end(&mut self, window_size: WindowSize) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.goto_file_end(max_contents_index); - - for cursor in &mut self.cursors { - cursor.goto_file_end(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.goto_file_end(max_contents_index)); self.clamp_screen_to_primary_cursor(window_size); } @@ -429,81 +349,35 @@ impl Buffer { fn move_next_word_start(&mut self, window_size: WindowSize) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.move_to_next_word(max_contents_index); - - for cursor in &mut self.cursors { - cursor.move_to_next_word(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.move_next_word_start(max_contents_index)); self.clamp_screen_to_primary_cursor(window_size); } fn move_next_word_end(&mut self, window_size: WindowSize) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.move_to_next_end(max_contents_index); - - for cursor in &mut self.cursors { - cursor.move_to_next_end(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.move_next_word_end(max_contents_index)); self.clamp_screen_to_primary_cursor(window_size); } fn move_previous_word_start(&mut self, window_size: WindowSize) { - self.primary_cursor.move_to_previous_beginning(); - - for cursor in &mut self.cursors { - cursor.move_to_previous_beginning(); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(Cursor::move_previous_word_start); self.clamp_screen_to_primary_cursor(window_size); } fn extend_next_word_start(&mut self, window_size: WindowSize) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.extend_to_next_word(max_contents_index); - - for cursor in &mut self.cursors { - cursor.extend_to_next_word(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.extend_next_word_start(max_contents_index)); self.clamp_screen_to_primary_cursor(window_size); } fn extend_next_word_end(&mut self, window_size: WindowSize) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.extend_to_next_end(max_contents_index); - - for cursor in &mut self.cursors { - cursor.extend_to_next_end(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.extend_next_word_end(max_contents_index)); self.clamp_screen_to_primary_cursor(window_size); } fn extend_previous_word_start(&mut self, window_size: WindowSize) { - self.primary_cursor.extend_to_previous_beginning(); - - for cursor in &mut self.cursors { - cursor.extend_to_previous_beginning(); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(Cursor::extend_previous_word_start); self.clamp_screen_to_primary_cursor(window_size); } @@ -517,29 +391,13 @@ impl Buffer { fn extend_line_below(&mut self, window_size: WindowSize) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.extend_line_below(max_contents_index); - - for cursor in &mut self.cursors { - cursor.extend_line_below(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.extend_line_below(max_contents_index)); self.clamp_screen_to_primary_cursor(window_size); } fn extend_line_above(&mut self, window_size: WindowSize) { let max_contents_index = self.max_contents_index(); - - self.primary_cursor.extend_line_above(max_contents_index); - - for cursor in &mut self.cursors { - cursor.extend_line_above(max_contents_index); - } - self.cursors.sort_by_key(|cursor| cursor.head); - - self.combine_cursors_if_overlapping(); + self.change_all_cursors(|cursor| cursor.extend_line_above(max_contents_index)); self.clamp_screen_to_primary_cursor(window_size); } diff --git a/src/cursor.rs b/src/cursor.rs index ebb3e1f..4c81a59 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -146,7 +146,7 @@ impl Cursor { self.collapse(); } - pub fn move_to_next_word(&mut self, max: usize) { + pub fn move_next_word_start(&mut self, max: usize) { if self.head == max { return; } if self.head.is_multiple_of(4) { // at the beginning of a word @@ -157,7 +157,7 @@ impl Cursor { self.collapse(); } - pub fn move_to_next_end(&mut self, max: usize) { + pub fn move_next_word_end(&mut self, max: usize) { if self.head == max { return; } self.collapse(); @@ -169,7 +169,7 @@ impl Cursor { } } - pub const fn move_to_previous_beginning(&mut self) { + pub const fn move_previous_word_start(&mut self) { if self.head == 0 { return; } self.collapse(); @@ -181,7 +181,7 @@ impl Cursor { } } - pub fn extend_to_next_word(&mut self, max: usize) { + pub fn extend_next_word_start(&mut self, max: usize) { if self.head == max { return; } if self.head.is_multiple_of(4) { // at the beginning of a word @@ -191,7 +191,7 @@ impl Cursor { } } - pub fn extend_to_next_end(&mut self, max: usize) { + pub fn extend_next_word_end(&mut self, max: usize) { if self.head == max { return; } if self.head % 4 == 3 { // at the end of a word @@ -201,7 +201,7 @@ impl Cursor { } } - pub const fn extend_to_previous_beginning(&mut self) { + pub const fn extend_previous_word_start(&mut self) { if self.head == 0 { return; } if self.head.is_multiple_of(4) { // at the beginning of a word