Skip to main content

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        // Unclosed delimiters and unexpected EOF both mean the user may still be
19        // typing a multi-line construct (e.g. an open `{` in the REPL).
20        // Unbalanced closers are complete but wrong inputs — keep them Complete
21        // so the REPL submits the line and shows the diagnostic.
22        if matches!(
23            working_set.parse_errors.first(),
24            Some(ParseError::UnexpectedEof(..) | ParseError::Unclosed(..))
25        ) {
26            ValidationResult::Incomplete
27        } else {
28            ValidationResult::Complete
29        }
30    }
31}