Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not validate if the command is 'NewLine' #640

Merged
merged 3 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/input_multiline.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustyline::validate::MatchingBracketValidator;
use rustyline::{Editor, Result};
use rustyline::{Cmd, Editor, EventHandler, KeyCode, KeyEvent, Modifiers, Result};
use rustyline_derive::{Completer, Helper, Highlighter, Hinter, Validator};

#[derive(Completer, Helper, Highlighter, Hinter, Validator)]
Expand All @@ -14,6 +14,10 @@ fn main() -> Result<()> {
};
let mut rl = Editor::new()?;
rl.set_helper(Some(h));
rl.bind_sequence(
KeyEvent(KeyCode::Char('s'), Modifiers::CTRL),
EventHandler::Simple(Cmd::Newline),
);

let input = rl.readline("> ")?;
println!("Input: {}", input);
Expand Down
28 changes: 15 additions & 13 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ pub fn execute<H: Helper>(
) -> Result<Status> {
use Status::{Proceed, Submit};

match cmd {
Cmd::EndOfFile | Cmd::AcceptLine | Cmd::AcceptOrInsertLine { .. } | Cmd::Newline => {
if s.has_hint() || !s.is_default_prompt() {
// Force a refresh without hints to leave the previous
// line as the user typed it after a newline.
s.refresh_line_with_msg(None)?;
}
}
_ => {}
};
match cmd {
Cmd::CompleteHint => {
complete_hint_line(s)?;
Expand Down Expand Up @@ -58,11 +68,6 @@ pub fn execute<H: Helper>(
s.edit_overwrite_char(c)?;
}
Cmd::EndOfFile => {
if s.has_hint() || !s.is_default_prompt() {
// Force a refresh without hints to leave the previous
// line as the user typed it after a newline.
s.refresh_line_with_msg(None)?;
}
if s.line.is_empty() {
return Err(error::ReadlineError::Eof);
} else if !input_state.is_emacs_mode() {
Expand Down Expand Up @@ -119,12 +124,10 @@ pub fn execute<H: Helper>(
kill_ring.kill(&text, Mode::Append);
}
}
Cmd::AcceptLine | Cmd::AcceptOrInsertLine { .. } | Cmd::Newline => {
if s.has_hint() || !s.is_default_prompt() {
// Force a refresh without hints to leave the previous
// line as the user typed it after a newline.
s.refresh_line_with_msg(None)?;
}
Cmd::Newline => {
s.edit_insert('\n', 1)?;
}
Cmd::AcceptLine | Cmd::AcceptOrInsertLine { .. } => {
let validation_result = s.validate()?;
let valid = validation_result.is_valid();
let end = s.line.is_end_of_input();
Expand All @@ -140,8 +143,7 @@ pub fn execute<H: Helper>(
) => {
return Ok(Submit);
}
(Cmd::Newline, ..)
| (Cmd::AcceptOrInsertLine { .. }, false, _)
(Cmd::AcceptOrInsertLine { .. }, false, _)
| (Cmd::AcceptOrInsertLine { .. }, true, false) => {
if valid || !validation_result.has_message() {
s.edit_insert('\n', 1)?;
Expand Down