ok/jj
1
0
Fork 0
forked from mirrors/jj

templater: Upper and lowercase ids and strings

This commit is contained in:
Ilya Grigoriev 2023-02-20 20:38:52 -08:00
parent 643fe9a218
commit 00b18bcd18
3 changed files with 57 additions and 0 deletions

View file

@ -382,6 +382,21 @@ impl Template<()> for ShortestIdPrefix {
}
}
impl ShortestIdPrefix {
fn to_upper(&self) -> Self {
Self {
prefix: self.prefix.to_ascii_uppercase(),
rest: self.rest.to_ascii_uppercase(),
}
}
fn to_lower(&self) -> Self {
Self {
prefix: self.prefix.to_ascii_lowercase(),
rest: self.rest.to_ascii_lowercase(),
}
}
}
fn build_shortest_id_prefix_method<'repo>(
language: &CommitTemplateLanguage<'repo, '_>,
self_property: impl TemplateProperty<Commit, Output = ShortestIdPrefix> + 'repo,
@ -402,6 +417,20 @@ fn build_shortest_id_prefix_method<'repo>(
TemplatePropertyFn(|id: &ShortestIdPrefix| id.rest.clone()),
))
}
"upper" => {
template_parser::expect_no_arguments(function)?;
language.wrap_shortest_id_prefix(template_parser::chain_properties(
self_property,
TemplatePropertyFn(|id: &ShortestIdPrefix| id.to_upper()),
))
}
"lower" => {
template_parser::expect_no_arguments(function)?;
language.wrap_shortest_id_prefix(template_parser::chain_properties(
self_property,
TemplatePropertyFn(|id: &ShortestIdPrefix| id.to_lower()),
))
}
_ => {
return Err(TemplateParseError::no_such_method(
"ShortestIdPrefix",

View file

@ -906,6 +906,20 @@ fn build_string_method<'a, L: TemplateLanguage<'a>>(
TemplatePropertyFn(|s: &String| s.lines().next().unwrap_or_default().to_string()),
))
}
"upper" => {
expect_no_arguments(function)?;
language.wrap_string(chain_properties(
self_property,
TemplatePropertyFn(|s: &String| s.to_uppercase()),
))
}
"lower" => {
expect_no_arguments(function)?;
language.wrap_string(chain_properties(
self_property,
TemplatePropertyFn(|s: &String| s.to_lowercase()),
))
}
_ => return Err(TemplateParseError::no_such_method("String", function)),
};
Ok(property)

View file

@ -388,6 +388,20 @@ fn test_templater_separate_function() {
render(r#"separate(author, "X", "Y", "Z")"#), @"X <>Y <>Z");
}
#[test]
fn test_templater_upper_lower() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
let render = |template| get_colored_template_output(&test_env, &repo_path, "@-", template);
insta::assert_snapshot!(
render(r#"change_id.shortest(4).upper() change_id.shortest(4).upper().lower()"#),
@"ZZZZzzzz");
insta::assert_snapshot!(
render(r#""Hello".upper() "Hello".lower()"#), @"HELLOhello");
}
#[test]
fn test_templater_alias() {
let test_env = TestEnvironment::default();