From 6cbc0aea926248f029ab3868d5ed9b8d5a4f207f Mon Sep 17 00:00:00 2001
From: Jakub Bartodziej <jqb@google.com>
Date: Thu, 3 Jun 2021 08:51:13 -0400
Subject: [PATCH] Disable deleting from an empty buffer which can cause a
 crash.

---
 helix-term/src/commands.rs | 4 ++++
 helix-view/src/document.rs | 4 ++++
 2 files changed, 8 insertions(+)

diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 7b30168a..8d3e31e2 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -745,6 +745,10 @@ pub fn extend_line(cx: &mut Context) {
 // heuristic: append changes to history after each command, unless we're in insert mode
 
 fn _delete_selection(doc: &mut Document, view_id: ViewId) {
+    if doc.empty() {
+        return;
+    }
+
     // first yank the selection
     let values: Vec<String> = doc
         .selection(view_id)
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index 3a3b9390..c41b78ad 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -494,6 +494,10 @@ impl Document {
     pub fn versioned_identifier(&self) -> lsp::VersionedTextDocumentIdentifier {
         lsp::VersionedTextDocumentIdentifier::new(self.url().unwrap(), self.version)
     }
+
+    pub fn empty(&self) -> bool {
+        self.text == "\n"
+    }
 }
 
 #[cfg(test)]