From 2c0468ffd16de1f835ac9c4e39692a682273fb7b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= <blaz@mxxn.io>
Date: Sat, 16 Oct 2021 18:43:07 +0900
Subject: [PATCH] fix: If backspacing past the start offset, cancel completion

Refs #822
---
 helix-term/src/ui/completion.rs | 4 ++++
 helix-term/src/ui/menu.rs       | 8 ++++++++
 2 files changed, 12 insertions(+)

diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs
index ba009c50..c75b24f1 100644
--- a/helix-term/src/ui/completion.rs
+++ b/helix-term/src/ui/completion.rs
@@ -215,6 +215,10 @@ impl Completion {
             let text = Cow::from(fragment);
             // TODO: logic is same as ui/picker
             menu.score(&text);
+        } else {
+            // we backspaced before the start offset, clear the menu
+            // this will cause the editor to remove the completion popup
+            menu.clear();
         }
     }
 
diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs
index dab0c34f..055593fd 100644
--- a/helix-term/src/ui/menu.rs
+++ b/helix-term/src/ui/menu.rs
@@ -90,6 +90,14 @@ impl<T: Item> Menu<T> {
         self.recalculate = true;
     }
 
+    pub fn clear(&mut self) {
+        self.matches.clear();
+
+        // reset cursor position
+        self.cursor = None;
+        self.scroll = 0;
+    }
+
     pub fn move_up(&mut self) {
         let len = self.matches.len();
         let pos = self.cursor.map_or(0, |i| (i + len.saturating_sub(1)) % len) % len;