diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index 456bc2d3ff..d68bece8fd 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -2836,26 +2836,6 @@ impl LogContentFormat { } } -pub fn run_ui_editor(settings: &UserSettings, edit_path: &Path) -> Result<(), CommandError> { - let editor = TextEditor::from_settings(settings)?; - editor.edit_file(edit_path)?; - Ok(()) -} - -pub fn edit_temp_file( - error_name: &str, - tempfile_suffix: &str, - dir: &Path, - content: &str, - settings: &UserSettings, -) -> Result { - let editor = TextEditor::from_settings(settings)?.with_temp_dir(dir); - let edited = editor - .edit_str(content, Some(tempfile_suffix)) - .map_err(|err| err.with_name(error_name))?; - Ok(edited) -} - pub fn short_commit_hash(commit_id: &CommitId) -> String { format!("{commit_id:.12}") } diff --git a/cli/src/commands/commit.rs b/cli/src/commands/commit.rs index 06fa75d5a3..cce5464ec0 100644 --- a/cli/src/commands/commit.rs +++ b/cli/src/commands/commit.rs @@ -87,6 +87,7 @@ pub(crate) fn cmd_commit( let advanceable_bookmarks = workspace_command.get_advanceable_bookmarks(commit.parent_ids())?; let diff_selector = workspace_command.diff_selector(ui, args.tool.as_deref(), args.interactive)?; + let text_editor = workspace_command.text_editor()?; let mut tx = workspace_command.start_transaction(); let base_tree = commit.parent_tree(tx.repo())?; let format_instructions = || { @@ -138,11 +139,7 @@ new working-copy commit. } let temp_commit = commit_builder.write_hidden()?; let template = description_template(ui, &tx, "", &temp_commit)?; - edit_description( - tx.base_workspace_helper().repo_path(), - &template, - command.settings(), - )? + edit_description(&text_editor, &template)? }; commit_builder.set_description(description); let new_commit = commit_builder.write(tx.repo_mut())?; diff --git a/cli/src/commands/config/edit.rs b/cli/src/commands/config/edit.rs index f59b6fe373..cd36d3ee6f 100644 --- a/cli/src/commands/config/edit.rs +++ b/cli/src/commands/config/edit.rs @@ -15,7 +15,6 @@ use tracing::instrument; use super::ConfigLevelArgs; -use crate::cli_util::run_ui_editor; use crate::cli_util::CommandHelper; use crate::command_error::CommandError; use crate::ui::Ui; @@ -36,9 +35,11 @@ pub fn cmd_config_edit( command: &CommandHelper, args: &ConfigEditArgs, ) -> Result<(), CommandError> { + let editor = command.text_editor()?; let file = args.level.edit_config_file(command)?; if !file.path().exists() { file.save()?; } - run_ui_editor(command.settings(), file.path()) + editor.edit_file(file.path())?; + Ok(()) } diff --git a/cli/src/commands/describe.rs b/cli/src/commands/describe.rs index 68ebe313b9..588201c58a 100644 --- a/cli/src/commands/describe.rs +++ b/cli/src/commands/describe.rs @@ -110,6 +110,7 @@ pub(crate) fn cmd_describe( return Ok(()); } workspace_command.check_rewritable(commits.iter().ids())?; + let text_editor = workspace_command.text_editor()?; let mut tx = workspace_command.start_transaction(); let tx_description = if commits.len() == 1 { @@ -172,12 +173,7 @@ pub(crate) fn cmd_describe( if let [(_, temp_commit)] = &*temp_commits { let template = description_template(ui, &tx, "", temp_commit)?; - let description = edit_description( - tx.base_workspace_helper().repo_path(), - &template, - command.settings(), - )?; - + let description = edit_description(&text_editor, &template)?; vec![(&commits[0], description)] } else { let ParsedBulkEditMessage { @@ -185,7 +181,7 @@ pub(crate) fn cmd_describe( missing, duplicates, unexpected, - } = edit_multiple_descriptions(ui, &tx, &temp_commits, command.settings())?; + } = edit_multiple_descriptions(ui, &text_editor, &tx, &temp_commits)?; if !missing.is_empty() { return Err(user_error(format!( "The description for the following commits were not found in the edited \ diff --git a/cli/src/commands/sparse/edit.rs b/cli/src/commands/sparse/edit.rs index fe145124e1..4963721326 100644 --- a/cli/src/commands/sparse/edit.rs +++ b/cli/src/commands/sparse/edit.rs @@ -17,15 +17,14 @@ use std::path::Path; use itertools::Itertools; use jj_lib::repo_path::RepoPathBuf; -use jj_lib::settings::UserSettings; use tracing::instrument; use super::update_sparse_patterns_with; -use crate::cli_util::edit_temp_file; use crate::cli_util::CommandHelper; use crate::command_error::internal_error; use crate::command_error::user_error_with_message; use crate::command_error::CommandError; +use crate::description_util::TextEditor; use crate::ui::Ui; /// Start an editor to update the patterns that are present in the working copy @@ -39,9 +38,9 @@ pub fn cmd_sparse_edit( _args: &SparseEditArgs, ) -> Result<(), CommandError> { let mut workspace_command = command.workspace_helper(ui)?; - let repo_path = workspace_command.repo_path().to_owned(); + let editor = workspace_command.text_editor()?; update_sparse_patterns_with(ui, &mut workspace_command, |_ui, old_patterns| { - let mut new_patterns = edit_sparse(&repo_path, old_patterns, command.settings())?; + let mut new_patterns = edit_sparse(&editor, old_patterns)?; new_patterns.sort_unstable(); new_patterns.dedup(); Ok(new_patterns) @@ -49,9 +48,8 @@ pub fn cmd_sparse_edit( } fn edit_sparse( - repo_path: &Path, + editor: &TextEditor, sparse: &[RepoPathBuf], - settings: &UserSettings, ) -> Result, CommandError> { let mut content = String::new(); for sparse_path in sparse { @@ -66,13 +64,9 @@ fn edit_sparse( writeln!(&mut content, "{path_string}").unwrap(); } - let content = edit_temp_file( - "sparse patterns", - ".jjsparse", - repo_path, - &content, - settings, - )?; + let content = editor + .edit_str(content, Some(".jjsparse")) + .map_err(|err| err.with_name("sparse patterns"))?; content .lines() diff --git a/cli/src/commands/split.rs b/cli/src/commands/split.rs index 5dcd4624b0..6553d3e897 100644 --- a/cli/src/commands/split.rs +++ b/cli/src/commands/split.rs @@ -101,6 +101,7 @@ pub(crate) fn cmd_split( args.tool.as_deref(), args.interactive || args.paths.is_empty(), )?; + let text_editor = workspace_command.text_editor()?; let mut tx = workspace_command.start_transaction(); let end_tree = commit.tree()?; let base_tree = commit.parent_tree(tx.repo())?; @@ -151,11 +152,7 @@ The remainder will be in the second commit. "Enter a description for the first commit.", &temp_commit, )?; - let description = edit_description( - tx.base_workspace_helper().repo_path(), - &template, - command.settings(), - )?; + let description = edit_description(&text_editor, &template)?; commit_builder.set_description(description); commit_builder.write(tx.repo_mut())? }; @@ -195,11 +192,7 @@ The remainder will be in the second commit. "Enter a description for the second commit.", &temp_commit, )?; - edit_description( - tx.base_workspace_helper().repo_path(), - &template, - command.settings(), - )? + edit_description(&text_editor, &template)? }; commit_builder.set_description(description); commit_builder.write(tx.repo_mut())? diff --git a/cli/src/commands/squash.rs b/cli/src/commands/squash.rs index 2a37458f77..2cbbd7c9cf 100644 --- a/cli/src/commands/squash.rs +++ b/cli/src/commands/squash.rs @@ -157,6 +157,7 @@ pub(crate) fn cmd_squash( .to_matcher(); let diff_selector = workspace_command.diff_selector(ui, args.tool.as_deref(), args.interactive)?; + let text_editor = workspace_command.text_editor()?; let description = SquashedDescription::from_args(args); workspace_command .check_rewritable(sources.iter().chain(std::iter::once(&destination)).ids())?; @@ -164,7 +165,6 @@ pub(crate) fn cmd_squash( let mut tx = workspace_command.start_transaction(); let tx_description = format!("squash commits into {}", destination.id().hex()); let source_commits = select_diff(&tx, &sources, &destination, &matcher, &diff_selector)?; - let repo_path = tx.base_workspace_helper().repo_path().to_owned(); match rewrite::squash_commits( tx.repo_mut(), &source_commits, @@ -175,12 +175,7 @@ pub(crate) fn cmd_squash( SquashedDescription::UseDestination => Ok(destination.description().to_owned()), SquashedDescription::Combine => { let abandoned_commits = abandoned_commits.iter().map(|c| &c.commit).collect_vec(); - combine_messages( - &repo_path, - &abandoned_commits, - &destination, - command.settings(), - ) + combine_messages(&text_editor, &abandoned_commits, &destination) } }, )? { diff --git a/cli/src/commands/unsquash.rs b/cli/src/commands/unsquash.rs index 22a893340a..8141d2c342 100644 --- a/cli/src/commands/unsquash.rs +++ b/cli/src/commands/unsquash.rs @@ -78,6 +78,7 @@ pub(crate) fn cmd_unsquash( } else { None }; + let text_editor = workspace_command.text_editor()?; let mut tx = workspace_command.start_transaction(); let parent_base_tree = parent.parent_tree(tx.repo())?; let new_parent_tree_id; @@ -116,12 +117,7 @@ aborted. // case). if new_parent_tree_id == parent_base_tree.id() { tx.repo_mut().record_abandoned_commit(parent.id().clone()); - let description = combine_messages( - tx.base_workspace_helper().repo_path(), - &[&parent], - &commit, - command.settings(), - )?; + let description = combine_messages(&text_editor, &[&parent], &commit)?; // Commit the new child on top of the parent's parents. tx.repo_mut() .rewrite_commit(&commit) diff --git a/cli/src/description_util.rs b/cli/src/description_util.rs index 74944dcb23..c0c1478490 100644 --- a/cli/src/description_util.rs +++ b/cli/src/description_util.rs @@ -19,7 +19,6 @@ use jj_lib::file_util::PathError; use jj_lib::settings::UserSettings; use thiserror::Error; -use crate::cli_util::edit_temp_file; use crate::cli_util::short_commit_hash; use crate::cli_util::WorkspaceCommandTransaction; use crate::command_error::CommandError; @@ -155,24 +154,16 @@ where text_util::complete_newline(description.trim_matches('\n')) } -pub fn edit_description( - repo_path: &Path, - description: &str, - settings: &UserSettings, -) -> Result { +pub fn edit_description(editor: &TextEditor, description: &str) -> Result { let description = format!( r#"{description} JJ: Lines starting with "JJ:" (like this one) will be removed. "# ); - let description = edit_temp_file( - "description", - ".jjdescription", - repo_path, - &description, - settings, - )?; + let description = editor + .edit_str(description, Some(".jjdescription")) + .map_err(|err| err.with_name("description"))?; Ok(cleanup_description_lines(description.lines())) } @@ -180,9 +171,9 @@ JJ: Lines starting with "JJ:" (like this one) will be removed. /// Edits the descriptions of the given commits in a single editor session. pub fn edit_multiple_descriptions( ui: &Ui, + editor: &TextEditor, tx: &WorkspaceCommandTransaction, commits: &[(&CommitId, Commit)], - settings: &UserSettings, ) -> Result, CommandError> { let mut commits_map = IndexMap::new(); let mut bulk_message = String::new(); @@ -206,13 +197,9 @@ pub fn edit_multiple_descriptions( } bulk_message.push_str("JJ: Lines starting with \"JJ: \" (like this one) will be removed.\n"); - let bulk_message = edit_temp_file( - "description", - ".jjdescription", - tx.base_workspace_helper().repo_path(), - &bulk_message, - settings, - )?; + let bulk_message = editor + .edit_str(bulk_message, Some(".jjdescription")) + .map_err(|err| err.with_name("description"))?; Ok(parse_bulk_edit_message(&bulk_message, &commits_map)?) } @@ -300,10 +287,9 @@ where /// then that one is used. Otherwise we concatenate the messages and ask the /// user to edit the result in their editor. pub fn combine_messages( - repo_path: &Path, + editor: &TextEditor, sources: &[&Commit], destination: &Commit, - settings: &UserSettings, ) -> Result { let non_empty = sources .iter() @@ -330,7 +316,7 @@ pub fn combine_messages( combined.push_str("\nJJ: Description from source commit:\n"); combined.push_str(commit.description()); } - edit_description(repo_path, &combined, settings) + edit_description(editor, &combined) } /// Create a description from a list of paragraphs.