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

templater: migrate shortest_prefix_and_brackets() over shortest()

This commit is contained in:
Yuya Nishihara 2023-02-06 15:43:24 +09:00
parent 3ccac9cda5
commit ebf9887d65
4 changed files with 43 additions and 35 deletions

View file

@ -1424,7 +1424,7 @@ fn log_template(settings: &UserSettings) -> String {
// TODO: If/when this logic is relevant in the `lib` crate, make this into
// and enum similar to `ColorChoice`.
let prefix_format = match settings.unique_prefixes().as_str() {
"brackets" => format!("shortest_prefix_and_brackets({desired_id_len})"),
"brackets" => format!("shortest({desired_id_len}).with_brackets()"),
"styled" => format!("shortest({desired_id_len})"),
_ => format!("short({desired_id_len})"),
};

View file

@ -390,11 +390,8 @@ fn parse_method_chain<'a, I: 'a>(
Property::CommitOrChangeId(property) => {
parse_commit_or_change_id_method(property, name, args_pair, parse_keyword)?
}
Property::ShortestIdPrefix(_property) => {
return Err(TemplateParseError::no_such_method(
"ShortestIdPrefix",
&name,
));
Property::ShortestIdPrefix(property) => {
parse_shortest_id_prefix_method(property, name, args_pair, parse_keyword)?
}
Property::Signature(property) => {
parse_signature_method(property, name, args_pair, parse_keyword)?
@ -493,15 +490,6 @@ fn parse_commit_or_change_id_method<'a, I: 'a>(
}),
))
}
"shortest_prefix_and_brackets" => {
let len_property = parse_optional_integer(args_pair)?;
Property::String(chain_properties(
(self_property, len_property),
TemplatePropertyFn(|(id, len): &(CommitOrChangeId, Option<i64>)| {
id.shortest_prefix_and_brackets(len.unwrap_or(0))
}),
))
}
"shortest" => {
let len_property = parse_optional_integer(args_pair)?;
Property::ShortestIdPrefix(chain_properties(
@ -521,6 +509,32 @@ fn parse_commit_or_change_id_method<'a, I: 'a>(
Ok(property)
}
fn parse_shortest_id_prefix_method<'a, I: 'a>(
self_property: impl TemplateProperty<I, Output = ShortestIdPrefix> + 'a,
name: Pair<Rule>,
args_pair: Pair<Rule>,
_parse_keyword: &impl Fn(Pair<Rule>) -> TemplateParseResult<PropertyAndLabels<'a, I>>,
) -> TemplateParseResult<Property<'a, I>> {
let property = match name.as_str() {
"with_brackets" => {
// TODO: If we had a map function, this could be expressed as a template
// like 'id.shortest() % (.prefix() if(.rest(), "[" .rest() "]"))'
expect_no_arguments(args_pair)?;
Property::String(chain_properties(
self_property,
TemplatePropertyFn(|id: &ShortestIdPrefix| id.with_brackets()),
))
}
_ => {
return Err(TemplateParseError::no_such_method(
"ShortestIdPrefix",
&name,
));
}
};
Ok(property)
}
fn parse_signature_method<'a, I: 'a>(
self_property: impl TemplateProperty<I, Output = Signature> + 'a,
name: Pair<Rule>,

View file

@ -567,22 +567,6 @@ impl<'a> CommitOrChangeId<'a> {
hex
}
/// The length of the id printed (not counting the brackets) will be the
/// maximum of `total_len` and the length of the shortest unique prefix
pub fn shortest_prefix_and_brackets(&self, total_len: i64) -> String {
let hex = self.hex();
let (prefix, rest) = extract_entire_prefix_and_trimmed_tail(
&hex,
self.repo.shortest_unique_id_prefix_len(self.as_bytes()),
max(total_len, 0) as usize,
);
if rest.is_empty() {
prefix.to_string()
} else {
format!("{prefix}[{rest}]")
}
}
/// The length of the id printed will be the maximum of `total_len` and the
/// length of the shortest unique prefix
pub fn shortest(&self, total_len: i64) -> ShortestIdPrefix {
@ -660,6 +644,16 @@ pub struct ShortestIdPrefix {
rest: String,
}
impl ShortestIdPrefix {
pub fn with_brackets(&self) -> String {
if self.rest.is_empty() {
self.prefix.clone()
} else {
format!("{}[{}]", self.prefix, self.rest)
}
}
}
impl Template<()> for ShortestIdPrefix {
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
formatter.with_label("prefix", |fmt| fmt.write_str(&self.prefix))?;

View file

@ -292,8 +292,8 @@ fn test_log_prefix_highlight_brackets() {
fn prefix_format(len: Option<usize>) -> String {
format!(
r#"
"Change " change_id.shortest_prefix_and_brackets({0}) " " description.first_line()
" " commit_id.shortest_prefix_and_brackets({0}) " " branches
"Change " change_id.shortest({0}).with_brackets() " " description.first_line()
" " commit_id.shortest({0}).with_brackets() " " branches
"#,
len.map(|l| l.to_string()).unwrap_or(String::default())
)
@ -508,8 +508,8 @@ fn test_log_prefix_highlight_counts_hidden_commits() {
let repo_path = test_env.env_root().join("repo");
let prefix_format = r#"
"Change " change_id.shortest_prefix_and_brackets(12) " " description.first_line()
" " commit_id.shortest_prefix_and_brackets(12) " " branches
"Change " change_id.shortest(12).with_brackets() " " description.first_line()
" " commit_id.shortest(12).with_brackets() " " branches
"#;
std::fs::write(repo_path.join("file"), "original file\n").unwrap();