Skip to content

Commit

Permalink
templater: inline commit/operation_templater::parse(), add command he…
Browse files Browse the repository at this point in the history
…lper fns

I'm going to add generic templating support for basic value types, and
"jj config list -T" will use CommandHelper::parse_template().
CommandHelper::load_template_aliases() is made private instead.
  • Loading branch information
yuja committed Mar 5, 2024
1 parent e825278 commit 749ef45
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 50 deletions.
55 changes: 40 additions & 15 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ use crate::command_error::{
handle_command_result, internal_error, internal_error_with_message, user_error,
user_error_with_hint, user_error_with_message, CommandError,
};
use crate::commit_templater::CommitTemplateLanguageExtension;
use crate::commit_templater::{CommitTemplateLanguage, CommitTemplateLanguageExtension};
use crate::config::{
new_config_path, AnnotatedValue, CommandNameAndArgs, ConfigSource, LayeredConfigs,
};
Expand All @@ -84,10 +84,11 @@ use crate::git_util::{
is_colocated_git_workspace, print_failed_git_export, print_git_import_stats,
};
use crate::merge_tools::{DiffEditor, MergeEditor, MergeToolConfigError};
use crate::template_builder::TemplateLanguage;
use crate::template_parser::TemplateAliasesMap;
use crate::templater::Template;
use crate::ui::{ColorChoice, Ui};
use crate::{commit_templater, text_util};
use crate::{template_builder, text_util};

