Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commit_templater: encode elided node as Option<Commit> #3319

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Graph node symbols are now configurable via templates
* `templates.log_node`
* `templates.op_log_node`
* `templates.log_node_elided`


* `jj log` now includes synthetic nodes in the graph where some revisions were
elided.
Expand Down
17 changes: 5 additions & 12 deletions cli/src/commands/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use crate::cli_util::{format_template, CommandHelper, LogContentFormat, Revision
use crate::command_error::CommandError;
use crate::commit_templater::CommitTemplateLanguage;
use crate::diff_util::{self, DiffFormatArgs};
use crate::generic_templater::GenericTemplateLanguage;
use crate::graphlog::{get_graphlog, Edge};
use crate::templater::Template as _;
use crate::ui::Ui;
Expand Down Expand Up @@ -111,7 +110,7 @@ pub(crate) fn cmd_log(
let with_content_format = LogContentFormat::new(ui, command.settings())?;

let template;
let commit_node_template;
let node_template;
{
let language = workspace_command.commit_template_language()?;
let template_string = match &args.template {
Expand All @@ -123,19 +122,13 @@ pub(crate) fn cmd_log(
&template_string,
CommitTemplateLanguage::wrap_commit,
)?;
commit_node_template = workspace_command.parse_template(
node_template = workspace_command.parse_template(
&language,
&command.settings().commit_node_template(),
CommitTemplateLanguage::wrap_commit,
CommitTemplateLanguage::wrap_commit_opt,
)?;
}

let elided_node_template = workspace_command.parse_template(
&GenericTemplateLanguage::new(),
&command.settings().elided_node_template(),
GenericTemplateLanguage::wrap_self,
)?;

{
ui.request_pager();
let mut formatter = ui.stdout_formatter();
Expand Down Expand Up @@ -201,7 +194,7 @@ pub(crate) fn cmd_log(
)?;
}

let node_symbol = format_template(ui, &commit, &commit_node_template);
let node_symbol = format_template(ui, &Some(commit), &node_template);
graph.add_node(
&key,
&graphlog_edges,
Expand All @@ -218,7 +211,7 @@ pub(crate) fn cmd_log(
|formatter| writeln!(formatter.labeled("elided"), "(elided revisions)"),
|| graph.width(&elided_key, &edges),
)?;
let node_symbol = format_template(ui, &(), &elided_node_template);
let node_symbol = format_template(ui, &None, &node_template);
graph.add_node(
&elided_key,
&edges,
Expand Down
8 changes: 4 additions & 4 deletions cli/src/commands/obslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub(crate) fn cmd_obslog(
let with_content_format = LogContentFormat::new(ui, command.settings())?;

let template;
let commit_node_template;
let node_template;
{
let language = workspace_command.commit_template_language()?;
let template_string = match &args.template {
Expand All @@ -85,10 +85,10 @@ pub(crate) fn cmd_obslog(
&template_string,
CommitTemplateLanguage::wrap_commit,
)?;
commit_node_template = workspace_command.parse_template(
node_template = workspace_command.parse_template(
&language,
&command.settings().commit_node_template(),
CommitTemplateLanguage::wrap_commit,
CommitTemplateLanguage::wrap_commit_opt,
)?;
}

Expand Down Expand Up @@ -131,7 +131,7 @@ pub(crate) fn cmd_obslog(
&diff_formats,
)?;
}
let node_symbol = format_template(ui, &commit, &commit_node_template);
let node_symbol = format_template(ui, &Some(commit.clone()), &node_template);
graph.add_node(
commit.id(),
&edges,
Expand Down
21 changes: 20 additions & 1 deletion cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::template_builder::{
use crate::template_parser::{self, FunctionCallNode, TemplateParseError, TemplateParseResult};
use crate::templater::{
self, IntoTemplate, PlainTextFormattedProperty, Template, TemplateProperty,
TemplatePropertyExt as _,
TemplatePropertyError, TemplatePropertyExt as _,
};
use crate::{revset_util, text_util};

Expand Down Expand Up @@ -126,6 +126,14 @@ impl<'repo> TemplateLanguage<'repo> for CommitTemplateLanguage<'repo> {
let build = template_parser::lookup_method("Commit", table, function)?;
build(self, build_ctx, property, function)
}
CommitTemplatePropertyKind::CommitOpt(property) => {
let table = &self.build_fn_table.commit_methods;
let build = template_parser::lookup_method("Commit", table, function)?;
let inner_property = property.and_then(|opt| {
opt.ok_or_else(|| TemplatePropertyError("No commit available".into()))
});
build(self, build_ctx, Box::new(inner_property), function)
}
CommitTemplatePropertyKind::CommitList(property) => {
// TODO: migrate to table?
template_builder::build_unformattable_list_method(
Expand Down Expand Up @@ -190,6 +198,12 @@ impl<'repo> CommitTemplateLanguage<'repo> {
CommitTemplatePropertyKind::Commit(Box::new(property))
}

pub fn wrap_commit_opt(
property: impl TemplateProperty<Output = Option<Commit>> + 'repo,
) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::CommitOpt(Box::new(property))
}

pub fn wrap_commit_list(
property: impl TemplateProperty<Output = Vec<Commit>> + 'repo,
) -> CommitTemplatePropertyKind<'repo> {
Expand Down Expand Up @@ -224,6 +238,7 @@ impl<'repo> CommitTemplateLanguage<'repo> {
pub enum CommitTemplatePropertyKind<'repo> {
Core(CoreTemplatePropertyKind<'repo>),
Commit(Box<dyn TemplateProperty<Output = Commit> + 'repo>),
CommitOpt(Box<dyn TemplateProperty<Output = Option<Commit>> + 'repo>),
CommitList(Box<dyn TemplateProperty<Output = Vec<Commit>> + 'repo>),
RefName(Box<dyn TemplateProperty<Output = RefName> + 'repo>),
RefNameList(Box<dyn TemplateProperty<Output = Vec<RefName>> + 'repo>),
Expand All @@ -236,6 +251,9 @@ impl<'repo> IntoTemplateProperty<'repo> for CommitTemplatePropertyKind<'repo> {
match self {
CommitTemplatePropertyKind::Core(property) => property.try_into_boolean(),
CommitTemplatePropertyKind::Commit(_) => None,
CommitTemplatePropertyKind::CommitOpt(property) => {
Some(Box::new(property.map(|opt| opt.is_some())))
}
CommitTemplatePropertyKind::CommitList(property) => {
Some(Box::new(property.map(|l| !l.is_empty())))
}
Expand Down Expand Up @@ -269,6 +287,7 @@ impl<'repo> IntoTemplateProperty<'repo> for CommitTemplatePropertyKind<'repo> {
match self {
CommitTemplatePropertyKind::Core(property) => property.try_into_template(),
CommitTemplatePropertyKind::Commit(_) => None,
CommitTemplatePropertyKind::CommitOpt(_) => None,
CommitTemplatePropertyKind::CommitList(_) => None,
CommitTemplatePropertyKind::RefName(property) => Some(property.into_template()),
CommitTemplatePropertyKind::RefNameList(property) => Some(property.into_template()),
Expand Down
24 changes: 13 additions & 11 deletions cli/tests/test_log_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,11 +1433,12 @@ fn test_log_with_custom_symbols() {
};

// Simple test with showing default and elided nodes.
test_env.add_config(concat!(
"ui.log-synthetic-elided-nodes = true\n",
"templates.log_node = 'if(current_working_copy, \"$\", if(root, \"┴\", \"┝\"))'\n",
"templates.log_node_elided = '\"🮀\"'",
));
test_env.add_config(
r###"
ui.log-synthetic-elided-nodes = true
templates.log_node = 'if(self, if(current_working_copy, "$", if(root, "┴", "┝")), "🮀")'
"###,
);
insta::assert_snapshot!(get_log("@ | @- | description(initial) | root()"), @r###"
$ merge
├─╮
Expand All @@ -1454,12 +1455,13 @@ fn test_log_with_custom_symbols() {
"###);

// Simple test with showing default and elided nodes, ascii style.
test_env.add_config(concat!(
"ui.log-synthetic-elided-nodes = true\n",
"ui.graph.style = 'ascii'\n",
"templates.log_node = 'if(current_working_copy, \"$\", if(root, \"^\", \"*\"))'\n",
"templates.log_node_elided = '\":\"'",
));
test_env.add_config(
r###"
ui.log-synthetic-elided-nodes = true
ui.graph.style = 'ascii'
templates.log_node = 'if(self, if(current_working_copy, "$", if(root, "^", "*")), ":")'
"###,
);
insta::assert_snapshot!(get_log("@ | @- | description(initial) | root()"), @r###"
$ merge
|\
Expand Down
10 changes: 5 additions & 5 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,23 @@ ui.graph.style = "square"
The symbols used to represent commits or operations can be customized via
templates.

* `templates.log_node` for commits (with `Commit` keywords)
* `templates.log_node` for commits (with `Option<Commit>` keywords)
* `templates.op_log_node` for operations (with `Operation` keywords)
* `templates.log_node_elided` for elided nodes

For example:
```toml
[templates]
log_node = '''
if(self,
if(current_working_copy, "@",
if(root, "┴",
if(immutable, "●", "○")
)
)
),
"🮀",
)
'''
op_log_node = 'if(current_operation, "@", "○")'
log_node_elided = '"🮀"'

```

### Wrap log content
Expand Down
6 changes: 6 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ The following methods are defined.

* `.short([len: Integer]) -> String`

### Option type

An option can be implicitly converted to `Boolean` denoting whether the
contained value is set. If set, all methods of the contained value can be
invoked. If not set, an error will be reported inline on method call.

### RefName type

The following methods are defined.
Expand Down
8 changes: 2 additions & 6 deletions lib/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ impl UserSettings {
pub fn commit_node_template(&self) -> String {
self.node_template_for_key(
"templates.log_node",
r#"if(current_working_copy, "@", "◉")"#,
r#"if(current_working_copy, "@", "o")"#,
r#"if(self, if(current_working_copy, "@", "◉"), "◌")"#,
r#"if(self, if(current_working_copy, "@", "o"), ".")"#,
)
}

Expand All @@ -258,10 +258,6 @@ impl UserSettings {
)
}

pub fn elided_node_template(&self) -> String {
self.node_template_for_key("templates.log_node_elided", r#""◌""#, r#"".""#)
}

pub fn max_new_file_size(&self) -> Result<u64, config::ConfigError> {
let cfg = self
.config
Expand Down