From 73312b506fa2fa6e28405bf9ce9f6337ab1cd003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Wed, 12 Feb 2020 20:20:19 -0500 Subject: [PATCH] Finer grained parsing and coloring command tail. (#1382) --- crates/nu-parser/src/parse/token_tree.rs | 17 ++++++++--------- crates/nu-parser/src/parse_command.rs | 4 ++++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/crates/nu-parser/src/parse/token_tree.rs b/crates/nu-parser/src/parse/token_tree.rs index ba3c50254..c49c7394f 100644 --- a/crates/nu-parser/src/parse/token_tree.rs +++ b/crates/nu-parser/src/parse/token_tree.rs @@ -363,16 +363,15 @@ impl SpannedToken { pub(crate) fn as_flag(&self, value: &str, short: Option, source: &Text) -> Option { match self.unspanned() { - Token::Flag(flag @ Flag { .. }) => { + Token::Flag(flag) => { let name = flag.name().slice(source); - let is_long = flag.kind == FlagKind::Longhand && value == name; - let is_short = flag.kind == FlagKind::Shorthand - && short.is_some() - && short == name.chars().nth(0); - if is_long || is_short { - Some(*flag) - } else { - None + + match flag.kind { + FlagKind::Longhand if value == name => Some(*flag), + FlagKind::Shorthand if short.is_some() && short == name.chars().next() => { + Some(*flag) + } + _ => None, } } _ => None, diff --git a/crates/nu-parser/src/parse_command.rs b/crates/nu-parser/src/parse_command.rs index 2a2ee1b58..fdaf099dc 100644 --- a/crates/nu-parser/src/parse_command.rs +++ b/crates/nu-parser/src/parse_command.rs @@ -42,6 +42,7 @@ pub fn parse_command_tail( tail.color_shape(flag.color(flag.span)); tail.move_to(pos); tail.expand_infallible(MaybeSpaceShape); + tail.move_to(0); } } } @@ -86,6 +87,7 @@ pub fn parse_command_tail( Ok(expr) => { named.insert_optional(name, Some(expr)); rest_signature.remove_named(name); + tail.move_to(pos); } Err(_) => { found_error = Some(ParseError::argument_error( @@ -192,6 +194,8 @@ pub fn continue_parsing_positionals( ) -> Result, ParseError> { let mut positional = vec![]; + eat_any_whitespace(tail); + for arg in &config.positional { trace!(target: "nu::parse::trace_remaining", "Processing positional {:?}", arg);