From 557c63033c57cb60917dd75ac45d14ca11d0557e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= <blaz@mxxn.io>
Date: Fri, 30 Jul 2021 23:40:58 +0900
Subject: [PATCH] fix: Map all selections on transaction.apply

---
 helix-view/src/document.rs | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index c02d6656..4ad08ce9 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -678,17 +678,21 @@ impl Document {
         let success = transaction.changes().apply(&mut self.text);
 
         if success {
-            // update the selection: either take the selection specified in the transaction, or map the
-            // current selection through changes.
-            let selection = transaction
-                .selection()
-                .cloned()
-                .unwrap_or_else(|| self.selection(view_id).clone().map(transaction.changes()));
-            self.selections.insert(view_id, selection);
-
-            // Ensure all selections accross all views still adhere to invariants.
             for selection in self.selections.values_mut() {
-                *selection = selection.clone().ensure_invariants(self.text.slice(..));
+                *selection = selection
+                    .clone()
+                    // Map through changes
+                    .map(transaction.changes())
+                    // Ensure all selections accross all views still adhere to invariants.
+                    .ensure_invariants(self.text.slice(..));
+            }
+
+            // if specified, the current selection should instead be replaced by transaction.selection
+            if let Some(selection) = transaction.selection() {
+                self.selections.insert(
+                    view_id,
+                    selection.clone().ensure_invariants(self.text.slice(..)),
+                );
             }
         }