From 92c328c088ae818338237d7f11644ba079c54648 Mon Sep 17 00:00:00 2001
From: Kitsu <mail@kitsu.me>
Date: Tue, 9 May 2023 17:21:29 -0300
Subject: [PATCH] Add wbc and wbc! commands (#6947)

---
 book/src/generated/typable-cmd.md |  4 ++-
 helix-term/src/commands/typed.rs  | 46 ++++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md
index ae28a9ba..a775c655 100644
--- a/book/src/generated/typable-cmd.md
+++ b/book/src/generated/typable-cmd.md
@@ -12,7 +12,9 @@
 | `:buffer-next`, `:bn`, `:bnext` | Goto next buffer. |
 | `:buffer-previous`, `:bp`, `:bprev` | Goto previous buffer. |
 | `: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) |
+| `:write!`, `:w!` | Force write changes to disk creating necessary subdirectories. Accepts an optional path (:write! some/path.txt) |
+| `:write-buffer-close`, `:wbc` | Write changes to disk and closes the buffer. Accepts an optional path (:write-buffer-close some/path.txt) |
+| `:write-buffer-close!`, `:wbc!` | Force write changes to disk creating necessary subdirectories and closes the buffer. Accepts an optional path (:write-buffer-close! some/path.txt) |
 | `:new`, `:n` | Create a new scratch buffer. |
 | `:format`, `:fmt` | Format the file using the LSP formatter. |
 | `:indent-style` | Set the indentation style for editing. ('t' for tabs or 1-8 for number of spaces.) |
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs
index fe92798b..16ee83d7 100644
--- a/helix-term/src/commands/typed.rs
+++ b/helix-term/src/commands/typed.rs
@@ -382,6 +382,36 @@ fn force_write(
     write_impl(cx, args.first(), true)
 }
 
+fn write_buffer_close(
+    cx: &mut compositor::Context,
+    args: &[Cow<str>],
+    event: PromptEvent,
+) -> anyhow::Result<()> {
+    if event != PromptEvent::Validate {
+        return Ok(());
+    }
+
+    write_impl(cx, args.first(), false)?;
+
+    let document_ids = buffer_gather_paths_impl(cx.editor, args);
+    buffer_close_by_ids_impl(cx, &document_ids, false)
+}
+
+fn force_write_buffer_close(
+    cx: &mut compositor::Context,
+    args: &[Cow<str>],
+    event: PromptEvent,
+) -> anyhow::Result<()> {
+    if event != PromptEvent::Validate {
+        return Ok(());
+    }
+
+    write_impl(cx, args.first(), true)?;
+
+    let document_ids = buffer_gather_paths_impl(cx.editor, args);
+    buffer_close_by_ids_impl(cx, &document_ids, false)
+}
+
 fn new_file(
     cx: &mut compositor::Context,
     _args: &[Cow<str>],
@@ -2287,10 +2317,24 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
         TypableCommand {
             name: "write!",
             aliases: &["w!"],
-            doc: "Force write changes to disk creating necessary subdirectories. Accepts an optional path (:write some/path.txt)",
+            doc: "Force write changes to disk creating necessary subdirectories. Accepts an optional path (:write! some/path.txt)",
             fun: force_write,
             signature: CommandSignature::positional(&[completers::filename]),
         },
+        TypableCommand {
+            name: "write-buffer-close",
+            aliases: &["wbc"],
+            doc: "Write changes to disk and closes the buffer. Accepts an optional path (:write-buffer-close some/path.txt)",
+            fun: write_buffer_close,
+            signature: CommandSignature::positional(&[completers::filename]),
+        },
+        TypableCommand {
+            name: "write-buffer-close!",
+            aliases: &["wbc!"],
+            doc: "Force write changes to disk creating necessary subdirectories and closes the buffer. Accepts an optional path (:write-buffer-close! some/path.txt)",
+            fun: force_write_buffer_close,
+            signature: CommandSignature::positional(&[completers::filename]),
+        },
         TypableCommand {
             name: "new",
             aliases: &["n"],