From 90c87ec3fd8b52e58903cde9991f783a5f8f7b50 Mon Sep 17 00:00:00 2001 From: Kyllingene Date: Tue, 19 Dec 2023 10:51:07 -0800 Subject: [PATCH 1/3] Fixed #288 --- crates/shrs_line/src/line.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/shrs_line/src/line.rs b/crates/shrs_line/src/line.rs index 8bb2c98c..0ccf6cf4 100644 --- a/crates/shrs_line/src/line.rs +++ b/crates/shrs_line/src/line.rs @@ -93,7 +93,7 @@ impl Default for Line { } /// State for where the prompt is in history browse mode -#[derive(PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq)] pub enum HistoryInd { /// Brand new prompt Prompt, @@ -112,7 +112,7 @@ impl HistoryInd { HistoryInd::Line(0) } }, - HistoryInd::Line(i) => HistoryInd::Line((i + 1).min(limit)), + HistoryInd::Line(i) => HistoryInd::Line((i + 1).min(limit - 1)), } } @@ -703,7 +703,10 @@ impl Line { }, // fill prompt with history element HistoryInd::Line(i) => { - let history_item = self.history.get(i).unwrap(); + let history_item = self + .history + .get(i) + .expect(&format!("i: {i} limit: {}", self.history.len())); ctx.cb.clear(); ctx.cb.insert(Location::Cursor(), history_item)?; }, From 968b8f7f06521fd2a476e2f3d6725132f36a6137 Mon Sep 17 00:00:00 2001 From: Kyllingene Date: Tue, 19 Dec 2023 10:55:27 -0800 Subject: [PATCH 2/3] Undid debug expect --- crates/shrs_line/src/line.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/crates/shrs_line/src/line.rs b/crates/shrs_line/src/line.rs index 0ccf6cf4..d3c5c3be 100644 --- a/crates/shrs_line/src/line.rs +++ b/crates/shrs_line/src/line.rs @@ -703,10 +703,7 @@ impl Line { }, // fill prompt with history element HistoryInd::Line(i) => { - let history_item = self - .history - .get(i) - .expect(&format!("i: {i} limit: {}", self.history.len())); + let history_item = self.history.get(i).unwrap(); ctx.cb.clear(); ctx.cb.insert(Location::Cursor(), history_item)?; }, From 20551ef23d4eebee8b8454b20c056ed4eec9403f Mon Sep 17 00:00:00 2001 From: Kyllingene Date: Tue, 19 Dec 2023 19:12:52 -0800 Subject: [PATCH 3/3] Should close #230, but not tested --- crates/shrs_line/src/vi.rs | 28 ++++++++++++++++++++++------ crates/shrs_vi/src/ast.rs | 9 ++++++++- crates/shrs_vi/src/grammar.lalrpop | 5 ++++- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/crates/shrs_line/src/vi.rs b/crates/shrs_line/src/vi.rs index 5d3bd5d2..e1382995 100644 --- a/crates/shrs_line/src/vi.rs +++ b/crates/shrs_line/src/vi.rs @@ -3,7 +3,7 @@ use arboard::Clipboard; use shrs_utils::cursor_buffer::{CursorBuffer, Location, Result}; use shrs_vi::{Action, Motion}; -use crate::line::LineMode; +use crate::{completion::CompletionCtx, line::LineMode}; /// Extension trait to [CursorBuffer] that enables the execution of vi motions pub trait ViCursorBuffer { @@ -14,13 +14,29 @@ pub trait ViCursorBuffer { impl ViCursorBuffer for CursorBuffer { fn motion_to_loc(&self, motion: Motion) -> Result { match motion { - Motion::Find(c) => { + Motion::Find { ch, back, to } => { // if current char is character we are looking for go to next one let offset = match self.char_at(Location::Cursor()) { - Some(cur_char) if cur_char == c => Location::After(), + Some(cur_char) if cur_char == ch => Location::After(), _ => Location::Cursor(), }; - Ok(Location::FindChar(self, offset, c).unwrap_or_default()) + + let mut loc = if back { + Location::FindCharBack(self, offset, ch).unwrap_or_default() + } else { + Location::FindChar(self, offset, ch).unwrap_or_default() + }; + + if to && loc != Location::Cursor() { + loc = loc + + if back { + Location::After() + } else { + Location::Before() + }; + } + + Ok(loc) }, Motion::Left => Ok(Location::Before()), Motion::Right => Ok(Location::After()), @@ -110,7 +126,7 @@ impl ViCursorBuffer for CursorBuffer { | Motion::Word | Motion::WordPunc | Motion::BackWord - | Motion::Find(_) => { + | Motion::Find { .. } => { self.move_cursor(self.motion_to_loc(motion)?)?; }, _ => (), @@ -126,7 +142,7 @@ impl ViCursorBuffer for CursorBuffer { | Motion::Word | Motion::WordPunc | Motion::BackWord - | Motion::Find(_) => { + | Motion::Find { .. } => { self.delete(Location::Cursor(), self.motion_to_loc(motion)?)?; }, _ => (), diff --git a/crates/shrs_vi/src/ast.rs b/crates/shrs_vi/src/ast.rs index 9e7120f5..e199f013 100644 --- a/crates/shrs_vi/src/ast.rs +++ b/crates/shrs_vi/src/ast.rs @@ -18,7 +18,14 @@ pub enum Motion { End, /// Select entire line (for Move action this behaves same as End) All, - Find(char), + /// Search forward/backward for/to character + /// + /// Encapsulates `f`, `F`, `t`, and `T` + Find { + ch: char, + back: bool, + to: bool, + }, } #[derive(Debug, PartialEq, Eq, Clone)] diff --git a/crates/shrs_vi/src/grammar.lalrpop b/crates/shrs_vi/src/grammar.lalrpop index 2aae1f25..3bef1723 100644 --- a/crates/shrs_vi/src/grammar.lalrpop +++ b/crates/shrs_vi/src/grammar.lalrpop @@ -21,7 +21,10 @@ pub Motion: Motion = { "$" => Motion::End, "^" => Motion::Start, "0" => Motion::Start, // currently implementing 0 as same as ^ - "f" => Motion::Find(c.chars().next().unwrap()), + "f" => Motion::Find { ch: c.chars().next().unwrap(), back: false, to: false }, + "F" => Motion::Find { ch: c.chars().next().unwrap(), back: true, to: false }, + "t" => Motion::Find { ch: c.chars().next().unwrap(), back: false, to: true}, + "T" => Motion::Find { ch: c.chars().next().unwrap(), back: true, to: true }, }; pub Action: Action = {