cli: render other kind of revset error suggestion as hint

This commit is contained in:
Yuya Nishihara 2023-03-23 19:07:34 +09:00
parent ddeb645d7f
commit 9d661d6f69
3 changed files with 16 additions and 4 deletions

View file

@ -62,13 +62,13 @@ pub struct RevsetParseError {
pub enum RevsetParseErrorKind {
#[error("Syntax error")]
SyntaxError,
#[error("'{op}' is not a postfix operator (Did you mean '{similar_op}' for {description}?)")]
#[error("'{op}' is not a postfix operator")]
NotPostfixOperator {
op: String,
similar_op: String,
description: String,
},
#[error("'{op}' is not an infix operator (Did you mean '{similar_op}' for {description}?)")]
#[error("'{op}' is not an infix operator")]
NotInfixOperator {
op: String,
similar_op: String,

View file

@ -254,6 +254,16 @@ impl From<RevsetParseError> for CommandError {
let message = iter::successors(Some(&err), |e| e.origin()).join("\n");
// Only for the top-level error as we can't attach hint to inner errors
let hint = match err.kind() {
RevsetParseErrorKind::NotPostfixOperator {
op: _,
similar_op,
description,
}
| RevsetParseErrorKind::NotInfixOperator {
op: _,
similar_op,
description,
} => Some(format!("Did you mean '{similar_op}' for {description}?")),
RevsetParseErrorKind::NoSuchFunction { name, candidates } => {
format_similarity_hint(&collect_similar(name, candidates))
}

View file

@ -39,7 +39,8 @@ fn test_syntax_error() {
1 | x - y
| ^
|
= '-' is not an infix operator (Did you mean '~' for difference?)
= '-' is not an infix operator
Hint: Did you mean '~' for difference?
"###);
let stderr = test_env.jj_cmd_failure(&repo_path, &["log", "-r", "HEAD^"]);
@ -49,7 +50,8 @@ fn test_syntax_error() {
1 | HEAD^
| ^
|
= '^' is not a postfix operator (Did you mean '-' for parents?)
= '^' is not a postfix operator
Hint: Did you mean '-' for parents?
"###);
}