From 8b86742ff9e86b7e06544274b3a2a5a903e31eb2 Mon Sep 17 00:00:00 2001 From: 5-pebbles <5-pebble@protonmail.com> Date: Thu, 4 Jul 2024 22:33:39 -0400 Subject: [PATCH] fix(cmds): prevent :move overwriting destination; --- helix-term/src/commands/typed.rs | 33 ++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index ab12fab10..62009b717 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -2396,28 +2396,41 @@ fn redraw( Ok(()) } -fn move_buffer( +fn move_buffer_impl( cx: &mut compositor::Context, - args: &[Cow], - event: PromptEvent, + path: &Cow, + force: bool, ) -> anyhow::Result<()> { - if event != PromptEvent::Validate { - return Ok(()); - } - - ensure!(args.len() == 1, format!(":move takes one argument")); let doc = doc!(cx.editor); let old_path = doc .path() - .context("Scratch buffer cannot be moved. Use :write instead")? + .context("Scratch buffer cannot be moved. Use `:write` instead")? .clone(); - let new_path = args.first().unwrap().to_string(); + let new_path = path.to_string(); + + if !force && Path::new(&new_path).exists() { + bail!("Destination already exists. Use `:move!` to overwrite"); + } + if let Err(err) = cx.editor.move_path(&old_path, new_path.as_ref()) { bail!("Could not move file: {err}"); } Ok(()) } +fn move_buffer( + cx: &mut compositor::Context, + args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + ensure!(args.len() == 1, format!(":move takes one argument")); + move_buffer_impl(cx, args.first().unwrap(), false) +} + fn yank_diagnostic( cx: &mut compositor::Context, args: &[Cow],