@@ -2,6 +2,7 @@
2//! single selection range.
2//! single selection range.
4//! All positioning is done via `char` offsets into the buffer.
4//! All positioning is done via `char` offsets into the buffer.
5+use crate::{Assoc, ChangeSet};
5use smallvec::{smallvec, SmallVec};
6use smallvec::{smallvec, SmallVec};
@@ -59,7 +60,18 @@ impl Range {
62− // TODO: map
63+ /// Map a range through a set of changes. Returns a new range representing the same position
64+ /// after the changes are applied.
65+ pub fn map(self, changes: &ChangeSet) -> Self {
66+ let anchor = changes.map_pos(self.anchor, Assoc::Before);
67+ let head = changes.map_pos(self.head, Assoc::Before);
69+ // TODO: possibly unnecessary
70+ if self.anchor == anchor && self.head == head {
73+ Self { anchor, head }
64 /// Extend the range to cover at least `from` `to`.
76 /// Extend the range to cover at least `from` `to`.
65 #[must_use]
77 #[must_use]
@@ -115,6 +127,22 @@ impl Selection {
115 // add_range // push
127 // add_range // push
116 // replace_range
128 // replace_range
130+ /// Map selections over a set of changes. Useful for adjusting the selection position after
131+ /// applying changes to a document.
132+ pub fn map(self, changes: &ChangeSet) -> Self {
133+ if changes.is_empty() {
140+ .map(|range| range.map(changes))
118 #[must_use]
146 #[must_use]
119 /// Constructs a selection holding a single range.
147 /// Constructs a selection holding a single range.
120 pub fn single(anchor: usize, head: usize) -> Self {
148 pub fn single(anchor: usize, head: usize) -> Self {