External merge tools: better error message for unsupported usage

I've heard of one instance of a person being confused by the error.

Previously, the error was:

```
Error: Failed to load tool configuration
Caused by: To use `diffedit3` as a merge tool, the config `merge-tools.diffedit3.merge-args` must be defined (see docs for details)

```

Now, it is:

```
Error: The tool `diffedit3` cannot be used as a merge tool with `jj resolve`.
Hint: To use `diffedit3` as a merge tool, the config `merge-tools.diffedit3.merge-args` must be defined (see docs for details)
```

TODO for future PR: allow setting `merge-tools.TOOL.edit-args = false` so that
attempting to use TOOL as a diff editor fails. This would be helpful, for
example, for the `vscode` tool.
This commit is contained in:
Ilya Grigoriev 2024-05-04 21:37:08 -07:00
parent 638649b435
commit 04158c3744
3 changed files with 20 additions and 5 deletions

View file

@ -375,7 +375,20 @@ impl From<ConflictResolveError> for CommandError {
impl From<MergeToolConfigError> for CommandError {
fn from(err: MergeToolConfigError) -> Self {
user_error_with_message("Failed to load tool configuration", err)
match &err {
MergeToolConfigError::MergeArgsNotConfigured { tool_name } => {
let tool_name = tool_name.clone();
user_error_with_hint(
err,
format!(
"To use `{tool_name}` as a merge tool, the config \
`merge-tools.{tool_name}.merge-args` must be defined (see docs for \
details)"
),
)
}
_ => user_error_with_message("Failed to load tool configuration", err),
}
}
}

View file

@ -55,6 +55,11 @@ impl Default for ExternalMergeTool {
fn default() -> Self {
Self {
program: String::new(),
// TODO(ilyagr): There should be a way to explicitly specify that a
// certain tool (e.g. vscode as of this writing) cannot be used as a
// diff editor (or a diff tool). A possible TOML syntax would be
// `edit-args = false`, or `edit-args = []`, or `edit = { disabled =
// true }` to go with `edit = { args = [...] }`.
diff_args: ["$left", "$right"].map(ToOwned::to_owned).to_vec(),
edit_args: ["$left", "$right"].map(ToOwned::to_owned).to_vec(),
merge_args: vec![],

View file

@ -91,10 +91,7 @@ pub enum ConflictResolveError {
pub enum MergeToolConfigError {
#[error(transparent)]
Config(#[from] ConfigError),
#[error(
"To use `{tool_name}` as a merge tool, the config `merge-tools.{tool_name}.merge-args` \
must be defined (see docs for details)"
)]
#[error("The tool `{tool_name}` cannot be used as a merge tool with `jj resolve`")]
MergeArgsNotConfigured { tool_name: String },
}