#[derive(Clone)]
struct ChromeTracingFlushGuard {
Expand Down Expand Up @@ -230,10 +231,25 @@ impl CommandHelper {
///
/// For most commands that depend on a loaded repo, you should use
/// `WorkspaceCommandHelper::template_aliases_map()` instead.
pub fn load_template_aliases(&self, ui: &Ui) -> Result<TemplateAliasesMap, CommandError> {
fn load_template_aliases(&self, ui: &Ui) -> Result<TemplateAliasesMap, CommandError> {
load_template_aliases(ui, &self.layered_configs)
}

/// Parses template of the given language into evaluation tree.
///
/// This function also loads template aliases from the settings. Use
/// `WorkspaceCommandHelper::parse_template()` if you've already
/// instantiated the workspace helper.
pub fn parse_template<'a, L: TemplateLanguage<'a> + ?Sized>(
&self,
ui: &Ui,
language: &L,
template_text: &str,
) -> Result<Box<dyn Template<L::Context> + 'a>, CommandError> {
let aliases = self.load_template_aliases(ui)?;
Ok(template_builder::parse(language, template_text, &aliases)?)
}

pub fn workspace_loader(&self) -> Result<&WorkspaceLoader, CommandError> {
self.maybe_workspace_loader.as_ref().map_err(Clone::clone)
}
Expand Down Expand Up @@ -866,20 +882,28 @@ Set which revision the branch points to with `jj branch set {branch_name} -r <RE
&self.template_aliases_map
}

/// Parses template of the given language into evaluation tree.
pub fn parse_template<'a, L: TemplateLanguage<'a> + ?Sized>(
&self,
language: &L,
template_text: &str,
) -> Result<Box<dyn Template<L::Context> + 'a>, CommandError> {
let aliases = &self.template_aliases_map;
Ok(template_builder::parse(language, template_text, aliases)?)
}

/// Parses commit template into evaluation tree.
pub fn parse_commit_template(
&self,
template_text: &str,
) -> Result<Box<dyn Template<Commit> + '_>, CommandError> {
let id_prefix_context = self.id_prefix_context()?;
let template = commit_templater::parse(
let language = CommitTemplateLanguage::new(
self.repo().as_ref(),
self.workspace_id(),
id_prefix_context,
self.id_prefix_context()?,
self.commit_template_extension.as_deref(),
template_text,
&self.template_aliases_map,
)?;
Ok(template)
);
self.parse_template(&language, template_text)
}

fn commit_summary_template(&self) -> Box<dyn Template<Commit> + '_> {
Expand Down Expand Up @@ -1350,15 +1374,16 @@ impl WorkspaceCommandTransaction<'_> {
) -> std::io::Result<()> {
// TODO: Use the disambiguation revset
let id_prefix_context = IdPrefixContext::default();
let template = commit_templater::parse(
let language = CommitTemplateLanguage::new(
self.tx.repo(),
self.helper.workspace_id(),
&id_prefix_context,
self.helper.commit_template_extension.as_deref(),
&self.helper.commit_summary_template_text,
&self.helper.template_aliases_map,
)
.expect("parse error should be confined by WorkspaceCommandHelper::new()");
);
let template = self
.helper
.parse_template(&language, &self.helper.commit_summary_template_text)
.expect("parse error should be confined by WorkspaceCommandHelper::new()");
template.format(commit, formatter)
}

Expand Down
22 changes: 11 additions & 11 deletions cli/src/commands/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use jj_lib::repo::Repo;
use crate::cli_util::{short_operation_hash, CommandHelper, LogContentFormat};
use crate::command_error::{user_error, user_error_with_hint, CommandError};
use crate::graphlog::{get_graphlog, Edge};
use crate::operation_templater;
use crate::operation_templater::OperationTemplateLanguage;
use crate::templater::Template as _;
use crate::ui::Ui;

Expand Down Expand Up @@ -158,17 +158,17 @@ fn cmd_op_log(
_ => None,
};

let template_string = match &args.template {
Some(value) => value.to_owned(),
None => command.settings().config().get_string("templates.op_log")?,
let template = {
let language = OperationTemplateLanguage::new(
repo_loader.op_store().root_operation_id(),
current_op_id,
);
let text = match &args.template {
Some(value) => value.to_owned(),
None => command.settings().config().get_string("templates.op_log")?,
};
command.parse_template(ui, &language, &text)?
};
let template_aliases = command.load_template_aliases(ui)?;
let template = operation_templater::parse(
repo_loader.op_store().root_operation_id(),
current_op_id,
&template_string,
&template_aliases,
)?;
let with_content_format = LogContentFormat::new(ui, command.settings())?;

ui.request_pager();
Expand Down
14 changes: 1 addition & 13 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::template_builder::{
self, merge_fn_map, BuildContext, CoreTemplateBuildFnTable, CoreTemplatePropertyKind,
IntoTemplateProperty, TemplateBuildMethodFnMap, TemplateLanguage,
};
use crate::template_parser::{self, FunctionCallNode, TemplateAliasesMap, TemplateParseResult};
use crate::template_parser::{self, FunctionCallNode, TemplateParseResult};
use crate::templater::{
self, IntoTemplate, PlainTextFormattedProperty, Template, TemplateFunction, TemplateProperty,
TemplatePropertyFn,
Expand Down Expand Up @@ -861,15 +861,3 @@ fn builtin_shortest_id_prefix_methods<'repo>(
});
map
}

pub fn parse<'repo>(
repo: &'repo dyn Repo,
workspace_id: &WorkspaceId,
id_prefix_context: &'repo IdPrefixContext,
extension: Option<&dyn CommitTemplateLanguageExtension>,
template_text: &str,
aliases_map: &TemplateAliasesMap,
) -> TemplateParseResult<Box<dyn Template<Commit> + 'repo>> {
let language = CommitTemplateLanguage::new(repo, workspace_id, id_prefix_context, extension);
template_builder::parse(&language, template_text, aliases_map)
}
12 changes: 1 addition & 11 deletions cli/src/operation_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::template_builder::{
self, BuildContext, CoreTemplateBuildFnTable, CoreTemplatePropertyKind, IntoTemplateProperty,
TemplateBuildMethodFnMap, TemplateLanguage,
};
use crate::template_parser::{self, FunctionCallNode, TemplateAliasesMap, TemplateParseResult};
use crate::template_parser::{self, FunctionCallNode, TemplateParseResult};
use crate::templater::{
IntoTemplate, PlainTextFormattedProperty, Template, TemplateFunction, TemplateProperty,
TemplatePropertyFn, TimestampRange,
Expand Down Expand Up @@ -261,13 +261,3 @@ fn builtin_operation_id_methods() -> OperationTemplateBuildMethodFnMap<Operation
});
map
}

pub fn parse(
root_op_id: &OperationId,
current_op_id: Option<&OperationId>,
template_text: &str,
aliases_map: &TemplateAliasesMap,
) -> TemplateParseResult<Box<dyn Template<Operation>>> {
let language = OperationTemplateLanguage::new(root_op_id, current_op_id);
template_builder::parse(&language, template_text, aliases_map)
}

0 comments on commit 749ef45

Please sign in to comment.