clean up cursor movement actions

This commit is contained in:
alice pellerin
2026-03-21 00:21:22 -05:00
parent f81d220d93
commit e0b3bbe052
2 changed files with 32 additions and 174 deletions
+26 -168
View File
@@ -176,158 +176,78 @@ impl Buffer {
self.partial_action = Some(PartialAction::Space); self.partial_action = Some(PartialAction::Space);
} }
// TODO: all these move/extend-cursor operations could be DRYed together fn change_all_cursors(&mut self, transform: impl Fn(&mut Cursor)) {
fn move_byte_up(&mut self, window_size: WindowSize) { transform(&mut self.primary_cursor);
self.primary_cursor.move_byte_up();
for cursor in &mut self.cursors { for cursor in &mut self.cursors {
cursor.move_byte_up(); transform(cursor);
} }
self.cursors.sort_by_key(|cursor| cursor.head); self.cursors.sort_by_key(|cursor| cursor.head);
self.combine_cursors_if_overlapping(); 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); self.clamp_screen_to_primary_cursor(window_size);
} }
fn move_byte_down(&mut self, window_size: WindowSize) { fn move_byte_down(&mut self, window_size: WindowSize) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.move_byte_down(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn move_byte_left(&mut self, window_size: WindowSize) { fn move_byte_left(&mut self, window_size: WindowSize) {
self.primary_cursor.move_byte_left(); self.change_all_cursors(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn move_byte_right(&mut self, window_size: WindowSize) { fn move_byte_right(&mut self, window_size: WindowSize) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.move_byte_right(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn extend_byte_up(&mut self, window_size: WindowSize) { fn extend_byte_up(&mut self, window_size: WindowSize) {
self.primary_cursor.extend_byte_up(); self.change_all_cursors(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn extend_byte_down(&mut self, window_size: WindowSize) { fn extend_byte_down(&mut self, window_size: WindowSize) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.extend_byte_down(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn extend_byte_left(&mut self, window_size: WindowSize) { fn extend_byte_left(&mut self, window_size: WindowSize) {
self.primary_cursor.extend_byte_left(); self.change_all_cursors(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn extend_byte_right(&mut self, window_size: WindowSize) { fn extend_byte_right(&mut self, window_size: WindowSize) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.extend_byte_right(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn goto_line_start(&mut self) { fn goto_line_start(&mut self) {
self.primary_cursor.goto_line_start(); self.change_all_cursors(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();
} }
fn goto_line_end(&mut self) { fn goto_line_end(&mut self) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.goto_line_end(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();
} }
fn goto_file_start(&mut self, window_size: WindowSize) { fn goto_file_start(&mut self, window_size: WindowSize) {
self.primary_cursor.goto_file_start(); self.change_all_cursors(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn goto_file_end(&mut self, window_size: WindowSize) { fn goto_file_end(&mut self, window_size: WindowSize) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.goto_file_end(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
@@ -429,81 +349,35 @@ impl Buffer {
fn move_next_word_start(&mut self, window_size: WindowSize) { fn move_next_word_start(&mut self, window_size: WindowSize) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.move_next_word_start(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn move_next_word_end(&mut self, window_size: WindowSize) { fn move_next_word_end(&mut self, window_size: WindowSize) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.move_next_word_end(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn move_previous_word_start(&mut self, window_size: WindowSize) { fn move_previous_word_start(&mut self, window_size: WindowSize) {
self.primary_cursor.move_to_previous_beginning(); self.change_all_cursors(Cursor::move_previous_word_start);
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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn extend_next_word_start(&mut self, window_size: WindowSize) { fn extend_next_word_start(&mut self, window_size: WindowSize) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.extend_next_word_start(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn extend_next_word_end(&mut self, window_size: WindowSize) { fn extend_next_word_end(&mut self, window_size: WindowSize) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.extend_next_word_end(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn extend_previous_word_start(&mut self, window_size: WindowSize) { fn extend_previous_word_start(&mut self, window_size: WindowSize) {
self.primary_cursor.extend_to_previous_beginning(); self.change_all_cursors(Cursor::extend_previous_word_start);
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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
@@ -517,29 +391,13 @@ impl Buffer {
fn extend_line_below(&mut self, window_size: WindowSize) { fn extend_line_below(&mut self, window_size: WindowSize) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.extend_line_below(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
fn extend_line_above(&mut self, window_size: WindowSize) { fn extend_line_above(&mut self, window_size: WindowSize) {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.change_all_cursors(|cursor| cursor.extend_line_above(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.clamp_screen_to_primary_cursor(window_size); self.clamp_screen_to_primary_cursor(window_size);
} }
+6 -6
View File
@@ -146,7 +146,7 @@ impl Cursor {
self.collapse(); 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 == max { return; }
if self.head.is_multiple_of(4) { // at the beginning of a word if self.head.is_multiple_of(4) { // at the beginning of a word
@@ -157,7 +157,7 @@ impl Cursor {
self.collapse(); 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; } if self.head == max { return; }
self.collapse(); 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; } if self.head == 0 { return; }
self.collapse(); 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 == max { return; }
if self.head.is_multiple_of(4) { // at the beginning of a word 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 == max { return; }
if self.head % 4 == 3 { // at the end of a word 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 == 0 { return; }
if self.head.is_multiple_of(4) { // at the beginning of a word if self.head.is_multiple_of(4) { // at the beginning of a word