mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-16 09:11:55 +00:00
Templater: Create shortest_styled_prefix
function for ids
This commit is contained in:
parent
bd37989b63
commit
26cc290157
2 changed files with 44 additions and 3 deletions
|
@ -27,9 +27,9 @@ use crate::templater::{
|
||||||
CommitOrChangeIdShort, CommitOrChangeIdShortestPrefixAndBrackets, CommitterProperty,
|
CommitOrChangeIdShort, CommitOrChangeIdShortestPrefixAndBrackets, CommitterProperty,
|
||||||
ConditionalTemplate, ConflictProperty, DescriptionProperty, DivergentProperty,
|
ConditionalTemplate, ConflictProperty, DescriptionProperty, DivergentProperty,
|
||||||
DynamicLabelTemplate, EmptyProperty, FormattablePropertyTemplate, GitHeadProperty,
|
DynamicLabelTemplate, EmptyProperty, FormattablePropertyTemplate, GitHeadProperty,
|
||||||
GitRefsProperty, IsWorkingCopyProperty, LabelTemplate, ListTemplate, Literal,
|
GitRefsProperty, HighlightPrefix, IdWithHighlightedPrefix, IsWorkingCopyProperty,
|
||||||
SignatureTimestamp, TagProperty, Template, TemplateFunction, TemplateProperty,
|
LabelTemplate, ListTemplate, Literal, SignatureTimestamp, TagProperty, Template,
|
||||||
WorkingCopiesProperty,
|
TemplateFunction, TemplateProperty, WorkingCopiesProperty,
|
||||||
};
|
};
|
||||||
use crate::time_util;
|
use crate::time_util;
|
||||||
|
|
||||||
|
@ -101,6 +101,7 @@ enum Property<'a, I> {
|
||||||
String(Box<dyn TemplateProperty<I, Output = String> + 'a>),
|
String(Box<dyn TemplateProperty<I, Output = String> + 'a>),
|
||||||
Boolean(Box<dyn TemplateProperty<I, Output = bool> + 'a>),
|
Boolean(Box<dyn TemplateProperty<I, Output = bool> + 'a>),
|
||||||
CommitOrChangeId(Box<dyn TemplateProperty<I, Output = CommitOrChangeId<'a>> + 'a>),
|
CommitOrChangeId(Box<dyn TemplateProperty<I, Output = CommitOrChangeId<'a>> + 'a>),
|
||||||
|
IdWithHighlightedPrefix(Box<dyn TemplateProperty<I, Output = IdWithHighlightedPrefix> + 'a>),
|
||||||
Signature(Box<dyn TemplateProperty<I, Output = Signature> + 'a>),
|
Signature(Box<dyn TemplateProperty<I, Output = Signature> + 'a>),
|
||||||
Timestamp(Box<dyn TemplateProperty<I, Output = Timestamp> + 'a>),
|
Timestamp(Box<dyn TemplateProperty<I, Output = Timestamp> + 'a>),
|
||||||
}
|
}
|
||||||
|
@ -121,6 +122,9 @@ impl<'a, I: 'a> Property<'a, I> {
|
||||||
Property::CommitOrChangeId(property) => {
|
Property::CommitOrChangeId(property) => {
|
||||||
Property::CommitOrChangeId(chain(first, property))
|
Property::CommitOrChangeId(chain(first, property))
|
||||||
}
|
}
|
||||||
|
Property::IdWithHighlightedPrefix(property) => {
|
||||||
|
Property::IdWithHighlightedPrefix(chain(first, property))
|
||||||
|
}
|
||||||
Property::Signature(property) => Property::Signature(chain(first, property)),
|
Property::Signature(property) => Property::Signature(chain(first, property)),
|
||||||
Property::Timestamp(property) => Property::Timestamp(chain(first, property)),
|
Property::Timestamp(property) => Property::Timestamp(chain(first, property)),
|
||||||
}
|
}
|
||||||
|
@ -136,6 +140,7 @@ impl<'a, I: 'a> Property<'a, I> {
|
||||||
Property::String(property) => wrap(property),
|
Property::String(property) => wrap(property),
|
||||||
Property::Boolean(property) => wrap(property),
|
Property::Boolean(property) => wrap(property),
|
||||||
Property::CommitOrChangeId(property) => wrap(property),
|
Property::CommitOrChangeId(property) => wrap(property),
|
||||||
|
Property::IdWithHighlightedPrefix(property) => wrap(property),
|
||||||
Property::Signature(property) => wrap(property),
|
Property::Signature(property) => wrap(property),
|
||||||
Property::Timestamp(property) => wrap(property),
|
Property::Timestamp(property) => wrap(property),
|
||||||
}
|
}
|
||||||
|
@ -165,6 +170,9 @@ fn parse_method_chain<'a, I: 'a>(
|
||||||
Property::CommitOrChangeId(property) => {
|
Property::CommitOrChangeId(property) => {
|
||||||
parse_commit_or_change_id_method(name, args).after(property)
|
parse_commit_or_change_id_method(name, args).after(property)
|
||||||
}
|
}
|
||||||
|
Property::IdWithHighlightedPrefix(_property) => {
|
||||||
|
panic!("Commit or change ids with styled prefix don't have any methods")
|
||||||
|
}
|
||||||
Property::Signature(property) => parse_signature_method(name, args).after(property),
|
Property::Signature(property) => parse_signature_method(name, args).after(property),
|
||||||
Property::Timestamp(property) => parse_timestamp_method(name, args).after(property),
|
Property::Timestamp(property) => parse_timestamp_method(name, args).after(property),
|
||||||
};
|
};
|
||||||
|
@ -195,6 +203,7 @@ fn parse_commit_or_change_id_method<'a>(
|
||||||
"shortest_prefix_and_brackets" => {
|
"shortest_prefix_and_brackets" => {
|
||||||
Property::String(Box::new(CommitOrChangeIdShortestPrefixAndBrackets))
|
Property::String(Box::new(CommitOrChangeIdShortestPrefixAndBrackets))
|
||||||
}
|
}
|
||||||
|
"shortest_styled_prefix" => Property::IdWithHighlightedPrefix(Box::new(HighlightPrefix)),
|
||||||
name => panic!("no such commit ID method: {name}"),
|
name => panic!("no such commit ID method: {name}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -561,6 +561,38 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct IdWithHighlightedPrefix {
|
||||||
|
prefix: String,
|
||||||
|
rest: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Template<()> for IdWithHighlightedPrefix {
|
||||||
|
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||||
|
formatter.with_label("prefix", |fmt| fmt.write_str(&self.prefix))?;
|
||||||
|
formatter.with_label("rest", |fmt| fmt.write_str(&self.rest))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct HighlightPrefix;
|
||||||
|
impl TemplateProperty<CommitOrChangeId<'_>> for HighlightPrefix {
|
||||||
|
type Output = IdWithHighlightedPrefix;
|
||||||
|
|
||||||
|
fn extract(&self, context: &CommitOrChangeId) -> Self::Output {
|
||||||
|
let hex = context.hex();
|
||||||
|
let (prefix, rest) = extract_entire_prefix_and_trimmed_tail(
|
||||||
|
&hex,
|
||||||
|
context
|
||||||
|
.repo
|
||||||
|
.shortest_unique_id_prefix_len(context.as_bytes()),
|
||||||
|
12,
|
||||||
|
);
|
||||||
|
IdWithHighlightedPrefix {
|
||||||
|
prefix: prefix.to_string(),
|
||||||
|
rest: rest.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct CommitOrChangeIdShort;
|
pub struct CommitOrChangeIdShort;
|
||||||
|
|
||||||
impl TemplateProperty<CommitOrChangeId<'_>> for CommitOrChangeIdShort {
|
impl TemplateProperty<CommitOrChangeId<'_>> for CommitOrChangeIdShort {
|
||||||
|
|
Loading…
Reference in a new issue