diff options
author | Michael Davis | 2022-10-10 19:54:57 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2022-10-11 03:17:32 +0000 |
commit | d418f0795d8a33c83ad87c5530c4819d83da09ef (patch) | |
tree | 090e90ed387f97f4866a8ffaabf3cbee46fb3c3a /helix-view/src | |
parent | a85e3862988933953d1c0e7435969604d3722959 (diff) |
Add View::apply for adjusting jumplist ranges
Applying a transaction to a View adjusts the ranges in the jumplist
to ensure that they remain within the text of the document and follow
regular selection invariants (for example, must be have a width of at
least one).
Diffstat (limited to 'helix-view/src')
-rw-r--r-- | helix-view/src/view.rs | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index 3df533df..62984b88 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -3,7 +3,9 @@ use crate::{ gutter::{self, Gutter}, Document, DocumentId, ViewId, }; -use helix_core::{pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection}; +use helix_core::{ + pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection, Transaction, +}; use std::fmt; @@ -62,6 +64,22 @@ impl JumpList { pub fn get(&self) -> &[Jump] { &self.jumps } + + /// Applies a [`Transaction`] of changes to the jumplist. + /// This is necessary to ensure that changes to documents do not leave jump-list + /// selections pointing to parts of the text which no longer exist. + fn apply(&mut self, transaction: &Transaction, doc: &Document) { + let text = doc.text().slice(..); + + for (doc_id, selection) in &mut self.jumps { + if doc.id() == *doc_id { + *selection = selection + .clone() + .map(transaction.changes()) + .ensure_invariants(text); + } + } + } } #[derive(Clone)] @@ -334,6 +352,14 @@ impl View { // (None, None) => return, // } // } + + /// Applies a [`Transaction`] to the view. + /// Instead of calling this function directly, use [crate::apply_transaction] + /// which applies a transaction to the [`Document`] and view together. + pub fn apply(&mut self, transaction: &Transaction, doc: &Document) -> bool { + self.jumps.apply(transaction, doc); + true + } } #[cfg(test)] |