Implement goto_line_start and goto_line_end

This commit is contained in:
mars 2023-04-14 15:31:31 -04:00
parent d68a719a90
commit 5fd2620121
3 changed files with 33 additions and 6 deletions

View File

@ -32,7 +32,7 @@ pub fn load_actions() -> HashMap<String, Action> {
("page_up", page_up),
("page_down", page_down),
("goto_line_start", goto_line_start),
("goto_line_end_newline", goto_line_end_newline),
("goto_line_end", goto_line_end),
("command_mode", command_mode),
("normal_mode", normal_mode),
("visual_mode", visual_mode),
@ -81,11 +81,11 @@ pub fn page_down(state: &mut State) {
}
pub fn goto_line_start(state: &mut State) {
state.set_error("goto_line_start is unimplemented");
state.cursor.column = 0;
}
pub fn goto_line_end_newline(state: &mut State) {
state.set_error("goto_line_end_newline is unimplemented");
pub fn goto_line_end(state: &mut State) {
state.cursor = state.buffer.cursor_at_line_end(state.cursor.line);
}
pub fn command_mode(state: &mut State) {

View File

@ -270,6 +270,33 @@ impl Buffer {
}
}
}
pub fn cursor_at_first_nonwhitespace(&self, line: usize) -> Cursor {
if let Some(line_slice) = self.text.get_line(line) {
let mut column = 0;
for c in line_slice.chars().reversed() {
column += 1;
if !c.is_whitespace() {
break;
}
}
Cursor { line, column }
} else {
Cursor { line, column: 0 }
}
}
pub fn cursor_at_line_end(&self, line: usize) -> Cursor {
Cursor {
line,
column: self
.text
.get_line(line)
.map(|line| line.len_chars().saturating_sub(1))
.unwrap_or(0),
}
}
}
#[cfg(test)]

View File

@ -47,12 +47,12 @@ impl Default for Keybinds {
(PageUp, page_up),
(PageDown, page_down),
(Home, goto_line_start),
(End, goto_line_end_newline),
(End, goto_line_end),
];
let goto_keys = &[
(Char('h'), goto_line_start as Action),
(Char('l'), goto_line_end_newline),
(Char('l'), goto_line_end),
];
let normalish_keys = [