Debian Codegraph @

cli/examples/custom-operation-templater/main.rs

raw
1 // Copyright 2024 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use jj_cli::cli_util::CliRunner;
16 use jj_cli::operation_templater::OperationTemplateLanguageBuildFnTable;
17 use jj_cli::operation_templater::OperationTemplateLanguageExtension;
18 use jj_cli::template_parser;
19 use jj_cli::template_parser::TemplateParseError;
20 use jj_cli::templater::TemplatePropertyExt as _;
21 use jj_lib::extensions_map::ExtensionsMap;
22 use jj_lib::object_id::ObjectId as _;
23 use jj_lib::op_store::OperationId;
24 use jj_lib::operation::Operation;
25
26 struct HexCounter;
27
28 fn num_digits_in_id(id: &OperationId) -> i64 {
29 let mut count = 0;
30 for ch in id.hex().chars() {
31 if ch.is_ascii_digit() {
32 count += 1;
33 }
34 }
35 count
36 }
37
38 fn num_char_in_id(operation: Operation, ch_match: char) -> i64 {
39 let mut count = 0;
40 for ch in operation.id().hex().chars() {
41 if ch == ch_match {
42 count += 1;
43 }
44 }
45 count
46 }
47
48 impl OperationTemplateLanguageExtension for HexCounter {
49 fn build_fn_table(&self) -> OperationTemplateLanguageBuildFnTable {
50 let mut table = OperationTemplateLanguageBuildFnTable::empty();
51 table.operation.operation_methods.insert(
52 "num_digits_in_id",
53 |_language, _diagnostics, _build_context, property, call| {
54 call.expect_no_arguments()?;
55 let out_property = property.map(|operation| num_digits_in_id(operation.id()));
56 Ok(out_property.into_dyn_wrapped())
57 },
58 );
59 table.operation.operation_methods.insert(
60 "num_char_in_id",
61 |_language, diagnostics, _build_context, property, call| {
62 let [string_arg] = call.expect_exact_arguments()?;
63 let char_arg = template_parser::catch_aliases(
64 diagnostics,
65 string_arg,
66 |_diagnostics, arg| {
67 let string = template_parser::expect_string_literal(arg)?;
68 let chars: Vec<_> = string.chars().collect();
69 match chars[..] {
70 [ch] => Ok(ch),
71 _ => Err(TemplateParseError::expression(
72 "Expected singular character argument",
73 arg.span,
74 )),
75 }
76 },
77 )?;
78
79 let out_property =
80 property.map(move |operation| num_char_in_id(operation, char_arg));
81 Ok(out_property.into_dyn_wrapped())
82 },
83 );
84
85 table
86 }
87
88 fn build_cache_extensions(&self, _extensions: &mut ExtensionsMap) {}
89 }
90
91 fn main() -> std::process::ExitCode {
92 CliRunner::init()
93 .add_operation_template_extension(Box::new(HexCounter))
94 .run()
95 .into()
96 }