Skip to content

Commit

Permalink
feat: add new command to move active buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
yo-main committed Oct 21, 2023
1 parent 31f50bf commit 8cc879a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@
| `:reset-diff-change`, `:diffget`, `:diffg` | Reset the diff change at the cursor position. |
| `:clear-register` | Clear given register. If no argument is provided, clear all registers. |
| `:redraw` | Clear and re-render the whole UI |
| `:move` | Move the current buffer and its corresponding file to a different path |
37 changes: 37 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2408,6 +2408,36 @@ fn redraw(
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"));

let new_path =
helix_core::path::get_normalized_path(&PathBuf::from(args.first().unwrap().as_ref()));
let (_, doc) = current!(cx.editor);

let old_path = doc
.path()
.ok_or_else(|| anyhow!("Scratch buffer cannot be moved. Use :write instead"))?
.clone();

doc.set_path(Some(new_path.as_path()));

if let Err(e) = std::fs::rename(&old_path, doc.path().unwrap()) {
doc.set_path(Some(old_path.as_path()));
bail!("Could not move file: {}", e);
};

Ok(())
}

pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "quit",
Expand Down Expand Up @@ -3008,6 +3038,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: redraw,
signature: CommandSignature::none(),
},
TypableCommand {
name: "move",
aliases: &[],
doc: "Move the current buffer and its corresponding file to a different path",
fun: move_buffer,
signature: CommandSignature::positional(&[completers::filename]),
},
];

pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =
Expand Down

0 comments on commit 8cc879a

Please sign in to comment.