nu_cli/
validation.rs

1use nu_parser::parse;
2use nu_protocol::{
3    ParseError,
4    engine::{EngineState, StateWorkingSet},
5};
6use reedline::{ValidationResult, Validator};
7use std::sync::Arc;
8
9pub struct NuValidator {
10    pub engine_state: Arc<EngineState>,
11}
12
13impl Validator for NuValidator {
14    fn validate(&self, line: &str) -> ValidationResult {
15        let mut working_set = StateWorkingSet::new(&self.engine_state);
16        parse(&mut working_set, None, line.as_bytes(), false);
17
18        if matches!(
19            working_set.parse_errors.first(),
20            Some(ParseError::UnexpectedEof(..))
21        ) {
22            ValidationResult::Incomplete
23        } else {
24            ValidationResult::Complete
25        }
26    }
27}