multiline validation

This commit is contained in:
JT
2021-09-22 17:29:53 +12:00
parent 9f9bec38e1
commit 0ccbebee7a
6 changed files with 47 additions and 13 deletions

View File

@ -1,7 +1,9 @@
mod completions;
mod errors;
mod syntax_highlight;
mod validation;
pub use completions::NuCompleter;
pub use errors::report_error;
pub use syntax_highlight::NuHighlighter;
pub use validation::NuValidator;

View File

@ -0,0 +1,23 @@
use std::{cell::RefCell, rc::Rc};
use nu_parser::{parse, ParseError};
use nu_protocol::engine::{EngineState, StateWorkingSet};
use reedline::{ValidationResult, Validator};
pub struct NuValidator {
pub engine_state: Rc<RefCell<EngineState>>,
}
impl Validator for NuValidator {
fn validate(&self, line: &str) -> ValidationResult {
let engine_state = self.engine_state.borrow();
let mut working_set = StateWorkingSet::new(&*engine_state);
let (_, err) = parse(&mut working_set, None, line.as_bytes(), false);
if matches!(err, Some(ParseError::UnexpectedEof(..))) {
ValidationResult::Incomplete
} else {
ValidationResult::Complete
}
}
}

View File

@ -21,7 +21,7 @@ pub enum ParseError {
#[error("Unexpected end of code.")]
#[diagnostic(code(nu::parser::unexpected_eof), url(docsrs))]
UnexpectedEof(String, #[label("expected {0}")] Span),
UnexpectedEof(String, #[label("expected closing {0}")] Span),
#[error("Unclosed delimiter.")]
#[diagnostic(code(nu::parser::unclosed_delimiter), url(docsrs))]

View File

@ -181,7 +181,13 @@ pub fn lex_item(
// correct information from the non-lite parse.
return (
span,
Some(ParseError::UnexpectedEof((delim as char).to_string(), span)),
Some(ParseError::UnexpectedEof(
(delim as char).to_string(),
Span {
start: span.end,
end: span.end,
},
)),
);
}