nushell/crates/nu-cli/src/validation.rs

28 lines
706 B
Rust
Raw Normal View History

use nu_parser::parse;
use nu_protocol::{
engine::{EngineState, StateWorkingSet},
ParseError,
};
2021-09-22 05:29:53 +00:00
use reedline::{ValidationResult, Validator};
use std::sync::Arc;
2021-09-22 05:29:53 +00:00
pub struct NuValidator {
pub engine_state: Arc<EngineState>,
2021-09-22 05:29:53 +00:00
}
impl Validator for NuValidator {
fn validate(&self, line: &str) -> ValidationResult {
2021-10-25 06:31:39 +00:00
let mut working_set = StateWorkingSet::new(&self.engine_state);
parse(&mut working_set, None, line.as_bytes(), false);
2021-09-22 05:29:53 +00:00
if matches!(
working_set.parse_errors.first(),
Some(ParseError::UnexpectedEof(..))
) {
2021-09-22 05:29:53 +00:00
ValidationResult::Incomplete
} else {
ValidationResult::Complete
}
}
}