From 753f7f381b96f591be3c0e37a93b90776c5405cb Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Wed, 7 Jul 2021 17:24:39 -0700 Subject: Implement `Range::put()` which manages range movements and extensions. In particular, this wraps the annoying logic involved in keeping the cursor width to 1 grapheme. --- helix-core/src/selection.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'helix-core/src/selection.rs') diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 64ff51d8..8951899b 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -5,6 +5,7 @@ use crate::{ graphemes::{ ensure_grapheme_boundary_next, ensure_grapheme_boundary_prev, next_grapheme_boundary, + prev_grapheme_boundary, }, Assoc, ChangeSet, RopeSlice, }; @@ -208,6 +209,28 @@ impl Range { } } + /// Moves the `Range` to `char_idx`. If `extend == true`, then only the head + /// is moved to `char_idx`, and the anchor is adjusted only as needed to + /// preserve 1-width range semantics. + /// + /// This method assumes that the range and `char_idx` are already properly + /// grapheme-aligned. + #[must_use] + #[inline] + pub fn put(self, text: RopeSlice, extend: bool, char_idx: usize) -> Range { + let anchor = if !extend { + char_idx + } else if self.head >= self.anchor && char_idx < self.anchor { + next_grapheme_boundary(text, self.anchor) + } else if self.head < self.anchor && char_idx >= self.anchor { + prev_grapheme_boundary(text, self.anchor) + } else { + self.anchor + }; + + Range::new(anchor, char_idx) + } + // groupAt #[inline] -- cgit v1.2.3-70-g09d2