diff --git a/crates/nu-cli/src/shell/helper.rs b/crates/nu-cli/src/shell/helper.rs index af4f3bb58..3c67149b1 100644 --- a/crates/nu-cli/src/shell/helper.rs +++ b/crates/nu-cli/src/shell/helper.rs @@ -15,6 +15,7 @@ pub struct Helper { hinter: Option, context: Context, pub colored_prompt: String, + validator: NuValidator, } impl Helper { @@ -24,6 +25,7 @@ impl Helper { hinter, context, colored_prompt: String::new(), + validator: NuValidator {}, } } } @@ -90,6 +92,40 @@ impl rustyline::highlight::Highlighter for Helper { } } +impl rustyline::validate::Validator for Helper { + fn validate( + &self, + ctx: &mut rustyline::validate::ValidationContext, + ) -> rustyline::Result { + self.validator.validate(ctx) + } + + fn validate_while_typing(&self) -> bool { + self.validator.validate_while_typing() + } +} + +struct NuValidator {} + +impl rustyline::validate::Validator for NuValidator { + fn validate( + &self, + ctx: &mut rustyline::validate::ValidationContext, + ) -> rustyline::Result { + let src = ctx.input(); + + let lite_result = nu_parser::lite_parse(src, 0); + + if let Err(err) = lite_result { + if let nu_errors::ParseErrorReason::Eof { .. } = err.cause.reason() { + return Ok(rustyline::validate::ValidationResult::Incomplete); + } + } + + Ok(rustyline::validate::ValidationResult::Valid(None)) + } +} + #[allow(unused)] fn vec_tag(input: Vec>) -> Option { let mut iter = input.iter(); @@ -189,7 +225,3 @@ impl Painter { } impl rustyline::Helper for Helper {} - -// Use default validator for normal single line behaviour -// In the future we can implement this for custom multi-line support -impl rustyline::validate::Validator for Helper {}