templater: inline wrap_fn() helpers

If I remember correctly, wrap_fn() was added to help type inference. It no
longer makes sense because the type is coerced by TemplateFunction::new()
and language.wrap_*().
This commit is contained in:
Yuya Nishihara 2024-02-23 12:39:14 +09:00
parent f0ce448609
commit bbf88643cc
2 changed files with 46 additions and 59 deletions

View file

@ -243,68 +243,62 @@ fn build_commit_method<'repo>(
self_property: impl TemplateProperty<Commit, Output = Commit> + 'repo, self_property: impl TemplateProperty<Commit, Output = Commit> + 'repo,
function: &FunctionCallNode, function: &FunctionCallNode,
) -> TemplateParseResult<CommitTemplatePropertyKind<'repo>> { ) -> TemplateParseResult<CommitTemplatePropertyKind<'repo>> {
fn wrap_fn<'repo, O>(
property: impl TemplateProperty<Commit, Output = Commit> + 'repo,
f: impl Fn(&Commit) -> O + 'repo,
) -> impl TemplateProperty<Commit, Output = O> + 'repo {
TemplateFunction::new(property, move |commit| f(&commit))
}
fn wrap_repo_fn<'repo, O>(
repo: &'repo dyn Repo,
property: impl TemplateProperty<Commit, Output = Commit> + 'repo,
f: impl Fn(&dyn Repo, &Commit) -> O + 'repo,
) -> impl TemplateProperty<Commit, Output = O> + 'repo {
TemplateFunction::new(property, move |commit| f(repo, &commit))
}
let repo = language.repo; let repo = language.repo;
let cache = &language.keyword_cache; let cache = &language.keyword_cache;
let property = match function.name { let property = match function.name {
"description" => { "description" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_string(wrap_fn(self_property, |commit| { language.wrap_string(TemplateFunction::new(self_property, |commit| {
text_util::complete_newline(commit.description()) text_util::complete_newline(commit.description())
})) }))
} }
"change_id" => { "change_id" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_commit_or_change_id(wrap_fn(self_property, |commit| { language.wrap_commit_or_change_id(TemplateFunction::new(self_property, |commit| {
CommitOrChangeId::Change(commit.change_id().to_owned()) CommitOrChangeId::Change(commit.change_id().to_owned())
})) }))
} }
"commit_id" => { "commit_id" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_commit_or_change_id(wrap_fn(self_property, |commit| { language.wrap_commit_or_change_id(TemplateFunction::new(self_property, |commit| {
CommitOrChangeId::Commit(commit.id().to_owned()) CommitOrChangeId::Commit(commit.id().to_owned())
})) }))
} }
"parents" => { "parents" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_commit_list(wrap_fn(self_property, |commit| commit.parents())) language.wrap_commit_list(TemplateFunction::new(self_property, |commit| {
commit.parents()
}))
} }
"author" => { "author" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_signature(wrap_fn(self_property, |commit| commit.author().clone())) language.wrap_signature(TemplateFunction::new(self_property, |commit| {
commit.author().clone()
}))
} }
"committer" => { "committer" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_signature(wrap_fn(self_property, |commit| commit.committer().clone())) language.wrap_signature(TemplateFunction::new(self_property, |commit| {
commit.committer().clone()
}))
} }
"working_copies" => { "working_copies" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_string(wrap_repo_fn(repo, self_property, extract_working_copies)) language.wrap_string(TemplateFunction::new(self_property, |commit| {
extract_working_copies(repo, &commit)
}))
} }
"current_working_copy" => { "current_working_copy" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
let workspace_id = language.workspace_id.clone(); let workspace_id = language.workspace_id.clone();
language.wrap_boolean(wrap_fn(self_property, move |commit| { language.wrap_boolean(TemplateFunction::new(self_property, move |commit| {
Some(commit.id()) == repo.view().get_wc_commit_id(&workspace_id) Some(commit.id()) == repo.view().get_wc_commit_id(&workspace_id)
})) }))
} }
"branches" => { "branches" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
let index = cache.branches_index(repo).clone(); let index = cache.branches_index(repo).clone();
language.wrap_ref_name_list(wrap_fn(self_property, move |commit| { language.wrap_ref_name_list(TemplateFunction::new(self_property, move |commit| {
index index
.get(commit.id()) .get(commit.id())
.iter() .iter()
@ -316,7 +310,7 @@ fn build_commit_method<'repo>(
"local_branches" => { "local_branches" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
let index = cache.branches_index(repo).clone(); let index = cache.branches_index(repo).clone();
language.wrap_ref_name_list(wrap_fn(self_property, move |commit| { language.wrap_ref_name_list(TemplateFunction::new(self_property, move |commit| {
index index
.get(commit.id()) .get(commit.id())
.iter() .iter()
@ -328,7 +322,7 @@ fn build_commit_method<'repo>(
"remote_branches" => { "remote_branches" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
let index = cache.branches_index(repo).clone(); let index = cache.branches_index(repo).clone();
language.wrap_ref_name_list(wrap_fn(self_property, move |commit| { language.wrap_ref_name_list(TemplateFunction::new(self_property, move |commit| {
index index
.get(commit.id()) .get(commit.id())
.iter() .iter()
@ -340,24 +334,26 @@ fn build_commit_method<'repo>(
"tags" => { "tags" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
let index = cache.tags_index(repo).clone(); let index = cache.tags_index(repo).clone();
language.wrap_ref_name_list(wrap_fn(self_property, move |commit| { language.wrap_ref_name_list(TemplateFunction::new(self_property, move |commit| {
index.get(commit.id()).to_vec() index.get(commit.id()).to_vec()
})) }))
} }
"git_refs" => { "git_refs" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
let index = cache.git_refs_index(repo).clone(); let index = cache.git_refs_index(repo).clone();
language.wrap_ref_name_list(wrap_fn(self_property, move |commit| { language.wrap_ref_name_list(TemplateFunction::new(self_property, move |commit| {
index.get(commit.id()).to_vec() index.get(commit.id()).to_vec()
})) }))
} }
"git_head" => { "git_head" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_ref_name_list(wrap_repo_fn(repo, self_property, extract_git_head)) language.wrap_ref_name_list(TemplateFunction::new(self_property, |commit| {
extract_git_head(repo, &commit)
}))
} }
"divergent" => { "divergent" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_boolean(wrap_fn(self_property, |commit| { language.wrap_boolean(TemplateFunction::new(self_property, |commit| {
// The given commit could be hidden in e.g. obslog. // The given commit could be hidden in e.g. obslog.
let maybe_entries = repo.resolve_change_id(commit.change_id()); let maybe_entries = repo.resolve_change_id(commit.change_id());
maybe_entries.map_or(0, |entries| entries.len()) > 1 maybe_entries.map_or(0, |entries| entries.len()) > 1
@ -365,18 +361,20 @@ fn build_commit_method<'repo>(
} }
"hidden" => { "hidden" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_boolean(wrap_fn(self_property, |commit| { language.wrap_boolean(TemplateFunction::new(self_property, |commit| {
let maybe_entries = repo.resolve_change_id(commit.change_id()); let maybe_entries = repo.resolve_change_id(commit.change_id());
maybe_entries.map_or(true, |entries| !entries.contains(commit.id())) maybe_entries.map_or(true, |entries| !entries.contains(commit.id()))
})) }))
} }
"conflict" => { "conflict" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_boolean(wrap_fn(self_property, |commit| commit.has_conflict().unwrap())) language.wrap_boolean(TemplateFunction::new(self_property, |commit| {
commit.has_conflict().unwrap()
}))
} }
"empty" => { "empty" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_boolean(wrap_fn(self_property, |commit| { language.wrap_boolean(TemplateFunction::new(self_property, |commit| {
if let [parent] = &commit.parents()[..] { if let [parent] = &commit.parents()[..] {
return parent.tree_id() == commit.tree_id(); return parent.tree_id() == commit.tree_id();
} }
@ -386,7 +384,7 @@ fn build_commit_method<'repo>(
} }
"root" => { "root" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_boolean(wrap_fn(self_property, move |commit| { language.wrap_boolean(TemplateFunction::new(self_property, |commit| {
commit.id() == repo.store().root_commit_id() commit.id() == repo.store().root_commit_id()
})) }))
} }

View file

@ -16,7 +16,7 @@ use std::io;
use itertools::Itertools as _; use itertools::Itertools as _;
use jj_lib::object_id::ObjectId; use jj_lib::object_id::ObjectId;
use jj_lib::op_store::{OperationId, OperationMetadata}; use jj_lib::op_store::OperationId;
use jj_lib::operation::Operation; use jj_lib::operation::Operation;
use crate::formatter::Formatter; use crate::formatter::Formatter;
@ -130,42 +130,29 @@ fn build_operation_method(
self_property: impl TemplateProperty<Operation, Output = Operation> + 'static, self_property: impl TemplateProperty<Operation, Output = Operation> + 'static,
function: &FunctionCallNode, function: &FunctionCallNode,
) -> TemplateParseResult<OperationTemplatePropertyKind> { ) -> TemplateParseResult<OperationTemplatePropertyKind> {
fn wrap_fn<O>(
property: impl TemplateProperty<Operation, Output = Operation>,
f: impl Fn(&Operation) -> O,
) -> impl TemplateProperty<Operation, Output = O> {
TemplateFunction::new(property, move |op| f(&op))
}
fn wrap_metadata_fn<O>(
property: impl TemplateProperty<Operation, Output = Operation>,
f: impl Fn(&OperationMetadata) -> O,
) -> impl TemplateProperty<Operation, Output = O> {
TemplateFunction::new(property, move |op| f(op.metadata()))
}
let property = match function.name { let property = match function.name {
"current_operation" => { "current_operation" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
let current_op_id = language.current_op_id.cloned(); let current_op_id = language.current_op_id.cloned();
language.wrap_boolean(wrap_fn(self_property, move |op| { language.wrap_boolean(TemplateFunction::new(self_property, move |op| {
Some(op.id()) == current_op_id.as_ref() Some(op.id()) == current_op_id.as_ref()
})) }))
} }
"description" => { "description" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_string(wrap_metadata_fn(self_property, |metadata| { language.wrap_string(TemplateFunction::new(self_property, |op| {
metadata.description.clone() op.metadata().description.clone()
})) }))
} }
"id" => { "id" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_operation_id(wrap_fn(self_property, |op| op.id().clone())) language.wrap_operation_id(TemplateFunction::new(self_property, |op| op.id().clone()))
} }
"tags" => { "tags" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_string(wrap_metadata_fn(self_property, |metadata| { language.wrap_string(TemplateFunction::new(self_property, |op| {
// TODO: introduce map type // TODO: introduce map type
metadata op.metadata()
.tags .tags
.iter() .iter()
.map(|(key, value)| format!("{key}: {value}")) .map(|(key, value)| format!("{key}: {value}"))
@ -174,24 +161,26 @@ fn build_operation_method(
} }
"time" => { "time" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_timestamp_range(wrap_metadata_fn(self_property, |metadata| { language.wrap_timestamp_range(TemplateFunction::new(self_property, |op| {
TimestampRange { TimestampRange {
start: metadata.start_time.clone(), start: op.metadata().start_time.clone(),
end: metadata.end_time.clone(), end: op.metadata().end_time.clone(),
} }
})) }))
} }
"user" => { "user" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_string(wrap_metadata_fn(self_property, |metadata| { language.wrap_string(TemplateFunction::new(self_property, |op| {
// TODO: introduce dedicated type and provide accessors? // TODO: introduce dedicated type and provide accessors?
format!("{}@{}", metadata.username, metadata.hostname) format!("{}@{}", op.metadata().username, op.metadata().hostname)
})) }))
} }
"root" => { "root" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
let root_op_id = language.root_op_id.clone(); let root_op_id = language.root_op_id.clone();
language.wrap_boolean(wrap_fn(self_property, move |op| op.id() == &root_op_id)) language.wrap_boolean(TemplateFunction::new(self_property, move |op| {
op.id() == &root_op_id
}))
} }
_ => return Err(TemplateParseError::no_such_method("Operation", function)), _ => return Err(TemplateParseError::no_such_method("Operation", function)),
}; };