mirror of
https://github.com/nushell/nushell.git
synced 2024-11-26 10:23:52 +01:00
Add some improvements to errors
This commit is contained in:
parent
e5aa8b9d3f
commit
4841d62d76
3
TODO.md
3
TODO.md
@ -21,6 +21,7 @@
|
||||
- [x] Simple completions
|
||||
- [x] Detecting `$it` currently only looks at top scope but should find any free `$it` in the expression (including subexprs)
|
||||
- [x] Signature needs to make parameters visible in scope before block is parsed
|
||||
- [ ] Support for `$in`
|
||||
- [ ] Value serialization
|
||||
- [ ] Handling rows with missing columns during a cell path
|
||||
- [ ] Error shortcircuit (stopping on first error)
|
||||
@ -28,7 +29,7 @@
|
||||
- [ ] operator overflow
|
||||
- [ ] finish operator type-checking
|
||||
- [ ] Source
|
||||
- [ ] Autoenv
|
||||
- [ ] Overlays (replacement for `autoenv`)
|
||||
- [ ] Externals
|
||||
- [ ] let [first, rest] = [1, 2, 3] (design question: how do you pattern match a table?)
|
||||
|
||||
|
@ -78,9 +78,9 @@ pub enum ParseError {
|
||||
#[diagnostic(code(nu::parser::non_utf8), url(docsrs))]
|
||||
NonUtf8(#[label = "non-UTF8 code"] Span),
|
||||
|
||||
#[error("Unknown flag.")]
|
||||
#[error("The `{0}` command doesn't have flag `{1}`.")]
|
||||
#[diagnostic(code(nu::parser::unknown_flag), url(docsrs))]
|
||||
UnknownFlag(#[label = "unknown flag"] Span),
|
||||
UnknownFlag(String, String, #[label = "unknown flag"] Span),
|
||||
|
||||
#[error("Unknown type.")]
|
||||
#[diagnostic(code(nu::parser::unknown_type), url(docsrs))]
|
||||
|
@ -160,9 +160,13 @@ fn parse_long_flag(
|
||||
}
|
||||
} else {
|
||||
(
|
||||
Some(long_name),
|
||||
Some(long_name.clone()),
|
||||
None,
|
||||
Some(ParseError::UnknownFlag(arg_span)),
|
||||
Some(ParseError::UnknownFlag(
|
||||
sig.name.clone(),
|
||||
long_name.clone(),
|
||||
arg_span,
|
||||
)),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
@ -214,17 +218,45 @@ fn parse_short_flags(
|
||||
if String::from_utf8_lossy(arg_contents).parse::<f64>().is_ok() {
|
||||
return (None, None);
|
||||
} else if let Some(first) = unmatched_short_flags.first() {
|
||||
error = error.or(Some(ParseError::UnknownFlag(*first)));
|
||||
let contents = working_set.get_span_contents(*first);
|
||||
error = error.or_else(|| {
|
||||
Some(ParseError::UnknownFlag(
|
||||
sig.name.clone(),
|
||||
format!("-{}", String::from_utf8_lossy(contents).to_string()),
|
||||
*first,
|
||||
))
|
||||
});
|
||||
}
|
||||
} else if let Some(first) = unmatched_short_flags.first() {
|
||||
error = error.or(Some(ParseError::UnknownFlag(*first)));
|
||||
let contents = working_set.get_span_contents(*first);
|
||||
error = error.or_else(|| {
|
||||
Some(ParseError::UnknownFlag(
|
||||
sig.name.clone(),
|
||||
format!("-{}", String::from_utf8_lossy(contents).to_string()),
|
||||
*first,
|
||||
))
|
||||
});
|
||||
}
|
||||
} else if let Some(first) = unmatched_short_flags.first() {
|
||||
error = error.or(Some(ParseError::UnknownFlag(*first)));
|
||||
let contents = working_set.get_span_contents(*first);
|
||||
error = error.or_else(|| {
|
||||
Some(ParseError::UnknownFlag(
|
||||
sig.name.clone(),
|
||||
format!("-{}", String::from_utf8_lossy(contents).to_string()),
|
||||
*first,
|
||||
))
|
||||
});
|
||||
}
|
||||
} else if !unmatched_short_flags.is_empty() {
|
||||
if let Some(first) = unmatched_short_flags.first() {
|
||||
error = error.or(Some(ParseError::UnknownFlag(*first)));
|
||||
let contents = working_set.get_span_contents(*first);
|
||||
error = error.or_else(|| {
|
||||
Some(ParseError::UnknownFlag(
|
||||
sig.name.clone(),
|
||||
format!("-{}", String::from_utf8_lossy(contents).to_string()),
|
||||
*first,
|
||||
))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,6 +32,7 @@ pub fn math_result_type(
|
||||
(Type::Unknown, _) => (Type::Unknown, None),
|
||||
(_, Type::Unknown) => (Type::Unknown, None),
|
||||
(Type::Int, _) => {
|
||||
let ty = rhs.ty.clone();
|
||||
*rhs = Expression::garbage(rhs.span);
|
||||
(
|
||||
Type::Unknown,
|
||||
@ -40,7 +41,7 @@ pub fn math_result_type(
|
||||
lhs.span,
|
||||
lhs.ty.clone(),
|
||||
rhs.span,
|
||||
rhs.ty.clone(),
|
||||
ty,
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
@ -574,6 +574,16 @@ impl<'a> miette::SourceCode for &StateWorkingSet<'a> {
|
||||
let content_span = span_contents.span();
|
||||
// Back to "global" indexing
|
||||
let retranslated = (content_span.offset() + start, content_span.len()).into();
|
||||
|
||||
if filename == "<cli>" {
|
||||
return Ok(Box::new(miette::MietteSpanContents::new(
|
||||
span_contents.data(),
|
||||
retranslated,
|
||||
span_contents.line(),
|
||||
span_contents.column(),
|
||||
span_contents.line_count(),
|
||||
)));
|
||||
} else {
|
||||
return Ok(Box::new(miette::MietteSpanContents::new_named(
|
||||
filename.clone(),
|
||||
span_contents.data(),
|
||||
@ -584,6 +594,7 @@ impl<'a> miette::SourceCode for &StateWorkingSet<'a> {
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(miette::MietteError::OutOfBounds)
|
||||
}
|
||||
}
|
||||
|
10
src/main.rs
10
src/main.rs
@ -71,7 +71,6 @@ fn main() -> Result<()> {
|
||||
));
|
||||
|
||||
let prompt = DefaultPrompt::new(1);
|
||||
let mut current_line = 1;
|
||||
let stack = nu_protocol::engine::Stack::new();
|
||||
|
||||
loop {
|
||||
@ -97,12 +96,8 @@ fn main() -> Result<()> {
|
||||
let (block, delta) = {
|
||||
let engine_state = engine_state.borrow();
|
||||
let mut working_set = StateWorkingSet::new(&*engine_state);
|
||||
let (output, err) = parse(
|
||||
&mut working_set,
|
||||
Some(&format!("line_{}", current_line)),
|
||||
s.as_bytes(),
|
||||
false,
|
||||
);
|
||||
let (output, err) =
|
||||
parse(&mut working_set, Some("<cli>"), s.as_bytes(), false);
|
||||
if let Some(err) = err {
|
||||
report_error(&working_set, &err);
|
||||
continue;
|
||||
@ -145,7 +140,6 @@ fn main() -> Result<()> {
|
||||
}
|
||||
}
|
||||
}
|
||||
current_line += 1;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
Loading…
Reference in New Issue
Block a user