From 3008434c0f7037d422307e4474cf4a7cb619ddec Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 25 Nov 2019 11:03:01 -0800 Subject: [PATCH] Eliminate repetitive code and fix Unix failure --- .cargo/config | 3 + TODO.md | 2 - src/data/files.rs | 2 +- .../hir/syntax_shape/expression/number.rs | 4 +- src/parser/parse_command.rs | 92 ++++++++----------- 5 files changed, 47 insertions(+), 56 deletions(-) diff --git a/.cargo/config b/.cargo/config index e69de29bb2..fd2875ddab 100644 --- a/.cargo/config +++ b/.cargo/config @@ -0,0 +1,3 @@ +[build] + +#rustflags = ["--cfg", "coloring_in_tokens"] \ No newline at end of file diff --git a/TODO.md b/TODO.md index 58456b5605..cc7c1e5ec5 100644 --- a/TODO.md +++ b/TODO.md @@ -33,8 +33,6 @@ This pattern is extremely repetitive and can be abstracted: ``` -Do we need Display impls? - Mandatory and Optional in parse_command trace_remaining? diff --git a/src/data/files.rs b/src/data/files.rs index d7ae1ace7e..3a2a99ee31 100644 --- a/src/data/files.rs +++ b/src/data/files.rs @@ -38,7 +38,7 @@ pub(crate) fn dir_entry_dict( { use std::os::unix::fs::PermissionsExt; let mode = metadata.permissions().mode(); - dict.insert( + dict.insert_untagged( "mode", UntaggedValue::string(umask::Mode::from(mode).to_string()), ); diff --git a/src/parser/hir/syntax_shape/expression/number.rs b/src/parser/hir/syntax_shape/expression/number.rs index a326ed27a7..c4fce3a36a 100644 --- a/src/parser/hir/syntax_shape/expression/number.rs +++ b/src/parser/hir/syntax_shape/expression/number.rs @@ -26,7 +26,9 @@ impl ExpandExpression for NumberShape { ) -> Result { parse_single_node(token_nodes, "Number", |token, token_span, err| { Ok(match token { - UnspannedToken::GlobPattern | UnspannedToken::Operator(..) => return Err(err.error()), + UnspannedToken::GlobPattern | UnspannedToken::Operator(..) => { + return Err(err.error()) + } UnspannedToken::Variable(tag) if tag.slice(context.source) == "it" => { hir::Expression::it_variable(tag, token_span) } diff --git a/src/parser/parse_command.rs b/src/parser/parse_command.rs index 1b9da910b1..41aff543f8 100644 --- a/src/parser/parse_command.rs +++ b/src/parser/parse_command.rs @@ -389,9 +389,47 @@ impl ColorSyntax for CommandTailShape { token_nodes: &'b mut TokensIterator<'a>, context: &ExpandContext, ) -> Self::Info { + use crate::parser::hir::syntax_shape::SyntaxShape; + let mut args = ColoringArgs::new(token_nodes.len()); trace_remaining("nodes", &token_nodes, context.source()); + fn insert_flag( + token_nodes: &mut TokensIterator, + syntax_type: &SyntaxShape, + args: &mut ColoringArgs, + flag: Flag, + pos: usize, + context: &ExpandContext, + ) { + let (_, shapes) = token_nodes.atomic_returning_shapes(|token_nodes| { + token_nodes.color_shape(flag.color()); + token_nodes.move_to(pos); + + if token_nodes.at_end() { + return Ok(()); + } + + // We still want to color the flag even if the following tokens don't match, so don't + // propagate the error to the parent atomic block if it fails + let _ = token_nodes.atomic(|token_nodes| { + // We can live with unmatched syntax after a mandatory flag + color_syntax(&MaybeSpaceShape, token_nodes, context); + + // If the part after a mandatory flag isn't present, that's ok, but we + // should roll back any whitespace we chomped + color_fallible_syntax(syntax_type, token_nodes, context)?; + + Ok(()) + }); + + Ok(()) + }); + + args.insert(pos, shapes); + token_nodes.restart(); + } + for (name, kind) in &signature.named { trace!(target: "nu::color_syntax", "looking for {} : {:?}", name, kind); @@ -414,32 +452,7 @@ impl ColorSyntax for CommandTailShape { // The mandatory flag didn't exist at all, so there's nothing to color } Ok((pos, flag)) => { - let (_, shapes) = token_nodes.atomic_returning_shapes(|token_nodes| { - token_nodes.color_shape(flag.color()); - token_nodes.move_to(pos); - - if token_nodes.at_end() { - return Ok(()); - } - - // We still want to color the flag even if the following tokens don't match, so don't - // propagate the error to the parent atomic block if it fails - let _ = token_nodes.atomic(|token_nodes| { - // We can live with unmatched syntax after a mandatory flag - color_syntax(&MaybeSpaceShape, token_nodes, context); - - // If the part after a mandatory flag isn't present, that's ok, but we - // should roll back any whitespace we chomped - color_fallible_syntax(syntax_type, token_nodes, context)?; - - Ok(()) - }); - - Ok(()) - }); - - args.insert(pos, shapes); - token_nodes.restart(); + insert_flag(token_nodes, syntax_type, &mut args, flag, pos, context) } } } @@ -449,32 +462,7 @@ impl ColorSyntax for CommandTailShape { // The optional flag didn't exist at all, so there's nothing to color } Ok(Some((pos, flag))) => { - let (_, shapes) = token_nodes.atomic_returning_shapes(|token_nodes| { - token_nodes.color_shape(flag.color()); - token_nodes.move_to(pos); - - if token_nodes.at_end() { - return Ok(()); - } - - // We still want to color the flag even if the following tokens don't match, so don't - // propagate the error to the parent atomic block if it fails - let _ = token_nodes.atomic(|token_nodes| { - // We can live with unmatched syntax after a mandatory flag - color_syntax(&MaybeSpaceShape, token_nodes, context); - - // If the part after a mandatory flag isn't present, that's ok, but we - // should roll back any whitespace we chomped - color_fallible_syntax(syntax_type, token_nodes, context)?; - - Ok(()) - }); - - Ok(()) - }); - - args.insert(pos, shapes); - token_nodes.restart(); + insert_flag(token_nodes, syntax_type, &mut args, flag, pos, context) } Ok(None) => {