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.
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use itertools::Itertools as _;
|
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-02-25 04:50:06 +00:00
|
|
|
self, BuildContext, CoreTemplateBuildFnTable, CoreTemplatePropertyKind, IntoTemplateProperty,
|
|
|
|
TemplateBuildMethodFnMap, TemplateLanguage,
|
2023-02-19 08:05:50 +00:00
|
|
|
};
|
2024-02-25 05:54:32 +00:00
|
|
|
use crate::template_parser::{self, FunctionCallNode, TemplateAliasesMap, TemplateParseResult};
|
2023-02-19 08:05:50 +00:00
|
|
|
use crate::templater::{
|
2023-03-08 05:48:27 +00:00
|
|
|
IntoTemplate, PlainTextFormattedProperty, Template, TemplateFunction, TemplateProperty,
|
|
|
|
TemplatePropertyFn, TimestampRange,
|
2023-02-19 08:05:50 +00:00
|
|
|
};
|
|
|
|
|
2024-02-24 12:11:14 +00:00
|
|
|
struct OperationTemplateLanguage {
|
|
|
|
root_op_id: OperationId,
|
|
|
|
current_op_id: Option<OperationId>,
|
2024-02-25 05:54:32 +00:00
|
|
|
build_fn_table: OperationTemplateBuildFnTable,
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 12:11:14 +00:00
|
|
|
impl TemplateLanguage<'static> for OperationTemplateLanguage {
|
2023-02-19 08:05:50 +00:00
|
|
|
type Context = Operation;
|
|
|
|
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-02-22 08:44:37 +00:00
|
|
|
fn build_self(&self) -> Self::Property {
|
|
|
|
// Operation object is lightweight (a few Arc + OperationId)
|
2024-02-27 10:42:50 +00:00
|
|
|
self.wrap_operation(TemplatePropertyFn(|op: &Operation| Ok(op.clone())))
|
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-02-22 08:39:43 +00:00
|
|
|
fn wrap_operation(
|
|
|
|
&self,
|
|
|
|
property: impl TemplateProperty<Operation, Output = Operation> + 'static,
|
|
|
|
) -> OperationTemplatePropertyKind {
|
|
|
|
OperationTemplatePropertyKind::Operation(Box::new(property))
|
|
|
|
}
|
|
|
|
|
2023-02-19 08:05:50 +00:00
|
|
|
fn wrap_operation_id(
|
|
|
|
&self,
|
2023-03-08 06:05:38 +00:00
|
|
|
property: impl TemplateProperty<Operation, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum OperationTemplatePropertyKind {
|
|
|
|
Core(CoreTemplatePropertyKind<'static, Operation>),
|
2024-02-22 08:39:43 +00:00
|
|
|
Operation(Box<dyn TemplateProperty<Operation, Output = Operation>>),
|
2023-02-19 08:05:50 +00:00
|
|
|
OperationId(Box<dyn TemplateProperty<Operation, Output = OperationId>>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoTemplateProperty<'static, Operation> for OperationTemplatePropertyKind {
|
|
|
|
fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<Operation, Output = bool>>> {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_into_integer(self) -> Option<Box<dyn TemplateProperty<Operation, Output = i64>>> {
|
|
|
|
match self {
|
|
|
|
OperationTemplatePropertyKind::Core(property) => property.try_into_integer(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-19 11:43:51 +00:00
|
|
|
fn try_into_plain_text(self) -> Option<Box<dyn TemplateProperty<Operation, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-19 11:43:51 +00:00
|
|
|
fn try_into_template(self) -> Option<Box<dyn Template<Operation>>> {
|
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`.
|
|
|
|
type OperationTemplateBuildMethodFnMap<T> =
|
|
|
|
TemplateBuildMethodFnMap<'static, OperationTemplateLanguage, T>;
|
|
|
|
|
|
|
|
/// Symbol table of methods available in the operation template.
|
|
|
|
struct OperationTemplateBuildFnTable {
|
2024-02-25 04:50:06 +00:00
|
|
|
core: CoreTemplateBuildFnTable<'static, OperationTemplateLanguage>,
|
2024-02-25 05:54:32 +00:00
|
|
|
operation_methods: OperationTemplateBuildMethodFnMap<Operation>,
|
|
|
|
operation_id_methods: OperationTemplateBuildMethodFnMap<OperationId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn builtin_operation_methods() -> OperationTemplateBuildMethodFnMap<Operation> {
|
|
|
|
// 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-02-25 05:54:32 +00:00
|
|
|
let out_property = TemplateFunction::new(self_property, move |op| {
|
2024-02-27 10:42:50 +00:00
|
|
|
Ok(Some(op.id()) == current_op_id.as_ref())
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
|
|
|
Ok(language.wrap_boolean(out_property))
|
|
|
|
},
|
|
|
|
);
|
|
|
|
map.insert(
|
|
|
|
"description",
|
|
|
|
|language, _build_ctx, self_property, function| {
|
2024-02-23 03:12:30 +00:00
|
|
|
template_parser::expect_no_arguments(function)?;
|
2024-02-25 05:54:32 +00:00
|
|
|
let out_property =
|
2024-02-27 10:42:50 +00:00
|
|
|
TemplateFunction::new(self_property, |op| Ok(op.metadata().description.clone()));
|
2024-02-25 05:54:32 +00:00
|
|
|
Ok(language.wrap_string(out_property))
|
|
|
|
},
|
|
|
|
);
|
|
|
|
map.insert("id", |language, _build_ctx, self_property, function| {
|
|
|
|
template_parser::expect_no_arguments(function)?;
|
2024-02-27 10:42:50 +00:00
|
|
|
let out_property = TemplateFunction::new(self_property, |op| Ok(op.id().clone()));
|
2024-02-25 05:54:32 +00:00
|
|
|
Ok(language.wrap_operation_id(out_property))
|
|
|
|
});
|
|
|
|
map.insert("tags", |language, _build_ctx, self_property, function| {
|
|
|
|
template_parser::expect_no_arguments(function)?;
|
|
|
|
let out_property = TemplateFunction::new(self_property, |op| {
|
|
|
|
// TODO: introduce map type
|
2024-02-27 10:42:50 +00:00
|
|
|
Ok(op
|
|
|
|
.metadata()
|
2024-02-25 05:54:32 +00:00
|
|
|
.tags
|
|
|
|
.iter()
|
|
|
|
.map(|(key, value)| format!("{key}: {value}"))
|
2024-02-27 10:42:50 +00:00
|
|
|
.join("\n"))
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
|
|
|
Ok(language.wrap_string(out_property))
|
|
|
|
});
|
|
|
|
map.insert("time", |language, _build_ctx, self_property, function| {
|
|
|
|
template_parser::expect_no_arguments(function)?;
|
2024-02-27 10:42:50 +00:00
|
|
|
let out_property = TemplateFunction::new(self_property, |op| {
|
|
|
|
Ok(TimestampRange {
|
|
|
|
start: op.metadata().start_time.clone(),
|
|
|
|
end: op.metadata().end_time.clone(),
|
|
|
|
})
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
|
|
|
Ok(language.wrap_timestamp_range(out_property))
|
|
|
|
});
|
|
|
|
map.insert("user", |language, _build_ctx, self_property, function| {
|
|
|
|
template_parser::expect_no_arguments(function)?;
|
|
|
|
let out_property = TemplateFunction::new(self_property, |op| {
|
|
|
|
// TODO: introduce dedicated type and provide accessors?
|
2024-02-27 10:42:50 +00:00
|
|
|
Ok(format!(
|
|
|
|
"{}@{}",
|
|
|
|
op.metadata().username,
|
|
|
|
op.metadata().hostname
|
|
|
|
))
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
|
|
|
Ok(language.wrap_string(out_property))
|
|
|
|
});
|
|
|
|
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-02-27 10:42:50 +00:00
|
|
|
let out_property =
|
|
|
|
TemplateFunction::new(self_property, move |op| Ok(op.id() == &root_op_id));
|
2024-02-25 05:54:32 +00:00
|
|
|
Ok(language.wrap_boolean(out_property))
|
|
|
|
});
|
|
|
|
map
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Template<()> for OperationId {
|
|
|
|
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
|
|
|
formatter.write_str(&self.hex())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 05:54:32 +00:00
|
|
|
fn builtin_operation_id_methods() -> OperationTemplateBuildMethodFnMap<OperationId> {
|
|
|
|
// 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()?;
|
|
|
|
let out_property = TemplateFunction::new((self_property, len_property), |(id, len)| {
|
|
|
|
let mut hex = id.hex();
|
2024-02-27 11:37:00 +00:00
|
|
|
hex.truncate(len.unwrap_or(12));
|
2024-02-27 10:42:50 +00:00
|
|
|
Ok(hex)
|
2024-02-25 05:54:32 +00:00
|
|
|
});
|
|
|
|
Ok(language.wrap_string(out_property))
|
|
|
|
});
|
|
|
|
map
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse(
|
2024-01-14 01:02:15 +00:00
|
|
|
root_op_id: &OperationId,
|
2024-01-11 03:15:07 +00:00
|
|
|
current_op_id: Option<&OperationId>,
|
2023-02-19 08:05:50 +00:00
|
|
|
template_text: &str,
|
|
|
|
aliases_map: &TemplateAliasesMap,
|
|
|
|
) -> TemplateParseResult<Box<dyn Template<Operation>>> {
|
2024-01-14 01:02:15 +00:00
|
|
|
let language = OperationTemplateLanguage {
|
2024-02-24 12:11:14 +00:00
|
|
|
root_op_id: root_op_id.clone(),
|
|
|
|
current_op_id: current_op_id.cloned(),
|
2024-02-25 05:54:32 +00:00
|
|
|
build_fn_table: OperationTemplateBuildFnTable::builtin(),
|
2024-01-14 01:02:15 +00:00
|
|
|
};
|
2023-03-12 11:02:32 +00:00
|
|
|
let node = template_parser::parse(template_text, aliases_map)?;
|
2023-03-14 11:25:54 +00:00
|
|
|
template_builder::build(&language, &node)
|
2024-02-26 07:06:51 +00:00
|
|
|
.map_err(|err| err.extend_alias_candidates(aliases_map))
|
2023-02-19 08:05:50 +00:00
|
|
|
}
|