Skip to content

Commit

Permalink
Merge pull request #461 from gwenn/invalid_result_msg
Browse files Browse the repository at this point in the history
Invalid result msg
  • Loading branch information
gwenn authored Nov 15, 2020
2 parents cbf0f94 + 517a2e7 commit 310f370
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
18 changes: 10 additions & 8 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::sync::{Arc, Mutex};

use crate::{Helper, Result};
use crate::{complete_hint_line};
use crate::complete_hint_line;
use crate::config::Config;
use crate::edit::State;
use crate::error;
use crate::history::{Direction};
use crate::history::Direction;
use crate::keymap::{Anchor, At, Cmd, Movement, Word};
use crate::keymap::{InputState, Refresher};
use crate::kill_ring::{KillRing, Mode};
use crate::line_buffer::WordAction;
use crate::keymap::{InputState, Refresher};

use crate::{Helper, Result};

pub enum Status {
Proceed,
Expand Down Expand Up @@ -130,7 +129,8 @@ pub fn execute<H: Helper>(
// line as the user typed it after a newline.
s.refresh_line_with_msg(None)?;
}
let valid = s.validate()?;
let validation_result = s.validate()?;
let valid = validation_result.is_valid();
let end = s.line.is_end_of_input();
match (cmd, valid, end) {
(Cmd::AcceptLine, ..)
Expand All @@ -147,7 +147,9 @@ pub fn execute<H: Helper>(
(Cmd::Newline, ..)
| (Cmd::AcceptOrInsertLine { .. }, false, _)
| (Cmd::AcceptOrInsertLine { .. }, true, false) => {
s.edit_insert('\n', 1)?;
if valid || !validation_result.has_message() {
s.edit_insert('\n', 1)?;
}
}
_ => unreachable!(),
}
Expand Down Expand Up @@ -231,5 +233,5 @@ pub fn execute<H: Helper>(
// Ignore the character typed.
}
}
return Ok(Proceed);
Ok(Proceed)
}
26 changes: 12 additions & 14 deletions src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,32 +202,30 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
self.layout.default_prompt
}

pub fn validate(&mut self) -> Result<bool> {
pub fn validate(&mut self) -> Result<ValidationResult> {
if let Some(validator) = self.helper {
self.changes.borrow_mut().begin();
let result = validator.validate(&mut ValidationContext::new(self))?;
let corrected = self.changes.borrow_mut().end();
let validated = match result {
ValidationResult::Incomplete => false,
ValidationResult::Valid(msg) => {
match result {
ValidationResult::Incomplete => {}
ValidationResult::Valid(ref msg) => {
// Accept the line regardless of where the cursor is.
if corrected || self.has_hint() || msg.is_some() {
// Force a refresh without hints to leave the previous
// line as the user typed it after a newline.
self.refresh_line_with_msg(msg)?;
self.refresh_line_with_msg(msg.as_deref())?;
}
true
}
ValidationResult::Invalid(msg) => {
ValidationResult::Invalid(ref msg) => {
if corrected || self.has_hint() || msg.is_some() {
self.refresh_line_with_msg(msg)?;
self.refresh_line_with_msg(msg.as_deref())?;
}
false
}
};
Ok(validated)
}
Ok(result)
} else {
Ok(true)
Ok(ValidationResult::Valid(None))
}
}
}
Expand All @@ -246,11 +244,11 @@ impl<'out, 'prompt, H: Helper> Refresher for State<'out, 'prompt, H> {
self.refresh(self.prompt, prompt_size, true, Info::Hint)
}

fn refresh_line_with_msg(&mut self, msg: Option<String>) -> Result<()> {
fn refresh_line_with_msg(&mut self, msg: Option<&str>) -> Result<()> {
let prompt_size = self.prompt_size;
self.hint = None;
self.highlight_char();
self.refresh(self.prompt, prompt_size, true, Info::Msg(msg.as_deref()))
self.refresh(self.prompt, prompt_size, true, Info::Msg(msg))
}

fn refresh_prompt_and_line(&mut self, prompt: &str) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ pub trait Refresher {
/// cursor position, and number of columns of the terminal.
fn refresh_line(&mut self) -> Result<()>;
/// Same as [`refresh_line`] with a specific message instead of hint
fn refresh_line_with_msg(&mut self, msg: Option<String>) -> Result<()>;
fn refresh_line_with_msg(&mut self, msg: Option<&str>) -> Result<()>;
/// Same as `refresh_line` but with a dynamic prompt.
fn refresh_prompt_and_line(&mut self, prompt: &str) -> Result<()>;
/// Vi only, switch to insert mode.
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use crate::history::{Direction, History};
pub use crate::keymap::{Anchor, At, CharSearch, Cmd, Movement, RepeatCount, Word};
use crate::keymap::{InputState, Refresher};
pub use crate::keys::{KeyCode, KeyEvent, Modifiers};
use crate::kill_ring::{KillRing};
use crate::kill_ring::KillRing;

use crate::validate::Validator;

Expand Down Expand Up @@ -507,7 +507,7 @@ fn readline_edit<H: Helper>(
}

// Execute things can be done solely on a state object
match command::execute(cmd, &mut s, &input_state, &mut editor.kill_ring, &editor.config)? {
match command::execute(cmd, &mut s, &input_state, &editor.kill_ring, &editor.config)? {
command::Status::Proceed => continue,
command::Status::Submit => break,
}
Expand Down
4 changes: 2 additions & 2 deletions src/line_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ impl LineBuffer {
let end = self.buf[end..]
.rfind('\n')
.map(|pos| end + pos)
.unwrap_or(self.buf.len());
.unwrap_or_else(|| self.buf.len());
let mut index = start;
if dedent {
for line in self.buf[start..end].to_string().split('\n') {
Expand Down Expand Up @@ -1159,7 +1159,7 @@ impl LineBuffer {
index += amount + line.len() + 1;
}
}
return true;
true
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ pub enum ValidationResult {
Valid(Option<String>),
}

impl ValidationResult {
pub(crate) fn is_valid(&self) -> bool {
if let ValidationResult::Valid(_) = self {
true
} else {
false
}
}
pub(crate) fn has_message(&self) -> bool {
match self {
ValidationResult::Valid(Some(_)) => true,
ValidationResult::Invalid(Some(_)) => true,
_ => false,
}
}
}

/// Give access to user input.
pub struct ValidationContext<'i> {
i: &'i mut dyn Invoke,
Expand Down

0 comments on commit 310f370

Please sign in to comment.