templater: add concat(contents..) function, migrate default templates

Multi-line templates looked a bit ugly if I replaced all implicit concats
with ++ operations.
This commit is contained in:
Yuya Nishihara 2023-02-09 19:46:55 +09:00
parent 433623c138
commit fd27d228ed
3 changed files with 60 additions and 34 deletions

View file

@ -8,38 +8,39 @@ separate(" ",
log = '''
label(if(current_working_copy, "working_copy"),
separate(" ",
if(divergent,
label("divergent", format_short_change_id(change_id) "??"),
format_short_change_id(change_id)),
format_short_signature(author),
format_timestamp(committer.timestamp()),
branches,
tags,
working_copies,
git_head,
format_short_commit_id(commit_id),
if(conflict, label("conflict", "conflict")),
)
"\n"
separate(" ",
if(empty, label("empty", "(empty)")),
if(description, description.first_line(), description_placeholder),
)
"\n"
concat(
separate(" ",
if(divergent,
label("divergent", format_short_change_id(change_id) "??"),
format_short_change_id(change_id)),
format_short_signature(author),
format_timestamp(committer.timestamp()),
branches,
tags,
working_copies,
git_head,
format_short_commit_id(commit_id),
if(conflict, label("conflict", "conflict")),
) "\n",
separate(" ",
if(empty, label("empty", "(empty)")),
if(description, description.first_line(), description_placeholder),
) "\n",
),
)
'''
op_log = '''
label(if(current_operation, "current_operation"),
separate(" ",
id.short(),
user,
format_time_range(time),
)
"\n"
description.first_line() "\n"
tags
concat(
separate(" ",
id.short(),
user,
format_time_range(time),
) "\n",
description.first_line() "\n",
tags,
),
)
'''
@ -61,11 +62,13 @@ show = 'show'
# TODO: Add branches, tags, etc
# TODO: Indent the description like Git does
'show' = '''
"Commit ID: " commit_id "\n"
"Change ID: " change_id "\n"
"Author: " author " (" format_timestamp(author.timestamp()) ")\n"
"Committer: " committer " (" format_timestamp(committer.timestamp()) ")\n"
"\n"
if(description, description, description_placeholder "\n")
"\n"
concat(
"Commit ID: " commit_id "\n",
"Change ID: " change_id "\n",
"Author: " author " (" format_timestamp(author.timestamp()) ")\n",
"Committer: " committer " (" format_timestamp(committer.timestamp()) ")\n",
"\n",
if(description, description, description_placeholder "\n"),
"\n",
)
'''

View file

@ -1068,6 +1068,15 @@ fn build_global_function<'a, L: TemplateLanguage<'a>>(
));
Expression::Template(template)
}
"concat" => {
let contents = function
.args
.iter()
.map(|node| build_expression(language, node).map(|x| x.into_template()))
.try_collect()?;
let template = Box::new(ListTemplate(contents));
Expression::Template(template)
}
"separate" => {
let ([separator_node], content_nodes) = expect_some_arguments(function)?;
let separator = build_expression(language, separator_node)?.into_template();

View file

@ -335,6 +335,20 @@ fn test_templater_label_function() {
render(r#"label(if(empty, "error", "warning"), "text")"#), @"text");
}
#[test]
fn test_templater_concat_function() {
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#"concat()"#), @"");
insta::assert_snapshot!(render(r#"concat(author, empty)"#), @" <>true");
insta::assert_snapshot!(
render(r#"concat(label("error", ""), label("warning", "a"), "b")"#),
@"ab");
}
#[test]
fn test_templater_separate_function() {
let test_env = TestEnvironment::default();