From 5fd262012127fe78d1ba168b12a963f487cdc720 Mon Sep 17 00:00:00 2001 From: mars Date: Fri, 14 Apr 2023 15:31:31 -0400 Subject: [PATCH 1/4] Implement goto_line_start and goto_line_end --- src/actions.rs | 8 ++++---- src/buffer.rs | 27 +++++++++++++++++++++++++++ src/keybinds.rs | 4 ++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index dfcf1d4..a64b48e 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -32,7 +32,7 @@ pub fn load_actions() -> HashMap { ("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) { diff --git a/src/buffer.rs b/src/buffer.rs index 68e56ae..82633ac 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -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)] diff --git a/src/keybinds.rs b/src/keybinds.rs index 7744766..eb0376a 100644 --- a/src/keybinds.rs +++ b/src/keybinds.rs @@ -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 = [ From 9558cdadb480f33bc328cfadbef3d527d2068c0c Mon Sep 17 00:00:00 2001 From: mars Date: Fri, 14 Apr 2023 15:34:54 -0400 Subject: [PATCH 2/4] Remove unused imports --- src/buffer.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 82633ac..7455214 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -21,11 +21,11 @@ use std::io::Write; use std::ops::Range; use std::sync::Arc; -use crossterm::{cursor, ExecutableCommand, QueueableCommand}; +use crossterm::{cursor, QueueableCommand}; use parking_lot::Mutex; use ropey::Rope; use syntect::easy::ScopeRangeIterator; -use syntect::parsing::{BasicScopeStackOp, ParseState, Scope, ScopeStack, SyntaxSet}; +use syntect::parsing::{BasicScopeStackOp, ParseState, ScopeStack, SyntaxSet}; use crate::theme::{Style, StyleStore}; use crate::{Cursor, Direction}; From 74e1bd88c6a8937fed8af9db736a3f3f2ac9dea0 Mon Sep 17 00:00:00 2001 From: mars Date: Fri, 14 Apr 2023 15:40:02 -0400 Subject: [PATCH 3/4] Fix Buffer::goto_first_whitespace --- src/buffer.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 7455214..ca86144 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -274,11 +274,12 @@ 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; + for c in line_slice.chars() { if !c.is_whitespace() { break; } + + column += 1; } Cursor { line, column } From 075b74afca83703c0cfa9947f7f2add5ea8145f1 Mon Sep 17 00:00:00 2001 From: mars Date: Fri, 14 Apr 2023 15:43:22 -0400 Subject: [PATCH 4/4] Add goto_first_nonwhitespace + impl insert_at_line_start and end --- src/actions.rs | 11 +++++++++-- src/keybinds.rs | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index a64b48e..b37c8a7 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -33,6 +33,7 @@ pub fn load_actions() -> HashMap { ("page_down", page_down), ("goto_line_start", goto_line_start), ("goto_line_end", goto_line_end), + ("goto_first_nonwhitespace", goto_first_nonwhitespace), ("command_mode", command_mode), ("normal_mode", normal_mode), ("visual_mode", visual_mode), @@ -88,6 +89,10 @@ pub fn goto_line_end(state: &mut State) { state.cursor = state.buffer.cursor_at_line_end(state.cursor.line); } +pub fn goto_first_nonwhitespace(state: &mut State) { + state.cursor = state.buffer.cursor_at_first_nonwhitespace(state.cursor.line); +} + pub fn command_mode(state: &mut State) { state.mode = Mode::Command(Default::default()); } @@ -117,11 +122,13 @@ pub fn append_mode(state: &mut State) { } pub fn insert_at_line_start(state: &mut State) { - state.set_error("insert_at_line_start is unimplemented"); + goto_first_nonwhitespace(state); + insert_mode(state); } pub fn insert_at_line_end(state: &mut State) { - state.set_error("insert_at_line_end is unimplemented"); + goto_line_end(state); + insert_mode(state); } pub fn open_below(state: &mut State) { diff --git a/src/keybinds.rs b/src/keybinds.rs index eb0376a..4cd0ae7 100644 --- a/src/keybinds.rs +++ b/src/keybinds.rs @@ -53,6 +53,7 @@ impl Default for Keybinds { let goto_keys = &[ (Char('h'), goto_line_start as Action), (Char('l'), goto_line_end), + (Char('s'), goto_first_nonwhitespace), ]; let normalish_keys = [