Skip to content

Commit

Permalink
merge_tools: enable :builtin as default diff/merge editor
Browse files Browse the repository at this point in the history
  • Loading branch information
arxanas committed Aug 19, 2023
1 parent 6e3bfd3 commit 2e7334c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
20 changes: 15 additions & 5 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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)]
Expand Down Expand Up @@ -115,8 +115,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)
}
Expand All @@ -126,8 +131,13 @@ fn default_diff_format(settings: &UserSettings) -> Result<DiffFormat, config::Co
if let Some(args) = config.get("ui.diff.tool").optional()? {
// External "tool" overrides the internal "format" option.
let tool = merge_tools::get_tool_config_from_args(settings, &args)?
.unwrap_or_else(|| ExternalMergeTool::with_diff_args(&args));
return Ok(DiffFormat::Tool(Box::new(tool)));
.unwrap_or_else(|| MergeTool::External(ExternalMergeTool::with_diff_args(&args)));
match tool {
MergeTool::Internal => {}
MergeTool::External(tool) => {
return Ok(DiffFormat::Tool(Box::new(tool)));
}
}
}
let name = if let Some(name) = config.get_string("ui.diff.format").optional()? {
name
Expand Down
35 changes: 21 additions & 14 deletions cli/src/merge_tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -160,7 +162,11 @@ pub enum MergeTool {
pub fn get_tool_config(
settings: &UserSettings,
name: &str,
) -> Result<Option<ExternalMergeTool>, ConfigError> {
) -> Result<Option<MergeTool>, 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) {
Expand All @@ -173,7 +179,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)
}
Expand All @@ -190,7 +196,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}"
Expand All @@ -205,7 +211,7 @@ fn editor_args_from_settings(
pub fn get_tool_config_from_args(
settings: &UserSettings,
args: &CommandNameAndArgs,
) -> Result<Option<ExternalMergeTool>, ConfigError> {
) -> Result<Option<MergeTool>, ConfigError> {
match args {
CommandNameAndArgs::String(name) => get_tool_config(settings, name),
CommandNameAndArgs::Vec(_) | CommandNameAndArgs::Structured { .. } => Ok(None),
Expand All @@ -218,23 +224,24 @@ fn get_diff_editor_from_settings(
) -> Result<MergeTool, ExternalToolError> {
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(
ui: &Ui,
settings: &UserSettings,
) -> Result<MergeTool, ExternalToolError> {
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),
}
}

Expand Down

0 comments on commit 2e7334c

Please sign in to comment.