fix(cmds): prevent :move overwriting destination;

pull/11093/head
5-pebbles 5 months ago
parent fc97ecc3e3
commit 8b86742ff9

@ -2396,28 +2396,41 @@ fn redraw(
Ok(()) Ok(())
} }
fn move_buffer( fn move_buffer_impl(
cx: &mut compositor::Context, cx: &mut compositor::Context,
args: &[Cow<str>], path: &Cow<str>,
event: PromptEvent, force: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
ensure!(args.len() == 1, format!(":move takes one argument"));
let doc = doc!(cx.editor); let doc = doc!(cx.editor);
let old_path = doc let old_path = doc
.path() .path()
.context("Scratch buffer cannot be moved. Use :write instead")? .context("Scratch buffer cannot be moved. Use `:write` instead")?
.clone(); .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()) { if let Err(err) = cx.editor.move_path(&old_path, new_path.as_ref()) {
bail!("Could not move file: {err}"); bail!("Could not move file: {err}");
} }
Ok(()) Ok(())
} }
fn move_buffer(
cx: &mut compositor::Context,
args: &[Cow<str>],
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( fn yank_diagnostic(
cx: &mut compositor::Context, cx: &mut compositor::Context,
args: &[Cow<str>], args: &[Cow<str>],

Loading…
Cancel
Save