mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 06:30:08 +02:00
multiline validation
This commit is contained in:
@ -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;
|
||||
|
23
crates/nu-cli/src/validation.rs
Normal file
23
crates/nu-cli/src/validation.rs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
@ -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))]
|
||||
|
@ -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,
|
||||
},
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user