Some error improvements (#659)

This commit is contained in:
JT
2022-01-04 10:14:33 +11:00
committed by GitHub
parent cb8b7e08a5
commit b6fcd46075
6 changed files with 145 additions and 66 deletions

View File

@ -274,6 +274,33 @@ impl Signature {
self
}
pub fn call_signature(&self) -> String {
let mut one_liner = String::new();
one_liner.push_str(&self.name);
one_liner.push(' ');
for positional in &self.required_positional {
one_liner.push_str(&get_positional_short_name(positional, true));
}
for positional in &self.optional_positional {
one_liner.push_str(&get_positional_short_name(positional, false));
}
if self.rest_positional.is_some() {
one_liner.push_str("...args ");
}
// if !self.subcommands.is_empty() {
// one_liner.push_str("<subcommand> ");
// }
if !self.named.is_empty() {
one_liner.push_str("{flags} ");
}
one_liner
}
/// Get list of the short-hand flags
pub fn get_shorts(&self) -> Vec<char> {
self.named.iter().filter_map(|f| f.short).collect()
@ -431,6 +458,25 @@ impl Command for Predeclaration {
}
}
fn get_positional_short_name(arg: &PositionalArg, is_required: bool) -> String {
match &arg.shape {
SyntaxShape::Keyword(name, ..) => {
if is_required {
format!("{} <{}> ", String::from_utf8_lossy(name), arg.name)
} else {
format!("({} <{}>) ", String::from_utf8_lossy(name), arg.name)
}
}
_ => {
if is_required {
format!("<{}> ", arg.name)
} else {
format!("({}) ", arg.name)
}
}
}
}
#[derive(Clone)]
struct BlockCommand {
signature: Signature,

View File

@ -46,6 +46,15 @@ impl Span {
pub fn contains(&self, pos: usize) -> bool {
pos >= self.start && pos < self.end
}
/// Point to the space just past this span, useful for missing
/// values
pub fn past(&self) -> Span {
Span {
start: self.end,
end: self.end,
}
}
}
/// Used when you have a slice of spans of at least size 1