Skip to content

Commit

Permalink
merge_tools: extract a more generic interpolate_variables (refactor…
Browse files Browse the repository at this point in the history
…) TODO: simplify

This will be used in the subsequent commit
  • Loading branch information
ilyagr committed Aug 15, 2023
1 parent 9a2a8f8 commit dbc0766
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions cli/src/merge_tools/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,24 +347,29 @@ pub fn run_mergetool_external(
fn interpolate_variables<V: AsRef<str>>(
args: &[String],
variables: &HashMap<&str, V>,
) -> Vec<String> {
interpolate_variables_with(args, |caps: &Captures| {
let name = &caps[1];
if let Some(subst) = variables.get(name) {
subst.as_ref().to_owned()
} else {
caps[0].to_owned()
}
})
}

fn interpolate_variables_with(
args: &[String],
mut f: impl FnMut(&Captures) -> String,
) -> Vec<String> {
// Not interested in $UPPER_CASE_VARIABLES
let re = Regex::new(r"\$([a-z0-9_]+)\b").unwrap();
args.iter()
.map(|arg| {
re.replace_all(arg, |caps: &Captures| {
let name = &caps[1];
if let Some(subst) = variables.get(name) {
subst.as_ref().to_owned()
} else {
caps[0].to_owned()
}
})
.into_owned()
})
.collect()
let mut result = vec![];
for arg in args {
result.push(re.replace_all(arg, &mut f).into_owned());
}
result
}

pub fn edit_diff_external(
editor: ExternalMergeTool,
left_tree: &Tree,
Expand Down

0 comments on commit dbc0766

Please sign in to comment.