templater: add shortest().prefix()/rest() accessors

This ensures that the "brackets" style can be emulated by a template
expression.
This commit is contained in:
Yuya Nishihara 2023-02-14 22:36:48 +09:00
parent 29792dab9b
commit ed4a696dea
3 changed files with 44 additions and 2 deletions

View file

@ -896,6 +896,20 @@ fn build_shortest_id_prefix_method<'a, I: 'a>(
_build_keyword: &impl Fn(&str, pest::Span) -> TemplateParseResult<PropertyAndLabels<'a, I>>,
) -> TemplateParseResult<Property<'a, I>> {
let property = match function.name {
"prefix" => {
expect_no_arguments(function)?;
Property::String(chain_properties(
self_property,
TemplatePropertyFn(|id: &ShortestIdPrefix| id.prefix.clone()),
))
}
"rest" => {
expect_no_arguments(function)?;
Property::String(chain_properties(
self_property,
TemplatePropertyFn(|id: &ShortestIdPrefix| id.rest.clone()),
))
}
"with_brackets" => {
// TODO: If we had a map function, this could be expressed as a template
// like 'id.shortest() % (.prefix() if(.rest(), "[" .rest() "]"))'

View file

@ -609,8 +609,8 @@ impl Template<()> for CommitOrChangeId<'_> {
}
pub struct ShortestIdPrefix {
prefix: String,
rest: String,
pub prefix: String,
pub rest: String,
}
impl ShortestIdPrefix {

View file

@ -288,6 +288,18 @@ fn test_log_prefix_highlight_brackets() {
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 = |rev, template| {
test_env.jj_cmd_success(
&repo_path,
&["log", "--no-graph", "-r", rev, "-T", template],
)
};
test_env.add_config(
r###"
[template-aliases]
'format_id(id)' = 'id.shortest(12).prefix() "[" id.shortest(12).rest() "]"'
"###,
);
fn prefix_format(len: Option<usize>) -> String {
format!(
@ -377,6 +389,22 @@ fn test_log_prefix_highlight_brackets() {
o Change zzz 00
"###
);
insta::assert_snapshot!(
render(":@", r#"format_id(change_id) " " format_id(commit_id) "\n""#),
@r###"
wq[nwkozpkust] 03[f51310b83e]
km[kuslswpqwq] f7[7fb1909080]
kp[qxywonksrl] e7[15ad5db646]
zn[kkpsqqskkl] 38[622e54e2e5]
yo[stqsxwqrlt] 0cf[42f60199c]
vr[uxwmqvtpmx] 9e[6015e4e622]
yq[osqzytrlsw] 06f[34d9b1475]
ro[yxmykxtrkr] 1f[99a5e19891]
mz[vwutvlkqwt] 7b[1f7dee65b4]
qpv[untsmwlqt] ba1[a30916d29]
zzz[zzzzzzzzz] 00[0000000000]
"###);
}
#[test]