@@ -2,7 +2,12 @@
2//! defined as a single empty or 1-wide selection range.
2//! defined as a single empty or 1-wide selection range.
4//! All positioning is done via `char` offsets into the buffer.
4//! All positioning is done via `char` offsets into the buffer.
7+ ensure_grapheme_boundary_next, ensure_grapheme_boundary_prev, next_grapheme_boundary,
5−use crate::{Assoc, ChangeSet, Rope, RopeSlice};
9+ Assoc, ChangeSet, Rope, RopeSlice,
6use smallvec::{smallvec, SmallVec};
11use smallvec::{smallvec, SmallVec};
7use std::borrow::Cow;
12use std::borrow::Cow;
@@ -132,6 +137,61 @@ impl Range {
140+ /// Compute the ends of the range, shifted (if needed) to align with
141+ /// grapheme boundaries.
143+ /// This should generally be used for cursor validation.
145+ /// Always succeeds.
147+ pub fn aligned_range(&self, slice: RopeSlice) -> (usize, usize) {
148+ if self.anchor == self.head {
149+ let pos = ensure_grapheme_boundary_prev(slice, self.anchor);
153+ ensure_grapheme_boundary_prev(slice, self.from()),
154+ ensure_grapheme_boundary_next(slice, self.to()),
159+ /// Same as `ensure_grapheme_validity()` + attempts to ensure a minimum
160+ /// char width in the direction of the head.
162+ /// This should generally be used as a pre-pass for operations that
163+ /// require a minimum selection width to achieve their intended behavior.
165+ /// This will fail at ensuring the minimum width only if the passed
166+ /// `RopeSlice` is too short in the direction of the head, in which
167+ /// case the range will fill the available length in that direction.
169+ /// Ensuring grapheme-boundary alignment always succeeds.
171+ pub fn min_width_range(&self, slice: RopeSlice, min_char_width: usize) -> (usize, usize) {
172+ if min_char_width == 0 {
173+ return self.aligned_range(slice);
176+ if self.anchor <= self.head {
177+ let anchor = ensure_grapheme_boundary_prev(slice, self.anchor);
178+ let head = ensure_grapheme_boundary_next(
181+ .max(anchor + min_char_width)
182+ .min(slice.len_chars()),
186+ let anchor = ensure_grapheme_boundary_next(slice, self.anchor);
187+ let head = ensure_grapheme_boundary_prev(
189+ self.head.min(anchor.saturating_sub(min_char_width)),
135 // groupAt
195 // groupAt
137 #[inline]
197 #[inline]
@@ -556,6 +616,54 @@ mod test {
556 assert!(Range::new(1, 1).overlaps(&Range::new(1, 1)));
616 assert!(Range::new(1, 1).overlaps(&Range::new(1, 1)));
620+ fn test_aligned_range() {
621+ let r = Rope::from_str("\r\nHi\r\n");
622+ let s = r.slice(..);
624+ assert_eq!(Range::new(0, 0).aligned_range(s), (0, 0));
625+ assert_eq!(Range::new(0, 1).aligned_range(s), (0, 2));
626+ assert_eq!(Range::new(1, 1).aligned_range(s), (0, 0));
627+ assert_eq!(Range::new(1, 2).aligned_range(s), (0, 2));
628+ assert_eq!(Range::new(2, 2).aligned_range(s), (2, 2));
629+ assert_eq!(Range::new(2, 3).aligned_range(s), (2, 3));
630+ assert_eq!(Range::new(1, 3).aligned_range(s), (0, 3));
631+ assert_eq!(Range::new(3, 5).aligned_range(s), (3, 6));
632+ assert_eq!(Range::new(4, 5).aligned_range(s), (4, 6));
633+ assert_eq!(Range::new(5, 5).aligned_range(s), (4, 4));
634+ assert_eq!(Range::new(6, 6).aligned_range(s), (6, 6));
638+ fn test_min_width_range() {
639+ let r = Rope::from_str("\r\nHi\r\n");
640+ let s = r.slice(..);
642+ assert_eq!(Range::new(0, 0).min_width_range(s, 1), (0, 2));
643+ assert_eq!(Range::new(0, 1).min_width_range(s, 1), (0, 2));
644+ assert_eq!(Range::new(1, 1).min_width_range(s, 1), (0, 2));
645+ assert_eq!(Range::new(1, 2).min_width_range(s, 1), (0, 2));
646+ assert_eq!(Range::new(2, 2).min_width_range(s, 1), (2, 3));
647+ assert_eq!(Range::new(2, 3).min_width_range(s, 1), (2, 3));
648+ assert_eq!(Range::new(1, 3).min_width_range(s, 1), (0, 3));
649+ assert_eq!(Range::new(3, 5).min_width_range(s, 1), (3, 6));
650+ assert_eq!(Range::new(4, 5).min_width_range(s, 1), (4, 6));
651+ assert_eq!(Range::new(5, 5).min_width_range(s, 1), (4, 6));
652+ assert_eq!(Range::new(6, 6).min_width_range(s, 1), (6, 6));
654+ assert_eq!(Range::new(1, 0).min_width_range(s, 1), (0, 2));
655+ assert_eq!(Range::new(2, 1).min_width_range(s, 1), (0, 2));
656+ assert_eq!(Range::new(3, 2).min_width_range(s, 1), (2, 3));
657+ assert_eq!(Range::new(3, 1).min_width_range(s, 1), (0, 3));
658+ assert_eq!(Range::new(5, 3).min_width_range(s, 1), (3, 6));
659+ assert_eq!(Range::new(5, 4).min_width_range(s, 1), (4, 6));
661+ assert_eq!(Range::new(3, 4).min_width_range(s, 3), (3, 6));
662+ assert_eq!(Range::new(4, 3).min_width_range(s, 3), (0, 4));
663+ assert_eq!(Range::new(3, 4).min_width_range(s, 20), (3, 6));
664+ assert_eq!(Range::new(4, 3).min_width_range(s, 20), (0, 4));
560 fn test_split_on_matches() {
668 fn test_split_on_matches() {
561 use crate::regex::Regex;
669 use crate::regex::Regex;
@@ -569,6 +677,9 @@ mod test {
569 assert_eq!(
677 assert_eq!(
570 result.ranges(),
678 result.ranges(),
680+ // TODO: rather than this behavior, maybe we want it
681+ // to be based on which side is the anchor?
572 // We get a leading zero-width range when there's
683 // We get a leading zero-width range when there's
573 // a leading match because ranges are inclusive on
684 // a leading match because ranges are inclusive on
574 // the left. Imagine, for example, if the entire
685 // the left. Imagine, for example, if the entire