From 8fc00376936939a948014eadd878b8a8898c375e Mon Sep 17 00:00:00 2001 From: junzhuo Date: Sat, 5 Oct 2024 03:58:07 -0700 Subject: [PATCH] Update the structs that implmented `Invoke` --- src/edit.rs | 21 +++++++-------------- src/keymap.rs | 4 ++-- src/lib.rs | 3 +-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/edit.rs b/src/edit.rs index 71ae33299..adf3fb33e 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -11,7 +11,7 @@ use crate::highlight::Highlighter; use crate::hint::Hint; use crate::history::SearchDirection; use crate::keymap::{Anchor, At, CharSearch, Cmd, Movement, RepeatCount, Word}; -use crate::keymap::{InputState, Refresher}; +use crate::keymap::{InputState, Invoke, Refresher}; use crate::layout::{Layout, Position}; use crate::line_buffer::{ ChangeListener, DeleteListener, Direction, LineBuffer, NoListener, WordAction, MAX_LINE, @@ -249,11 +249,8 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> { pub fn validate(&mut self) -> Result { if let Some(ref mut validator) = self.helper { self.changes.begin(); - // We only use `self.line` to create `impl Invoke`. - // Using entire `State` to make a `impl Invoke` is too wasted - // when other `State` attributes' lifetime are required. let result = - (*validator).validate(&mut ValidationContext::new(&mut self.line.as_str()))?; + (*validator).validate(&mut ValidationContext::new(&mut self.line))?; let corrected = self.changes.end(); match result { ValidationResult::Incomplete => {} @@ -278,15 +275,11 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> { } } -// We only use `self.line` to create `impl Invoke`. -// Using entire `State` to make a `impl Invoke` is too wasted -// when other `State` attributes' lifetime are required. -// -// impl<'out, 'prompt, H: Helper> Invoke for State<'out, 'prompt, H> { -// fn input(&self) -> &str { -// self.line.as_str() -// } -// } +impl Invoke for LineBuffer { + fn input(&self) -> &str { + self.as_str() + } +} impl<'out, 'prompt, H: Helper> Refresher for State<'out, 'prompt, H> { fn refresh_line(&mut self) -> Result<()> { diff --git a/src/keymap.rs b/src/keymap.rs index 8564c163a..87d48e950 100644 --- a/src/keymap.rs +++ b/src/keymap.rs @@ -363,9 +363,9 @@ pub trait Invoke { //fn invoke(&mut self, cmd: Cmd) -> Result; } -impl Invoke for &str { +impl Invoke for String { fn input(&self) -> &str { - self + self.as_str() } } diff --git a/src/lib.rs b/src/lib.rs index 3163b9e96..23e8ab82f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -517,8 +517,7 @@ fn readline_direct( match validator { None => return Ok(input), Some(ref mut v) => { - let mut ctx = input.as_str(); - let mut ctx = validate::ValidationContext::new(&mut ctx); + let mut ctx = validate::ValidationContext::new(&mut input); match v.validate(&mut ctx)? { validate::ValidationResult::Valid(msg) => {