From 2a48779e6de5191377225080288b46d876ac8709 Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sat, 29 Oct 2022 15:25:37 +0530 Subject: [PATCH 01/11] add rename file command --- helix-term/src/commands/typed.rs | 55 +++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 0cf75ada91e7..313f905b8bf6 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1,4 +1,4 @@ -use std::ops::Deref; +use std::{ffi::OsStr, ops::Deref}; use crate::job::Job; @@ -322,6 +322,52 @@ fn force_write( write_impl(cx, args.first(), true) } +fn rename_impl(cx: &mut compositor::Context, new_name: Option<&Cow>) -> anyhow::Result<()> { + 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.")); + }; + + let mut new_path = path.clone(); + new_path.set_file_name(OsStr::new(new_name.unwrap().as_ref())); + + 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(()); + } + + 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], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + if args.len() != 1 { + anyhow::bail!("Bad arguments. Usage: `:rename new_name`"); + } + + rename_impl(cx, args.first())?; + + Ok(()) +} + fn new_file( cx: &mut compositor::Context, _args: &[Cow], @@ -1746,6 +1792,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: buffer_previous, completer: None, }, + TypableCommand { + name: "rename", + aliases: &["r"], + doc: "Rename file. Accepts a file name arg (:rename new_name.txt)", + fun: rename, + completer: None, + }, TypableCommand { name: "write", aliases: &["w"], From bdba643612358847842f0ea59f528ff7eeb1f52d Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sat, 29 Oct 2022 15:34:55 +0530 Subject: [PATCH 02/11] add docs --- book/src/generated/typable-cmd.md | 1 + 1 file changed, 1 insertion(+) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index f858ba7255df..326331b48148 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -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. | From c9e2f1c66cbb809befbab013d42cb856b2bc0fb9 Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sat, 29 Oct 2022 23:17:20 +0530 Subject: [PATCH 03/11] Update helix-term/src/commands/typed.rs Co-authored-by: Michael Davis --- helix-term/src/commands/typed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 313f905b8bf6..3aa437cb1951 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -322,7 +322,7 @@ fn force_write( write_impl(cx, args.first(), true) } -fn rename_impl(cx: &mut compositor::Context, new_name: Option<&Cow>) -> anyhow::Result<()> { +fn rename_impl(cx: &mut compositor::Context, new_name: Cow) -> anyhow::Result<()> { let (_view, doc) = current!(cx.editor); let path = if let Some(path) = doc.relative_path() { From b86f380b63f54e4bf9cc413876be96b3a14cb1e1 Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sat, 29 Oct 2022 23:17:30 +0530 Subject: [PATCH 04/11] Update helix-term/src/commands/typed.rs Co-authored-by: Michael Davis --- helix-term/src/commands/typed.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 3aa437cb1951..d06263ffb60e 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -359,9 +359,7 @@ fn rename( return Ok(()); } - if args.len() != 1 { - anyhow::bail!("Bad arguments. Usage: `:rename new_name`"); - } + ensure!(args.len() == 1, "Bad arguments. Usage: `:rename new_name`"); rename_impl(cx, args.first())?; From d3c1a4b446e254271bafc9554d3a1f1dbba73b6c Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sat, 29 Oct 2022 23:17:47 +0530 Subject: [PATCH 05/11] Update helix-term/src/commands/typed.rs Co-authored-by: Michael Davis --- helix-term/src/commands/typed.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index d06263ffb60e..131b5fea8858 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -361,9 +361,7 @@ fn rename( ensure!(args.len() == 1, "Bad arguments. Usage: `:rename new_name`"); - rename_impl(cx, args.first())?; - - Ok(()) + rename_impl(cx, *args.first().unwrap()) } fn new_file( From 1e73ef9f7dfcc8c5245c92b2654352439eb40dcf Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sat, 29 Oct 2022 23:17:57 +0530 Subject: [PATCH 06/11] Update helix-term/src/commands/typed.rs Co-authored-by: Michael Davis --- helix-term/src/commands/typed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 131b5fea8858..cb713fcbb936 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -328,7 +328,7 @@ fn rename_impl(cx: &mut compositor::Context, new_name: Cow) -> anyhow::Resu let path = if let Some(path) = doc.relative_path() { path } else { - return Err(anyhow!("File does not exist to rename.")); + bail!("File must exist to rename."); }; let mut new_path = path.clone(); From d3dd62a934bf06664113e91a1a2a9cf49fbf0bdb Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sat, 29 Oct 2022 23:18:05 +0530 Subject: [PATCH 07/11] Update helix-term/src/commands/typed.rs Co-authored-by: Michael Davis --- helix-term/src/commands/typed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index cb713fcbb936..ea3f548c1fb7 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -332,7 +332,7 @@ fn rename_impl(cx: &mut compositor::Context, new_name: Cow) -> anyhow::Resu }; let mut new_path = path.clone(); - new_path.set_file_name(OsStr::new(new_name.unwrap().as_ref())); + new_path.set_file_name(OsStr::new(new_name.as_ref())); if new_path.parent() != path.parent() { cx.editor.set_status("Root path must be same."); From 1fcccea4c1f807d8ddaf49c12e66958beeb4bab2 Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sat, 29 Oct 2022 23:18:20 +0530 Subject: [PATCH 08/11] Update helix-term/src/commands/typed.rs Co-authored-by: Michael Davis --- helix-term/src/commands/typed.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index ea3f548c1fb7..118a23742c25 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -334,15 +334,9 @@ fn rename_impl(cx: &mut compositor::Context, new_name: Cow) -> anyhow::Resu let mut new_path = path.clone(); new_path.set_file_name(OsStr::new(new_name.as_ref())); - if new_path.parent() != path.parent() { - cx.editor.set_status("Root path must be same."); - return Ok(()); - } + ensure!(new_path.parent() == path.parent(), "Root path must be the same."); - if new_path.exists() { - cx.editor.set_status("File already exist."); - return Ok(()); - } + ensure!(!new_path.exists(), "File already exists."); std::fs::rename(path, new_path.as_path())?; doc.set_path(Some(new_path.as_path()))?; From f3cd40729893f2953b1051cf2f657fd69ca48893 Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sat, 29 Oct 2022 23:18:37 +0530 Subject: [PATCH 09/11] Update helix-term/src/commands/typed.rs Co-authored-by: Michael Davis --- helix-term/src/commands/typed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 118a23742c25..1d40776bb5f4 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1787,7 +1787,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ aliases: &["r"], doc: "Rename file. Accepts a file name arg (:rename new_name.txt)", fun: rename, - completer: None, + completer: Some(completers::filename), }, TypableCommand { name: "write", From 735e600553f696ee16a72c7c9300b3b8bee994da Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sat, 29 Oct 2022 23:31:36 +0530 Subject: [PATCH 10/11] resolve commit suggestions build issue --- helix-term/src/commands/typed.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 1d40776bb5f4..49164bce3bd9 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -322,7 +322,7 @@ fn force_write( write_impl(cx, args.first(), true) } -fn rename_impl(cx: &mut compositor::Context, new_name: Cow) -> anyhow::Result<()> { +fn rename_impl(cx: &mut compositor::Context, new_name: &str) -> anyhow::Result<()> { let (_view, doc) = current!(cx.editor); let path = if let Some(path) = doc.relative_path() { @@ -332,9 +332,12 @@ fn rename_impl(cx: &mut compositor::Context, new_name: Cow) -> anyhow::Resu }; let mut new_path = path.clone(); - new_path.set_file_name(OsStr::new(new_name.as_ref())); + new_path.set_file_name(OsStr::new(new_name)); - ensure!(new_path.parent() == path.parent(), "Root path must be the same."); + ensure!( + new_path.parent() == path.parent(), + "Root path must be the same." + ); ensure!(!new_path.exists(), "File already exists."); @@ -355,7 +358,7 @@ fn rename( ensure!(args.len() == 1, "Bad arguments. Usage: `:rename new_name`"); - rename_impl(cx, *args.first().unwrap()) + rename_impl(cx, args.first().unwrap()) } fn new_file( From 9c711c8ffd889ea44d35b03da8e8af995660a7ee Mon Sep 17 00:00:00 2001 From: Gaurav Tyagi Date: Sat, 29 Oct 2022 23:34:50 +0530 Subject: [PATCH 11/11] update shortcut :r to :rnm --- book/src/generated/typable-cmd.md | 2 +- helix-term/src/commands/typed.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index 326331b48148..cce854543f6c 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -11,7 +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) | +| `:rename`, `:rnm` | 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. | diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 49164bce3bd9..12ffe1d865ab 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1787,7 +1787,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ }, TypableCommand { name: "rename", - aliases: &["r"], + aliases: &["rnm"], doc: "Rename file. Accepts a file name arg (:rename new_name.txt)", fun: rename, completer: Some(completers::filename),