diff --git a/cli/src/diff_util.rs b/cli/src/diff_util.rs index 3d1d0fe4ea..b2d1b9c604 100644 --- a/cli/src/diff_util.rs +++ b/cli/src/diff_util.rs @@ -33,7 +33,7 @@ use tracing::instrument; use crate::cli_util::{CommandError, WorkspaceCommandHelper}; use crate::formatter::Formatter; -use crate::merge_tools::{self, ExternalMergeTool}; +use crate::merge_tools::{self, ExternalMergeTool, MergeTool}; use crate::ui::Ui; #[derive(clap::Args, Clone, Debug)] @@ -121,8 +121,13 @@ fn diff_formats_from_args( .collect_vec(); if let Some(name) = &args.tool { let tool = merge_tools::get_tool_config(settings, name)? - .unwrap_or_else(|| ExternalMergeTool::with_program(name)); - formats.push(DiffFormat::Tool(Box::new(tool))); + .unwrap_or_else(|| MergeTool::External(ExternalMergeTool::with_program(name))); + match tool { + MergeTool::Internal => {} + MergeTool::External(tool) => { + formats.push(DiffFormat::Tool(Box::new(tool))); + } + } } Ok(formats) } @@ -132,8 +137,13 @@ fn default_diff_format(settings: &UserSettings) -> Result {} + MergeTool::External(tool) => { + return Ok(DiffFormat::Tool(Box::new(tool))); + } + } } let name = if let Some(name) = config.get_string("ui.diff.format").optional()? { name diff --git a/cli/src/merge_tools/mod.rs b/cli/src/merge_tools/mod.rs index 42c501b1e5..6762e3ccc3 100644 --- a/cli/src/merge_tools/mod.rs +++ b/cli/src/merge_tools/mod.rs @@ -33,6 +33,8 @@ use self::internal::{edit_diff_internal, edit_merge_internal, InternalToolError} use crate::config::CommandNameAndArgs; use crate::ui::Ui; +const BUILTIN_EDITOR_NAME: &str = ":builtin"; + #[derive(Debug, Error)] pub enum DiffEditError { #[error(transparent)] @@ -161,7 +163,11 @@ pub enum MergeTool { pub fn get_tool_config( settings: &UserSettings, name: &str, -) -> Result, ConfigError> { +) -> Result, ConfigError> { + if name == BUILTIN_EDITOR_NAME { + return Ok(Some(MergeTool::Internal)); + } + const TABLE_KEY: &str = "merge-tools"; let tools_table = settings.config().get_table(TABLE_KEY)?; if let Some(v) = tools_table.get(name) { @@ -174,7 +180,7 @@ pub fn get_tool_config( if result.program.is_empty() { result.program.clone_from(&name.to_string()); }; - Ok(Some(result)) + Ok(Some(MergeTool::External(result))) } else { Ok(None) } @@ -191,7 +197,7 @@ fn editor_args_from_settings( if let Some(args) = settings.config().get(key).optional()? { Ok(args) } else { - let default_editor = "meld"; + let default_editor = BUILTIN_EDITOR_NAME; writeln!( ui.hint(), "Using default editor '{default_editor}'; you can change this by setting {key}" @@ -206,7 +212,7 @@ fn editor_args_from_settings( pub fn get_tool_config_from_args( settings: &UserSettings, args: &CommandNameAndArgs, -) -> Result, ConfigError> { +) -> Result, ConfigError> { match args { CommandNameAndArgs::String(name) => get_tool_config(settings, name), CommandNameAndArgs::Vec(_) | CommandNameAndArgs::Structured { .. } => Ok(None), @@ -219,8 +225,8 @@ fn get_diff_editor_from_settings( ) -> Result { let args = editor_args_from_settings(ui, settings, "ui.diff-editor")?; let editor = get_tool_config_from_args(settings, &args)? - .unwrap_or_else(|| ExternalMergeTool::with_edit_args(&args)); - Ok(MergeTool::External(editor)) + .unwrap_or_else(|| MergeTool::External(ExternalMergeTool::with_edit_args(&args))); + Ok(editor) } fn get_merge_tool_from_settings( @@ -228,14 +234,15 @@ fn get_merge_tool_from_settings( settings: &UserSettings, ) -> Result { let args = editor_args_from_settings(ui, settings, "ui.merge-editor")?; - let editor = get_tool_config_from_args(settings, &args)? - .unwrap_or_else(|| ExternalMergeTool::with_merge_args(&args)); - if editor.merge_args.is_empty() { - Err(ExternalToolError::MergeArgsNotConfigured { - tool_name: args.to_string(), - }) - } else { - Ok(MergeTool::External(editor)) + let mergetool = get_tool_config_from_args(settings, &args)? + .unwrap_or_else(|| MergeTool::External(ExternalMergeTool::with_merge_args(&args))); + match mergetool { + MergeTool::External(mergetool) if mergetool.merge_args.is_empty() => { + Err(ExternalToolError::MergeArgsNotConfigured { + tool_name: args.to_string(), + }) + } + mergetool => Ok(mergetool), } } @@ -262,30 +269,7 @@ mod tests { }; // Default - insta::assert_debug_snapshot!(get("").unwrap(), @r###" - External( - ExternalMergeTool { - program: "meld", - diff_args: [ - "$left", - "$right", - ], - edit_args: [ - "$left", - "$right", - ], - merge_args: [ - "$left", - "$base", - "$right", - "-o", - "$output", - "--auto-merge", - ], - merge_tool_edits_conflict_markers: false, - }, - ) - "###); + insta::assert_debug_snapshot!(get("").unwrap(), @"Internal"); // Just program name, edit_args are filled by default insta::assert_debug_snapshot!(get(r#"ui.diff-editor = "my-diff""#).unwrap(), @r###" @@ -434,30 +418,7 @@ mod tests { }; // Default - insta::assert_debug_snapshot!(get("").unwrap(), @r###" - External( - ExternalMergeTool { - program: "meld", - diff_args: [ - "$left", - "$right", - ], - edit_args: [ - "$left", - "$right", - ], - merge_args: [ - "$left", - "$base", - "$right", - "-o", - "$output", - "--auto-merge", - ], - merge_tool_edits_conflict_markers: false, - }, - ) - "###); + insta::assert_debug_snapshot!(get("").unwrap(), @"Internal"); // Just program name insta::assert_debug_snapshot!(get(r#"ui.merge-editor = "my-merge""#).unwrap_err(), @r###"