Skip to content

Commit

Permalink
Update the structs that implmented Invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
zao111222333 committed Oct 5, 2024
1 parent 7097ef6 commit 8fc0037
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
21 changes: 7 additions & 14 deletions src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -249,11 +249,8 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
pub fn validate(&mut self) -> Result<ValidationResult> {
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 => {}
Expand All @@ -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<()> {
Expand Down
4 changes: 2 additions & 2 deletions src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 8fc0037

Please sign in to comment.