Add goto_file_start and goto_file_end

This commit is contained in:
mars 2023-04-14 17:29:19 -04:00
parent a7e121ba18
commit e47275cb88
3 changed files with 30 additions and 2 deletions

View File

@ -19,7 +19,7 @@
use std::collections::HashMap;
use crate::{Direction, InsertState, Mode, NormalState, State};
use crate::{Cursor, Direction, InsertState, Mode, NormalState, State};
pub type Action = fn(&mut State);
@ -34,6 +34,8 @@ pub fn load_actions() -> HashMap<String, Action> {
("goto_line_start", goto_line_start),
("goto_line_end", goto_line_end),
("goto_first_nonwhitespace", goto_first_nonwhitespace),
("goto_file_start", goto_file_start),
("goto_file_end", goto_file_end),
("command_mode", command_mode),
("normal_mode", normal_mode),
("visual_mode", visual_mode),
@ -90,7 +92,19 @@ pub fn goto_line_end(state: &mut State) {
}
pub fn goto_first_nonwhitespace(state: &mut State) {
state.cursor = state.buffer.cursor_at_first_nonwhitespace(state.cursor.line);
state.cursor = state
.buffer
.cursor_at_first_nonwhitespace(state.cursor.line);
}
pub fn goto_file_start(state: &mut State) {
state.cursor = Cursor { line: 0, column: 0 };
state.scroll_to_cursor();
}
pub fn goto_file_end(state: &mut State) {
state.cursor = state.buffer.cursor_at_file_end();
state.scroll_to_cursor();
}
pub fn command_mode(state: &mut State) {

View File

@ -298,6 +298,18 @@ impl Buffer {
.unwrap_or(0),
}
}
pub fn cursor_at_file_end(&self) -> Cursor {
let last_line = if self.text.len_lines() == 0 {
0
} else if self.text.line(self.text.len_lines() - 1).len_chars() == 0 {
self.text.len_lines().saturating_sub(2)
} else {
self.text.len_lines() - 1
};
self.cursor_at_line_end(last_line)
}
}
#[cfg(test)]

View File

@ -54,6 +54,8 @@ impl Default for Keybinds {
(Char('h'), goto_line_start as Action),
(Char('l'), goto_line_end),
(Char('s'), goto_first_nonwhitespace),
(Char('g'), goto_file_start),
(Char('e'), goto_file_end),
];
let normalish_keys = [