-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit_templater: support extensions of the template language
- Loading branch information
1 parent
5e8227a
commit d905698
Showing
3 changed files
with
153 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2024 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::sync::Arc; | ||
|
||
use jj_cli::cli_util::CliRunner; | ||
use jj_cli::commit_templater::{ | ||
CommitTemplateBuildFnTable, CommitTemplateLanguageExtension, CommitTemplatePropertyKind, | ||
}; | ||
use jj_cli::template_builder::CoreTemplatePropertyKind; | ||
use jj_cli::template_parser::{self, ExpressionKind, ExpressionNode, TemplateParseError}; | ||
use jj_cli::templater::{TemplateFunction, TemplatePropertyError}; | ||
use jj_lib::commit::Commit; | ||
use jj_lib::object_id::ObjectId; | ||
|
||
struct HexCounter; | ||
|
||
fn num_digits_in_id(commit: Commit) -> Result<i64, TemplatePropertyError> { | ||
let mut count = 0; | ||
for ch in commit.id().hex().chars() { | ||
if ch.is_ascii_digit() { | ||
count += 1; | ||
} | ||
} | ||
Ok(count) | ||
} | ||
|
||
fn num_char_in_id(commit: Commit, ch_match: char) -> Result<i64, TemplatePropertyError> { | ||
let mut count = 0; | ||
for ch in commit.id().hex().chars() { | ||
if ch == ch_match { | ||
count += 1; | ||
} | ||
} | ||
Ok(count) | ||
} | ||
|
||
impl CommitTemplateLanguageExtension for HexCounter { | ||
fn build_fn_table<'repo>(&self) -> CommitTemplateBuildFnTable<'repo> { | ||
let mut table = CommitTemplateBuildFnTable::new(); | ||
table.commit_methods.insert( | ||
"num_digits_in_id", | ||
|_language, _build_context, property, call| { | ||
template_parser::expect_no_arguments(call)?; | ||
Ok(CommitTemplatePropertyKind::Core( | ||
CoreTemplatePropertyKind::Integer(Box::new(TemplateFunction::new( | ||
property, | ||
num_digits_in_id, | ||
))), | ||
)) | ||
}, | ||
); | ||
table.commit_methods.insert( | ||
"num_char_in_id", | ||
|_language, _build_context, property, call| match &call.args[..] { | ||
[ExpressionNode { | ||
kind: ExpressionKind::String(string), | ||
span: _, | ||
}] => { | ||
let chars: Vec<_> = string.chars().collect(); | ||
if chars.len() != 1 { | ||
return Err(TemplateParseError::invalid_arguments( | ||
call, | ||
"Expected single character argument", | ||
)); | ||
} | ||
|
||
let char = chars[0]; | ||
Ok(CommitTemplatePropertyKind::Core( | ||
CoreTemplatePropertyKind::Integer(Box::new(TemplateFunction::new( | ||
property, | ||
move |commit| num_char_in_id(commit, char), | ||
))), | ||
)) | ||
} | ||
_ => Err(TemplateParseError::invalid_arguments( | ||
call, | ||
"Expected single character argument", | ||
)), | ||
}, | ||
); | ||
|
||
table | ||
} | ||
} | ||
|
||
fn main() -> std::process::ExitCode { | ||
CliRunner::init() | ||
.set_commit_template_extension(Arc::new(HexCounter)) | ||
.run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters