mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-30 16:10:23 +00:00
merge_tools: function to extract all variables from tool arguments
To be used in the next commit
This commit is contained in:
parent
e743ba78b0
commit
ccd4f8e159
1 changed files with 41 additions and 11 deletions
|
@ -344,27 +344,39 @@ pub fn run_mergetool_external(
|
|||
Ok(tree_builder.write_tree())
|
||||
}
|
||||
|
||||
// Not interested in $UPPER_CASE_VARIABLES
|
||||
static VARIABLE_REGEX: once_cell::sync::Lazy<Regex> =
|
||||
once_cell::sync::Lazy::new(|| Regex::new(r"\$([a-z0-9_]+)\b").unwrap());
|
||||
|
||||
fn interpolate_variables<V: AsRef<str>>(
|
||||
args: &[String],
|
||||
variables: &HashMap<&str, V>,
|
||||
) -> 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()
|
||||
VARIABLE_REGEX
|
||||
.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()
|
||||
}
|
||||
|
||||
/// Return all variable names found in the args, without the dollar sign
|
||||
#[allow(unused)]
|
||||
fn find_all_variables(args: &[String]) -> Vec<String> {
|
||||
args.iter()
|
||||
.flat_map(|arg| VARIABLE_REGEX.find_iter(arg))
|
||||
.map(|single_match| single_match.as_str()[1..].to_owned())
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn edit_diff_external(
|
||||
editor: ExternalMergeTool,
|
||||
left_tree: &Tree,
|
||||
|
@ -515,4 +527,22 @@ mod tests {
|
|||
["$left $right"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_all_variables() {
|
||||
assert_eq!(
|
||||
find_all_variables(
|
||||
&[
|
||||
"$left",
|
||||
"$right",
|
||||
"--two=$1 and $2",
|
||||
"--can-be-part-of-string=$output",
|
||||
"$NOT_CAPITALS",
|
||||
"--can-repeat=$right"
|
||||
]
|
||||
.map(ToOwned::to_owned),
|
||||
),
|
||||
["left", "right", "1", "2", "output", "right"],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue