Skip to content

Commit

Permalink
templater: expand similarity hint with aliases
Browse files Browse the repository at this point in the history
-Tbuiltin now shows the list of the builtin templates, which seems useful.
  • Loading branch information
yuja committed Feb 28, 2024
1 parent 71a9dc8 commit 7bc4521
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,4 +804,5 @@ pub fn parse<'repo>(
};
let node = template_parser::parse(template_text, aliases_map)?;
template_builder::build(&language, &node)
.map_err(|err| err.extend_alias_candidates(aliases_map))
}
1 change: 1 addition & 0 deletions cli/src/operation_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,5 @@ pub fn parse(
};
let node = template_parser::parse(template_text, aliases_map)?;
template_builder::build(&language, &node)
.map_err(|err| err.extend_alias_candidates(aliases_map))
}
22 changes: 22 additions & 0 deletions cli/src/template_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,28 @@ impl TemplateParseError {
self
}

/// If this is a `NoSuchFunction` error, expands the candidates list with
/// the given `other_functions`.
pub fn extend_function_candidates<I>(mut self, other_functions: I) -> Self
where
I: IntoIterator,
I::Item: AsRef<str>,
{
if let TemplateParseErrorKind::NoSuchFunction { name, candidates } = &mut self.kind {
let other_candidates = collect_similar(name, other_functions);
*candidates = itertools::merge(mem::take(candidates), other_candidates)
.dedup()
.collect();
}
self
}

/// Expands keyword/function candidates with the given aliases.
pub fn extend_alias_candidates(self, aliases_map: &TemplateAliasesMap) -> Self {
self.extend_keyword_candidates(aliases_map.symbol_aliases.keys())
.extend_function_candidates(aliases_map.function_aliases.keys())
}

pub fn kind(&self) -> &TemplateParseErrorKind {
&self.kind
}
Expand Down
18 changes: 17 additions & 1 deletion cli/tests/test_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ fn test_templater_parse_error() {
test_env.add_config(
r###"
[template-aliases]
'conflicting' = ''
'shorted()' = ''
'cap(x)' = 'x'
'format_id(id)' = 'id.sort()'
"###,
);
Expand All @@ -46,7 +49,7 @@ fn test_templater_parse_error() {
| ^-------^
|
= Keyword "conflicts" doesn't exist
Hint: Did you mean "conflict"?
Hint: Did you mean "conflict", "conflicting"?
"###);
insta::assert_snapshot!(render_err(r#"commit_id.shorter()"#), @r###"
Error: Failed to parse template: --> 1:11
Expand All @@ -64,6 +67,7 @@ fn test_templater_parse_error() {
| ^-^
|
= Function "cat" doesn't exist
Hint: Did you mean "cap"?
"###);
insta::assert_snapshot!(render_err(r#""".lines().map(|s| se)"#), @r###"
Error: Failed to parse template: --> 1:20
Expand All @@ -89,6 +93,18 @@ fn test_templater_parse_error() {
= Method "sort" doesn't exist for type "CommitOrChangeId"
Hint: Did you mean "short", "shortest"?
"###);

// -Tbuiltin shows the predefined builtin_* aliases. This isn't 100%
// guaranteed, but is nice.
insta::assert_snapshot!(render_err(r#"builtin"#), @r###"
Error: Failed to parse template: --> 1:1
|
1 | builtin
| ^-----^
|
= Keyword "builtin" doesn't exist
Hint: Did you mean "builtin_change_id_with_hidden_and_divergent_info", "builtin_log_comfortable", "builtin_log_compact", "builtin_log_detailed", "builtin_log_oneline", "builtin_op_log_comfortable", "builtin_op_log_compact"?
"###);
}

#[test]
Expand Down

0 comments on commit 7bc4521

Please sign in to comment.