2023-02-19 08:05:50 +00:00
|
|
|
// Copyright 2023 The Jujutsu Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2024-03-15 20:48:13 +00:00
|
|
|
use std::any::Any;
|
|
|
|
use std::collections::HashMap;
|
2023-02-19 08:05:50 +00:00
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use itertools::Itertools as _;
|
2024-03-15 20:48:13 +00:00
|
|
|
use jj_lib::extensions_map::ExtensionsMap;
|
2024-01-04 07:18:04 +00:00
|
|
|
use jj_lib::object_id::ObjectId;
|
2024-02-23 03:39:14 +00:00
|
|
|
use jj_lib::op_store::OperationId;
|
2023-06-28 14:12:40 +00:00
|
|
|
use jj_lib::operation::Operation;
|
2023-02-19 08:05:50 +00:00
|
|
|
|
|
|
|
use crate::formatter::Formatter;
|
2023-03-12 11:10:19 +00:00
|
|
|
use crate::template_builder::{
|
2024-03-15 20:48:13 +00:00
|
|
|
self, merge_fn_map, BuildContext, CoreTemplateBuildFnTable, CoreTemplatePropertyKind,
|
|
|
|
IntoTemplateProperty, TemplateBuildMethodFnMap, TemplateLanguage,
|
2023-02-19 08:05:50 +00:00
|
|
|
};
|
2024-03-03 14:25:02 +00:00
|
|
|
use crate::template_parser::{self, FunctionCallNode, TemplateParseResult};
|
2023-02-19 08:05:50 +00:00
|
|
|
use crate::templater::{
|
2024-03-21 02:38:23 +00:00
|
|
|
IntoTemplate, PlainTextFormattedProperty, Template, TemplateProperty, TemplatePropertyExt as _,
|
2024-03-19 11:10:59 +00:00
|
|
|
TimestampRange,
|
2023-02-19 08:05:50 +00:00
|
|
|
};
|
|
|
|
|
2024-03-15 20:48:13 +00:00
|
|
|
pub trait OperationTemplateLanguageExtension {
|
|
|
|
fn build_fn_table(&self) -> OperationTemplateBuildFnTable;
|
|
|
|
|
|
|
|
fn build_cache_extensions(&self, extensions: &mut ExtensionsMap);
|
|
|
|
}
|
|
|
|
|
2024-03-03 14:43:11 +00:00
|
|
|
pub struct OperationTemplateLanguage {
|
2024-02-24 12:11:14 +00:00
|
|
|
root_op_id: OperationId,
|
|
|
|
current_op_id: Option<OperationId>,
|
2024-02-25 05:54:32 +00:00
|
|
|
build_fn_table: OperationTemplateBuildFnTable,
|
2024-03-15 20:48:13 +00:00
|
|
|
cache_extensions: ExtensionsMap,
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 14:20:07 +00:00
|
|
|
impl OperationTemplateLanguage {
|
|
|
|
/// Sets up environment where operation template will be transformed to
|
|
|
|
/// evaluation tree.
|
2024-03-15 20:48:13 +00:00
|
|
|
pub fn new(
|
|
|
|
root_op_id: &OperationId,
|
|
|
|
current_op_id: Option<&OperationId>,
|
|
|
|
extension: Option<&dyn OperationTemplateLanguageExtension>,
|
|
|
|
) -> Self {
|
|
|
|
let mut build_fn_table = OperationTemplateBuildFnTable::builtin();
|
|
|
|
let mut cache_extensions = ExtensionsMap::empty();
|
|
|
|
if let Some(extension) = extension {
|
|
|
|
let ext_table = extension.build_fn_table();
|
|
|
|
build_fn_table.merge(ext_table);
|
|
|
|
extension.build_cache_extensions(&mut cache_extensions);
|
|
|
|
}
|
|
|
|
|
2024-03-03 14:20:07 +00:00
|
|
|
OperationTemplateLanguage {
|
|
|
|
root_op_id: root_op_id.clone(),
|
|
|
|
current_op_id: current_op_id.cloned(),
|
2024-03-15 20:48:13 +00:00
|
|
|
build_fn_table,
|
|
|
|
cache_extensions,
|
2024-03-03 14:20:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 12:11:14 +00:00
|
|
|
impl TemplateLanguage<'static> for OperationTemplateLanguage {
|
2023-02-19 08:05:50 +00:00
|
|
|
type Property = OperationTemplatePropertyKind;
|
|
|
|
|
2023-03-12 11:10:19 +00:00
|
|
|
template_builder::impl_core_wrap_property_fns!('static, OperationTemplatePropertyKind::Core);
|
2023-02-19 08:05:50 +00:00
|
|
|
|
2024-03-14 11:00:29 +00:00
|
|
|
fn build_function(
|
|
|
|
&self,
|
|
|
|
build_ctx: &BuildContext<Self::Property>,
|
|
|
|
function: &FunctionCallNode,
|
|
|
|
) -> TemplateParseResult<Self::Property> {
|
2024-03-14 10:42:08 +00:00
|
|
|
let table = &self.build_fn_table.core;
|
|
|
|
table.build_function(self, build_ctx, function)
|
2024-03-14 11:00:29 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 08:05:50 +00:00
|
|
|
fn build_method(
|
|
|
|
&self,
|
2023-03-14 11:25:54 +00:00
|
|
|
build_ctx: &BuildContext<Self::Property>,
|
2023-02-19 08:05:50 +00:00
|
|
|
property: Self::Property,
|
|
|
|
function: &FunctionCallNode,
|
|
|
|
) -> TemplateParseResult<Self::Property> {
|
|
|
|
match property {
|
|
|
|
OperationTemplatePropertyKind::Core(property) => {
|
2024-02-25 04:50:06 +00:00
|
|
|
let table = &self.build_fn_table.core;
|
2024-02-27 06:49:38 +00:00
|
|
|
table.build_method(self, build_ctx, property, function)
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
2024-02-22 08:39:43 +00:00
|
|
|
OperationTemplatePropertyKind::Operation(property) => {
|
2024-02-25 05:54:32 +00:00
|
|
|
let table = &self.build_fn_table.operation_methods;
|
|
|
|
let build = template_parser::lookup_method("Operation", table, function)?;
|
|
|
|
build(self, build_ctx, property, function)
|
2024-02-22 08:39:43 +00:00
|
|
|
}
|
2023-02-19 08:05:50 +00:00
|
|
|
OperationTemplatePropertyKind::OperationId(property) => {
|
2024-02-25 05:54:32 +00:00
|
|
|
let table = &self.build_fn_table.operation_id_methods;
|
|
|
|
let build = template_parser::lookup_method("OperationId", table, function)?;
|
|
|
|
build(self, build_ctx, property, function)
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 12:11:14 +00:00
|
|
|
impl OperationTemplateLanguage {
|
2024-03-15 20:48:13 +00:00
|
|
|
pub fn cache_extension<T: Any>(&self) -> Option<&T> {
|
|
|
|
self.cache_extensions.get::<T>()
|
|
|
|
}
|
|
|
|
|
2024-03-03 14:43:11 +00:00
|
|
|
pub fn wrap_operation(
|
2024-03-20 04:08:34 +00:00
|
|
|
property: impl TemplateProperty<Output = Operation> + 'static,
|
2024-02-22 08:39:43 +00:00
|
|
|
) -> OperationTemplatePropertyKind {
|
|
|
|
OperationTemplatePropertyKind::Operation(Box::new(property))
|
|
|
|
}
|
|
|
|
|
2024-03-03 14:43:11 +00:00
|
|
|
pub fn wrap_operation_id(
|
2024-03-20 04:08:34 +00:00
|
|
|
property: impl TemplateProperty<Output = OperationId> + 'static,
|
2023-02-19 08:05:50 +00:00
|
|
|
) -> OperationTemplatePropertyKind {
|
2023-03-08 06:05:38 +00:00
|
|
|
OperationTemplatePropertyKind::OperationId(Box::new(property))
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 14:43:11 +00:00
|
|
|
pub enum OperationTemplatePropertyKind {
|
2024-03-20 04:08:34 +00:00
|
|
|
Core(CoreTemplatePropertyKind<'static>),
|
|
|
|
Operation(Box<dyn TemplateProperty<Output = Operation>>),
|
|
|
|
OperationId(Box<dyn TemplateProperty<Output = OperationId>>),
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 04:08:34 +00:00
|
|
|
impl IntoTemplateProperty<'static> for OperationTemplatePropertyKind {
|
|
|
|
fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<Output = bool>>> {
|
2023-02-19 08:05:50 +00:00
|
|
|
match self {
|
|
|
|
OperationTemplatePropertyKind::Core(property) => property.try_into_boolean(),
|
2024-02-22 08:39:43 +00:00
|
|
|
OperationTemplatePropertyKind::Operation(_) => None,
|
2023-10-27 00:28:06 +00:00
|
|
|
OperationTemplatePropertyKind::OperationId(_) => None,
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 04:08:34 +00:00
|
|
|
fn try_into_integer(self) -> Option<Box<dyn TemplateProperty<Output = i64>>> {
|
2023-02-19 08:05:50 +00:00
|
|
|
match self {
|
|
|
|
OperationTemplatePropertyKind::Core(property) => property.try_into_integer(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 04:08:34 +00:00
|
|
|
fn try_into_plain_text(self) -> Option<Box<dyn TemplateProperty<Output = String>>> {
|
2023-02-19 08:05:50 +00:00
|
|
|
match self {
|
2023-03-19 11:43:51 +00:00
|
|
|
OperationTemplatePropertyKind::Core(property) => property.try_into_plain_text(),
|
|
|
|
_ => {
|
|
|
|
let template = self.try_into_template()?;
|
|
|
|
Some(Box::new(PlainTextFormattedProperty::new(template)))
|
|
|
|
}
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 11:10:59 +00:00
|
|
|
fn try_into_template(self) -> Option<Box<dyn Template<()>>> {
|
2023-02-19 08:05:50 +00:00
|
|
|
match self {
|
2023-03-19 11:43:51 +00:00
|
|
|
OperationTemplatePropertyKind::Core(property) => property.try_into_template(),
|
2024-02-22 08:39:43 +00:00
|
|
|
OperationTemplatePropertyKind::Operation(_) => None,
|
2023-03-19 11:43:51 +00:00
|
|
|
OperationTemplatePropertyKind::OperationId(property) => Some(property.into_template()),
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 05:54:32 +00:00
|
|
|
/// Table of functions that translate method call node of self type `T`.
|
2024-03-03 14:43:11 +00:00
|
|
|
pub type OperationTemplateBuildMethodFnMap<T> =
|
2024-02-25 05:54:32 +00:00
|
|
|
TemplateBuildMethodFnMap<'static, OperationTemplateLanguage, T>;
|
|
|
|
|
|
|
|
/// Symbol table of methods available in the operation template.
|
2024-03-15 20:48:13 +00:00
|
|
|
pub struct OperationTemplateBuildFnTable {
|
2024-03-03 14:43:11 +00:00
|
|
|
pub core: CoreTemplateBuildFnTable<'static, OperationTemplateLanguage>,
|
|
|
|
pub operation_methods: OperationTemplateBuildMethodFnMap<Operation>,
|
|
|
|
pub operation_id_methods: OperationTemplateBuildMethodFnMap<OperationId>,
|
2024-02-25 05:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OperationTemplateBuildFnTable {
|
|
|
|
/// Creates new symbol table containing the builtin methods.
|
|
|
|
fn builtin() -> Self {
|
|
|
|
OperationTemplateBuildFnTable {
|
2024-02-25 04:50:06 +00:00
|
|
|
core: CoreTemplateBuildFnTable::builtin(),
|
2024-02-25 05:54:32 +00:00
|
|
|
operation_methods: builtin_operation_methods(),
|
|
|
|
operation_id_methods: builtin_operation_id_methods(),
|
|
|
|
}
|
|
|
|
}
|
2024-03-15 20:48:13 +00:00
|
|
|
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
OperationTemplateBuildFnTable {
|
|
|
|
core: CoreTemplateBuildFnTable::empty(),
|
|
|
|
operation_methods: HashMap::new(),
|
|
|
|
operation_id_methods: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge(&mut self, other: OperationTemplateBuildFnTable) {
|
|
|
|
let OperationTemplateBuildFnTable {
|
|
|
|
core,
|
|
|
|
operation_methods,
|
|
|
|
operation_id_methods,
|
|
|
|
} = other;
|
|
|
|
|
|
|
|
self.core.merge(core);
|
|
|
|
merge_fn_map(&mut self.operation_methods, operation_methods);
|
|
|
|
merge_fn_map(&mut self.operation_id_methods, operation_id_methods);
|
|
|
|
}
|
2024-02-25 05:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn builtin_operation_methods() -> OperationTemplateBuildMethodFnMap<Operation> {
|
2024-03-19 15:01:06 +00:00
|
|
|
type L = OperationTemplateLanguage;
|
2024-02-25 05:54:32 +00:00
|
|
|
// Not using maplit::hashmap!{} or custom declarative macro here because
|
|
|
|
// code completion inside macro is quite restricted.
|
|
|
|
let mut map = OperationTemplateBuildMethodFnMap::<Operation>::new();
|
|
|
|
map.insert(
|
|
|
|
"current_operation",
|
|
|
|
|language, _build_ctx, self_property, function| {
|
2024-02-23 03:12:30 +00:00
|
|
|
template_parser::expect_no_arguments(function)?;
|
2024-02-24 12:11:14 +00:00
|
|
|
let current_op_id = language.current_op_id.clone();
|
2024-03-21 02:38:23 +00:00
|
|
|
let out_property = self_property.map(move |op| Some(op.id()) == current_op_id.as_ref());
|
2024-03-19 15:01:06 +00:00
|
|
|
Ok(L::wrap_boolean(out_property))
|
2024-02-25 05:54:32 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
map.insert(
|
|
|
|
"description",
|
2024-03-19 15:01:06 +00:00
|
|
|
|_language, _build_ctx, self_property, function| {
|
2024-02-23 03:12:30 +00:00
|
|
|
template_parser::expect_no_arguments(function)?;
|
2024-03-21 02:38:23 +00:00
|
|
|
let out_property = self_property.map(|op| op.metadata().description.clone());
|
2024-03-19 15:01:06 +00:00
|
|
|
Ok(L::wrap_string(out_property))
|
2024-02-25 05:54:32 +00:00
|
|
|
},
|
|
|
|
);
|
2024-03-19 15:01:06 +00:00
|
|
|
map.insert("id", |_language, _build_ctx, self_property, function| {
|
2024-02-25 05:54:32 +00:00
|
|
|
template_parser::expect_no_arguments(function)?;
|
2024-03-21 02:38:23 +00:00
|
|
|
let out_property = self_property.map(|op| op.id().clone());
|
2024-03-19 15:01:06 +00:00
|
|
|
Ok(L::wrap_operation_id(out_property))
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
2024-03-19 15:01:06 +00:00
|
|
|
map.insert("tags", |_language, _build_ctx, self_property, function| {
|
2024-02-25 05:54:32 +00:00
|
|
|
template_parser::expect_no_arguments(function)?;
|
2024-03-21 02:38:23 +00:00
|
|
|
let out_property = self_property.map(|op| {
|
2024-02-25 05:54:32 +00:00
|
|
|
// TODO: introduce map type
|
2024-03-21 02:38:23 +00:00
|
|
|
op.metadata()
|
2024-02-25 05:54:32 +00:00
|
|
|
.tags
|
|
|
|
.iter()
|
|
|
|
.map(|(key, value)| format!("{key}: {value}"))
|
2024-03-21 02:38:23 +00:00
|
|
|
.join("\n")
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
2024-03-19 15:01:06 +00:00
|
|
|
Ok(L::wrap_string(out_property))
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
2024-03-19 15:01:06 +00:00
|
|
|
map.insert("time", |_language, _build_ctx, self_property, function| {
|
2024-02-25 05:54:32 +00:00
|
|
|
template_parser::expect_no_arguments(function)?;
|
2024-03-21 02:38:23 +00:00
|
|
|
let out_property = self_property.map(|op| TimestampRange {
|
|
|
|
start: op.metadata().start_time.clone(),
|
|
|
|
end: op.metadata().end_time.clone(),
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
2024-03-19 15:01:06 +00:00
|
|
|
Ok(L::wrap_timestamp_range(out_property))
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
2024-03-19 15:01:06 +00:00
|
|
|
map.insert("user", |_language, _build_ctx, self_property, function| {
|
2024-02-25 05:54:32 +00:00
|
|
|
template_parser::expect_no_arguments(function)?;
|
2024-03-21 02:38:23 +00:00
|
|
|
let out_property = self_property.map(|op| {
|
2024-02-25 05:54:32 +00:00
|
|
|
// TODO: introduce dedicated type and provide accessors?
|
2024-03-21 02:38:23 +00:00
|
|
|
format!("{}@{}", op.metadata().username, op.metadata().hostname)
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
2024-03-19 15:01:06 +00:00
|
|
|
Ok(L::wrap_string(out_property))
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
|
|
|
map.insert("root", |language, _build_ctx, self_property, function| {
|
|
|
|
template_parser::expect_no_arguments(function)?;
|
|
|
|
let root_op_id = language.root_op_id.clone();
|
2024-03-21 02:38:23 +00:00
|
|
|
let out_property = self_property.map(move |op| op.id() == &root_op_id);
|
2024-03-19 15:01:06 +00:00
|
|
|
Ok(L::wrap_boolean(out_property))
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
|
|
|
map
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Template<()> for OperationId {
|
|
|
|
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
2024-03-22 08:17:14 +00:00
|
|
|
write!(formatter, "{}", self.hex())
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 05:54:32 +00:00
|
|
|
fn builtin_operation_id_methods() -> OperationTemplateBuildMethodFnMap<OperationId> {
|
2024-03-19 15:01:06 +00:00
|
|
|
type L = OperationTemplateLanguage;
|
2024-02-25 05:54:32 +00:00
|
|
|
// Not using maplit::hashmap!{} or custom declarative macro here because
|
|
|
|
// code completion inside macro is quite restricted.
|
|
|
|
let mut map = OperationTemplateBuildMethodFnMap::<OperationId>::new();
|
|
|
|
map.insert("short", |language, build_ctx, self_property, function| {
|
|
|
|
let ([], [len_node]) = template_parser::expect_arguments(function)?;
|
|
|
|
let len_property = len_node
|
2024-02-27 11:37:00 +00:00
|
|
|
.map(|node| template_builder::expect_usize_expression(language, build_ctx, node))
|
2024-02-25 05:54:32 +00:00
|
|
|
.transpose()?;
|
2024-03-21 02:38:23 +00:00
|
|
|
let out_property = (self_property, len_property).map(|(id, len)| {
|
2024-02-25 05:54:32 +00:00
|
|
|
let mut hex = id.hex();
|
2024-02-27 11:37:00 +00:00
|
|
|
hex.truncate(len.unwrap_or(12));
|
2024-03-21 02:38:23 +00:00
|
|
|
hex
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
2024-03-19 15:01:06 +00:00
|
|
|
Ok(L::wrap_string(out_property))
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
|
|
|
map
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|