forked from mirrors/jj
templater: expand similarity hint with aliases
-Tbuiltin now shows the list of the builtin templates, which seems useful.
This commit is contained in:
parent
71a9dc8304
commit
7bc4521862
4 changed files with 41 additions and 1 deletions
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()'
|
||||
"###,
|
||||
);
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue