diff --git a/src/actions.rs b/src/actions.rs index b37c8a7..f0bfa31 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -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 { ("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) { diff --git a/src/buffer.rs b/src/buffer.rs index ca86144..3ca275c 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -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)] diff --git a/src/keybinds.rs b/src/keybinds.rs index 4cd0ae7..f2337db 100644 --- a/src/keybinds.rs +++ b/src/keybinds.rs @@ -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 = [