Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rename command. #4514

Closed
wants to merge 15 commits into from
Closed
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
| `:buffer-close-all!`, `:bca!`, `:bcloseall!` | Force close all buffers ignoring unsaved changes without quitting. |
| `:buffer-next`, `:bn`, `:bnext` | Goto next buffer. |
| `:buffer-previous`, `:bp`, `:bprev` | Goto previous buffer. |
| `:rename`, `:r` | Rename file. Accepts a file name arg (:rename new_name.txt) |
| `:write`, `:w` | Write changes to disk. Accepts an optional path (:write some/path.txt) |
| `:write!`, `:w!` | Force write changes to disk creating necessary subdirectories. Accepts an optional path (:write some/path.txt) |
| `:new`, `:n` | Create a new scratch buffer. |
Expand Down
55 changes: 54 additions & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Deref;
use std::{ffi::OsStr, ops::Deref};

use crate::job::Job;

Expand Down Expand Up @@ -322,6 +322,52 @@ fn force_write(
write_impl(cx, args.first(), true)
}

fn rename_impl(cx: &mut compositor::Context, new_name: Option<&Cow<str>>) -> anyhow::Result<()> {
grv07 marked this conversation as resolved.
Show resolved Hide resolved
let (_view, doc) = current!(cx.editor);

let path = if let Some(path) = doc.relative_path() {
path
} else {
return Err(anyhow!("File does not exist to rename."));
grv07 marked this conversation as resolved.
Show resolved Hide resolved
};

let mut new_path = path.clone();
new_path.set_file_name(OsStr::new(new_name.unwrap().as_ref()));
grv07 marked this conversation as resolved.
Show resolved Hide resolved

if new_path.parent() != path.parent() {
cx.editor.set_status("Root path must be same.");
return Ok(());
}

if new_path.exists() {
cx.editor.set_status("File already exist.");
return Ok(());
}
grv07 marked this conversation as resolved.
Show resolved Hide resolved

std::fs::rename(path, new_path.as_path())?;
doc.set_path(Some(new_path.as_path()))?;

Ok(())
}

fn rename(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

if args.len() != 1 {
anyhow::bail!("Bad arguments. Usage: `:rename new_name`");
}
grv07 marked this conversation as resolved.
Show resolved Hide resolved

rename_impl(cx, args.first())?;

Ok(())
grv07 marked this conversation as resolved.
Show resolved Hide resolved
}

fn new_file(
cx: &mut compositor::Context,
_args: &[Cow<str>],
Expand Down Expand Up @@ -1746,6 +1792,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: buffer_previous,
completer: None,
},
TypableCommand {
name: "rename",
aliases: &["r"],
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
doc: "Rename file. Accepts a file name arg (:rename new_name.txt)",
fun: rename,
completer: None,
grv07 marked this conversation as resolved.
Show resolved Hide resolved
},
TypableCommand {
name: "write",
aliases: &["w"],
Expand Down