From 0dc24bc21d5c06cf93cef72701eac616bb172079 Mon Sep 17 00:00:00 2001 From: iff Date: Tue, 19 Nov 2024 03:18:54 +0100 Subject: [PATCH] fix: multiple capture matches --- rules/cargo.toml | 3 ++- src/suggestions.rs | 37 ++++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/rules/cargo.toml b/rules/cargo.toml index 5b6f076..4a54bdf 100644 --- a/rules/cargo.toml +++ b/rules/cargo.toml @@ -2,10 +2,11 @@ command = "cargo" [[match_err]] pattern = [ - "did you mean" + "no such command" ] suggest = [ ''' +#[err_contains(did you mean)] {{command[0]}} {{err::(?:did you mean `)(.*)(?:`)}} {{command[2:]}} ''' ] diff --git a/src/suggestions.rs b/src/suggestions.rs index 3e2bec9..50a93be 100644 --- a/src/suggestions.rs +++ b/src/suggestions.rs @@ -80,35 +80,38 @@ pub fn check_executable(shell: &str, executable: &str) -> bool { pub fn opt_regex(regex: &str, command: &mut String) -> String { let regex = Regex::new(regex).unwrap(); - let opts = regex - .captures_iter(command) - .map(|cap| cap.get(1).unwrap().as_str().to_owned()) - .collect::>(); - for opt in opts.clone() { - *command = command.replace(&opt, ""); - } - opts.join(" ") + let mut opt = Vec::new(); + for captures in regex.captures_iter(command) { + for cap in captures.iter().skip(1).flatten() { + opt.push(cap.as_str().to_owned()); + } + } + opt.join(" ") } pub fn err_regex(regex: &str, error_msg: &str) -> String { let regex = Regex::new(regex).unwrap(); - let err = regex - .captures_iter(error_msg) - .map(|cap| cap.get(1).unwrap().as_str().to_owned()) - .collect::>(); + let mut err = Vec::new(); + for captures in regex.captures_iter(error_msg) { + for cap in captures.iter().skip(1).flatten() { + err.push(cap.as_str().to_owned()); + } + } err.join(" ") } pub fn cmd_regex(regex: &str, command: &str) -> String { let regex = Regex::new(regex).unwrap(); - let err = regex - .captures_iter(command) - .map(|cap| cap.get(1).unwrap().as_str().to_owned()) - .collect::>(); - err.join(" ") + let mut cmd = Vec::new(); + for captures in regex.captures_iter(command) { + for cap in captures.iter().skip(1).flatten() { + cmd.push(cap.as_str().to_owned()); + } + } + cmd.join(" ") } pub fn eval_shell_command(shell: &str, command: &str) -> Vec {