From d8478ca690e7f7f99f52567d0386bc21612a8436 Mon Sep 17 00:00:00 2001 From: Reilly Wood <26268125+rgwood@users.noreply.github.com> Date: Sat, 25 Mar 2023 00:17:20 -0700 Subject: [PATCH] Clean up unnecessary macro use (#8607) Some minor code cleanup. We've accumulated a few macros over the years that arguably don't need to be macros. This PR removes 4 macros by either: 1. Inlining the macro 2. Replacing the macro with a local function 3. Replacing the macro with a closure --- crates/nu-cli/src/syntax_highlight.rs | 62 ++++++----- crates/nu-color-config/src/nu_style.rs | 32 +++--- crates/nu-color-config/src/style_computer.rs | 13 ++- crates/nu-explore/src/commands/help.rs | 26 +++-- crates/nu-protocol/src/value/mod.rs | 104 ++++++------------- 5 files changed, 96 insertions(+), 141 deletions(-) diff --git a/crates/nu-cli/src/syntax_highlight.rs b/crates/nu-cli/src/syntax_highlight.rs index 589cc719ba..77764599fd 100644 --- a/crates/nu-cli/src/syntax_highlight.rs +++ b/crates/nu-cli/src/syntax_highlight.rs @@ -79,29 +79,27 @@ impl Highlighter for NuHighlighter { }}; } - macro_rules! add_colored_token { - ($shape:expr, $text:expr) => { - output.push((get_shape_color($shape.to_string(), &self.config), $text)) - }; - } + let mut add_colored_token = |shape: &FlatShape, text: String| { + output.push((get_shape_color(shape.to_string(), &self.config), text)); + }; match shape.1 { - FlatShape::Garbage => add_colored_token!(shape.1, next_token), - FlatShape::Nothing => add_colored_token!(shape.1, next_token), - FlatShape::Binary => add_colored_token!(shape.1, next_token), - FlatShape::Bool => add_colored_token!(shape.1, next_token), - FlatShape::Int => add_colored_token!(shape.1, next_token), - FlatShape::Float => add_colored_token!(shape.1, next_token), - FlatShape::Range => add_colored_token!(shape.1, next_token), - FlatShape::InternalCall => add_colored_token!(shape.1, next_token), - FlatShape::External => add_colored_token!(shape.1, next_token), - FlatShape::ExternalArg => add_colored_token!(shape.1, next_token), - FlatShape::Literal => add_colored_token!(shape.1, next_token), - FlatShape::Operator => add_colored_token!(shape.1, next_token), - FlatShape::Signature => add_colored_token!(shape.1, next_token), - FlatShape::String => add_colored_token!(shape.1, next_token), - FlatShape::StringInterpolation => add_colored_token!(shape.1, next_token), - FlatShape::DateTime => add_colored_token!(shape.1, next_token), + FlatShape::Garbage => add_colored_token(&shape.1, next_token), + FlatShape::Nothing => add_colored_token(&shape.1, next_token), + FlatShape::Binary => add_colored_token(&shape.1, next_token), + FlatShape::Bool => add_colored_token(&shape.1, next_token), + FlatShape::Int => add_colored_token(&shape.1, next_token), + FlatShape::Float => add_colored_token(&shape.1, next_token), + FlatShape::Range => add_colored_token(&shape.1, next_token), + FlatShape::InternalCall => add_colored_token(&shape.1, next_token), + FlatShape::External => add_colored_token(&shape.1, next_token), + FlatShape::ExternalArg => add_colored_token(&shape.1, next_token), + FlatShape::Literal => add_colored_token(&shape.1, next_token), + FlatShape::Operator => add_colored_token(&shape.1, next_token), + FlatShape::Signature => add_colored_token(&shape.1, next_token), + FlatShape::String => add_colored_token(&shape.1, next_token), + FlatShape::StringInterpolation => add_colored_token(&shape.1, next_token), + FlatShape::DateTime => add_colored_token(&shape.1, next_token), FlatShape::List => { add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token) } @@ -116,17 +114,17 @@ impl Highlighter for NuHighlighter { add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token) } - FlatShape::Filepath => add_colored_token!(shape.1, next_token), - FlatShape::Directory => add_colored_token!(shape.1, next_token), - FlatShape::GlobPattern => add_colored_token!(shape.1, next_token), - FlatShape::Variable => add_colored_token!(shape.1, next_token), - FlatShape::Flag => add_colored_token!(shape.1, next_token), - FlatShape::Pipe => add_colored_token!(shape.1, next_token), - FlatShape::And => add_colored_token!(shape.1, next_token), - FlatShape::Or => add_colored_token!(shape.1, next_token), - FlatShape::Redirection => add_colored_token!(shape.1, next_token), - FlatShape::Custom(..) => add_colored_token!(shape.1, next_token), - FlatShape::MatchPattern => add_colored_token!(shape.1, next_token), + FlatShape::Filepath => add_colored_token(&shape.1, next_token), + FlatShape::Directory => add_colored_token(&shape.1, next_token), + FlatShape::GlobPattern => add_colored_token(&shape.1, next_token), + FlatShape::Variable => add_colored_token(&shape.1, next_token), + FlatShape::Flag => add_colored_token(&shape.1, next_token), + FlatShape::Pipe => add_colored_token(&shape.1, next_token), + FlatShape::And => add_colored_token(&shape.1, next_token), + FlatShape::Or => add_colored_token(&shape.1, next_token), + FlatShape::Redirection => add_colored_token(&shape.1, next_token), + FlatShape::Custom(..) => add_colored_token(&shape.1, next_token), + FlatShape::MatchPattern => add_colored_token(&shape.1, next_token), } last_seen_span = shape.0.end; } diff --git a/crates/nu-color-config/src/nu_style.rs b/crates/nu-color-config/src/nu_style.rs index 0d85726037..d84bf27667 100644 --- a/crates/nu-color-config/src/nu_style.rs +++ b/crates/nu-color-config/src/nu_style.rs @@ -20,22 +20,26 @@ impl From