From f5121a5c786841035637d825bca7fa0211ab884c Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Thu, 17 Oct 2024 09:15:42 -0500 Subject: [PATCH] add `like` and `not-like` operators as synonyms for the regex operators `=~` and `!~` (#14072) # Description This PR adds `like` as a synonym for `=~` and `not-like` as a synonym for `!~`. This is mainly a quality-of-life change to help those people who think in sql. ![image](https://github.com/user-attachments/assets/a0b142cd-30c9-487d-b755-d6da0a0874ec) closes #13261 # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-cli/src/completions/operator_completions.rs | 2 ++ crates/nu-parser/src/parser.rs | 4 ++-- crates/nu-protocol/src/ast/operator.rs | 4 ++-- crates/nu-std/std/help/mod.nu | 6 +++--- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/nu-cli/src/completions/operator_completions.rs b/crates/nu-cli/src/completions/operator_completions.rs index e9e95d0799..460e7f590b 100644 --- a/crates/nu-cli/src/completions/operator_completions.rs +++ b/crates/nu-cli/src/completions/operator_completions.rs @@ -67,7 +67,9 @@ impl Completer for OperatorCompletion { ], Expr::String(_) => vec![ ("=~", "Contains regex match"), + ("like", "Contains regex match"), ("!~", "Does not contain regex match"), + ("not-like", "Does not contain regex match"), ( "++", "Appends two lists, a list and a value, two strings, or two binary values", diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 64a5086628..d2ed11188a 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -5018,8 +5018,8 @@ pub fn parse_operator(working_set: &mut StateWorkingSet, span: Span) -> Expressi b"<=" => Operator::Comparison(Comparison::LessThanOrEqual), b">" => Operator::Comparison(Comparison::GreaterThan), b">=" => Operator::Comparison(Comparison::GreaterThanOrEqual), - b"=~" => Operator::Comparison(Comparison::RegexMatch), - b"!~" => Operator::Comparison(Comparison::NotRegexMatch), + b"=~" | b"like" => Operator::Comparison(Comparison::RegexMatch), + b"!~" | b"not-like" => Operator::Comparison(Comparison::NotRegexMatch), b"+" => Operator::Math(Math::Plus), b"++" => Operator::Math(Math::Append), b"-" => Operator::Math(Math::Minus), diff --git a/crates/nu-protocol/src/ast/operator.rs b/crates/nu-protocol/src/ast/operator.rs index 713c6c0060..8c2e59d93a 100644 --- a/crates/nu-protocol/src/ast/operator.rs +++ b/crates/nu-protocol/src/ast/operator.rs @@ -115,8 +115,8 @@ impl Display for Operator { Operator::Comparison(Comparison::NotEqual) => write!(f, "!="), Operator::Comparison(Comparison::LessThan) => write!(f, "<"), Operator::Comparison(Comparison::GreaterThan) => write!(f, ">"), - Operator::Comparison(Comparison::RegexMatch) => write!(f, "=~"), - Operator::Comparison(Comparison::NotRegexMatch) => write!(f, "!~"), + Operator::Comparison(Comparison::RegexMatch) => write!(f, "=~ or like"), + Operator::Comparison(Comparison::NotRegexMatch) => write!(f, "!~ or not-like"), Operator::Comparison(Comparison::LessThanOrEqual) => write!(f, "<="), Operator::Comparison(Comparison::GreaterThanOrEqual) => write!(f, ">="), Operator::Comparison(Comparison::StartsWith) => write!(f, "starts-with"), diff --git a/crates/nu-std/std/help/mod.nu b/crates/nu-std/std/help/mod.nu index d608ae8bcb..089bbff7f8 100644 --- a/crates/nu-std/std/help/mod.nu +++ b/crates/nu-std/std/help/mod.nu @@ -47,8 +47,8 @@ def get-all-operators [] { return [ [Comparison, <=, LessThanOrEqual, "Checks if a value is less than or equal to another.", 80] [Comparison, >, GreaterThan, "Checks if a value is greater than another.", 80] [Comparison, >=, GreaterThanOrEqual, "Checks if a value is greater than or equal to another.", 80] - [Comparison, =~, RegexMatch, "Checks if a value matches a regular expression.", 80] - [Comparison, !~, NotRegexMatch, "Checks if a value does not match a regular expression.", 80] + [Comparison, '=~ or like', RegexMatch, "Checks if a value matches a regular expression.", 80] + [Comparison, '!~ or not-like', NotRegexMatch, "Checks if a value does not match a regular expression.", 80] [Comparison, in, In, "Checks if a value is in a list or string.", 80] [Comparison, not-in, NotIn, "Checks if a value is not in a list or string.", 80] [Comparison, starts-with, StartsWith, "Checks if a string starts with another.", 80] @@ -771,7 +771,7 @@ You can also learn more at (ansi default_italic)(ansi light_cyan_underline)https let modules = (try { modules $target_item --find $find }) if not ($modules | is-empty) { return $modules } - + if ($find | is-not-empty) { print -e $"No help results found mentioning: ($find)" return []