2021-06-25 12:40:23 +04:00
|
|
|
//! Selections are the primary editing construct. Even a single cursor is
|
|
|
|
//! defined as a single empty or 1-wide selection range.
|
2020-05-25 08:02:21 +04:00
|
|
|
//!
|
|
|
|
//! All positioning is done via `char` offsets into the buffer.
|
2021-06-27 02:37:32 +04:00
|
|
|
use crate::{
|
|
|
|
graphemes::{
|
|
|
|
ensure_grapheme_boundary_next, ensure_grapheme_boundary_prev, next_grapheme_boundary,
|
2021-07-08 04:24:39 +04:00
|
|
|
prev_grapheme_boundary,
|
2021-06-27 02:37:32 +04:00
|
|
|
},
|
2021-07-02 06:37:28 +04:00
|
|
|
Assoc, ChangeSet, RopeSlice,
|
2021-06-27 02:37:32 +04:00
|
|
|
};
|
2020-05-25 08:02:21 +04:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2020-09-28 20:01:27 +04:00
|
|
|
use std::borrow::Cow;
|
2020-05-25 08:02:21 +04:00
|
|
|
|
2021-06-25 12:40:23 +04:00
|
|
|
/// A single selection range.
|
|
|
|
///
|
|
|
|
/// The range consists of an "anchor" and "head" position in
|
|
|
|
/// the text. The head is the part that the user moves when
|
|
|
|
/// directly extending the selection. The head and anchor
|
|
|
|
/// can be in any order: either can precede or follow the
|
|
|
|
/// other in the text, and they can share the same position
|
|
|
|
/// for a zero-width range.
|
|
|
|
///
|
|
|
|
/// Below are some example `Range` configurations to better
|
|
|
|
/// illustrate. The anchor and head indices are show as
|
|
|
|
/// "(anchor, head)", followed by example text with "[" and "]"
|
|
|
|
/// inserted to visually represent the anchor and head positions:
|
|
|
|
///
|
|
|
|
/// - (0, 3): [Som]e text.
|
|
|
|
/// - (3, 0): ]Som[e text.
|
|
|
|
/// - (2, 7): So[me te]xt.
|
|
|
|
/// - (1, 1): S[]ome text.
|
|
|
|
///
|
|
|
|
/// Ranges are considered to be inclusive on the left and
|
|
|
|
/// exclusive on the right, regardless of anchor-head ordering.
|
|
|
|
/// This means, for example, that non-zero-width ranges that
|
|
|
|
/// are directly adjecent, sharing an edge, do not overlap.
|
|
|
|
/// However, a zero-width range will overlap with the shared
|
|
|
|
/// left-edge of another range.
|
2020-05-25 08:02:21 +04:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub struct Range {
|
|
|
|
/// The anchor of the range: the side that doesn't move when extending.
|
|
|
|
pub anchor: usize,
|
|
|
|
/// The head of the range, moved when extending.
|
|
|
|
pub head: usize,
|
2021-02-12 11:49:24 +04:00
|
|
|
pub horiz: Option<u32>,
|
2021-06-25 12:40:23 +04:00
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
|
|
|
|
impl Range {
|
|
|
|
pub fn new(anchor: usize, head: usize) -> Self {
|
2021-02-12 11:49:24 +04:00
|
|
|
Self {
|
|
|
|
anchor,
|
|
|
|
head,
|
|
|
|
horiz: None,
|
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
|
2021-06-11 16:57:07 +04:00
|
|
|
pub fn point(head: usize) -> Self {
|
|
|
|
Self::new(head, head)
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
/// Start of the range.
|
|
|
|
#[inline]
|
2020-05-28 09:45:44 +04:00
|
|
|
#[must_use]
|
2020-05-25 08:02:21 +04:00
|
|
|
pub fn from(&self) -> usize {
|
|
|
|
std::cmp::min(self.anchor, self.head)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// End of the range.
|
|
|
|
#[inline]
|
2020-05-28 09:45:44 +04:00
|
|
|
#[must_use]
|
2020-05-25 08:02:21 +04:00
|
|
|
pub fn to(&self) -> usize {
|
|
|
|
std::cmp::max(self.anchor, self.head)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `true` when head and anchor are at the same position.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.anchor == self.head
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check two ranges for overlap.
|
2020-05-28 09:45:44 +04:00
|
|
|
#[must_use]
|
2020-05-25 08:02:21 +04:00
|
|
|
pub fn overlaps(&self, other: &Self) -> bool {
|
2021-06-25 12:40:23 +04:00
|
|
|
// To my eye, it's non-obvious why this works, but I arrived
|
|
|
|
// at it after transforming the slower version that explicitly
|
|
|
|
// enumerated more cases. The unit tests are thorough.
|
|
|
|
self.from() == other.from() || (self.to() > other.from() && other.to() > self.from())
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
|
2020-09-28 20:00:35 +04:00
|
|
|
pub fn contains(&self, pos: usize) -> bool {
|
2021-06-25 12:40:23 +04:00
|
|
|
self.from() <= pos && pos < self.to()
|
2020-09-28 20:00:35 +04:00
|
|
|
}
|
|
|
|
|
2020-05-28 09:59:50 +04:00
|
|
|
/// Map a range through a set of changes. Returns a new range representing the same position
|
|
|
|
/// after the changes are applied.
|
|
|
|
pub fn map(self, changes: &ChangeSet) -> Self {
|
2020-09-05 17:01:05 +04:00
|
|
|
let anchor = changes.map_pos(self.anchor, Assoc::After);
|
|
|
|
let head = changes.map_pos(self.head, Assoc::After);
|
2020-05-28 09:59:50 +04:00
|
|
|
|
2021-06-25 12:40:23 +04:00
|
|
|
// We want to return a new `Range` with `horiz == None` every time,
|
|
|
|
// even if the anchor and head haven't changed, because we don't
|
|
|
|
// know if the *visual* position hasn't changed due to
|
|
|
|
// character-width or grapheme changes earlier in the text.
|
2021-02-12 11:49:24 +04:00
|
|
|
Self {
|
|
|
|
anchor,
|
|
|
|
head,
|
|
|
|
horiz: None,
|
|
|
|
}
|
2020-05-28 09:59:50 +04:00
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
|
|
|
|
/// Extend the range to cover at least `from` `to`.
|
2020-05-28 09:45:44 +04:00
|
|
|
#[must_use]
|
2020-05-25 08:02:21 +04:00
|
|
|
pub fn extend(&self, from: usize, to: usize) -> Self {
|
2021-06-25 12:40:23 +04:00
|
|
|
debug_assert!(from <= to);
|
2020-05-25 08:02:21 +04:00
|
|
|
|
2021-06-25 12:40:23 +04:00
|
|
|
if self.anchor <= self.head {
|
|
|
|
Self {
|
|
|
|
anchor: self.anchor.min(from),
|
|
|
|
head: self.head.max(to),
|
|
|
|
horiz: None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Self {
|
|
|
|
anchor: self.anchor.max(to),
|
|
|
|
head: self.head.min(from),
|
|
|
|
horiz: None,
|
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 22:40:07 +04:00
|
|
|
/// Returns a range that encompasses both input ranges.
|
|
|
|
///
|
|
|
|
/// This is like `extend()`, but tries to negotiate the
|
|
|
|
/// anchor/head ordering between the two input ranges.
|
|
|
|
#[must_use]
|
|
|
|
pub fn merge(&self, other: Self) -> Self {
|
|
|
|
if self.anchor > self.head && other.anchor > other.head {
|
|
|
|
Range {
|
|
|
|
anchor: self.anchor.max(other.anchor),
|
|
|
|
head: self.head.min(other.head),
|
|
|
|
horiz: None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Range {
|
|
|
|
anchor: self.from().min(other.from()),
|
|
|
|
head: self.to().max(other.to()),
|
|
|
|
horiz: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 18:51:47 +04:00
|
|
|
/// Compute a possibly new range from this range, attempting to ensure
|
|
|
|
/// a minimum range width of 1 char by shifting the head in the forward
|
|
|
|
/// direction as needed.
|
2021-06-27 02:37:32 +04:00
|
|
|
///
|
2021-06-28 18:51:47 +04:00
|
|
|
/// This method will never shift the anchor, and will only shift the
|
|
|
|
/// head in the forward direction. Therefore, this method can fail
|
|
|
|
/// at ensuring the minimum width if and only if the passed range is
|
|
|
|
/// both zero-width and at the end of the `RopeSlice`.
|
2021-06-27 02:37:32 +04:00
|
|
|
///
|
2021-06-28 18:51:47 +04:00
|
|
|
/// If the input range is grapheme-boundary aligned, the returned range
|
|
|
|
/// will also be. Specifically, if the head needs to shift to achieve
|
|
|
|
/// the minimum width, it will shift to the next grapheme boundary.
|
2021-06-27 02:37:32 +04:00
|
|
|
#[must_use]
|
2021-06-28 18:51:47 +04:00
|
|
|
#[inline]
|
|
|
|
pub fn min_width_1(&self, slice: RopeSlice) -> Self {
|
2021-06-27 02:37:32 +04:00
|
|
|
if self.anchor == self.head {
|
2021-06-28 18:51:47 +04:00
|
|
|
Range {
|
|
|
|
anchor: self.anchor,
|
|
|
|
head: next_grapheme_boundary(slice, self.head),
|
|
|
|
horiz: self.horiz,
|
|
|
|
}
|
2021-06-27 02:37:32 +04:00
|
|
|
} else {
|
2021-06-28 18:51:47 +04:00
|
|
|
*self
|
2021-06-27 02:37:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 18:51:47 +04:00
|
|
|
/// Compute a possibly new range from this range, with its ends
|
|
|
|
/// shifted as needed to align with grapheme boundaries.
|
2021-06-27 02:37:32 +04:00
|
|
|
///
|
2021-06-28 18:51:47 +04:00
|
|
|
/// Zero-width ranges will always stay zero-width, and non-zero-width
|
|
|
|
/// ranges will never collapse to zero-width.
|
2021-06-27 02:37:32 +04:00
|
|
|
#[must_use]
|
2021-06-28 18:51:47 +04:00
|
|
|
pub fn grapheme_aligned(&self, slice: RopeSlice) -> Self {
|
2021-06-28 22:40:07 +04:00
|
|
|
use std::cmp::Ordering;
|
|
|
|
let (new_anchor, new_head) = match self.anchor.cmp(&self.head) {
|
|
|
|
Ordering::Equal => {
|
|
|
|
let pos = ensure_grapheme_boundary_prev(slice, self.anchor);
|
|
|
|
(pos, pos)
|
|
|
|
}
|
|
|
|
Ordering::Less => (
|
2021-06-28 18:51:47 +04:00
|
|
|
ensure_grapheme_boundary_prev(slice, self.anchor),
|
|
|
|
ensure_grapheme_boundary_next(slice, self.head),
|
2021-06-28 22:40:07 +04:00
|
|
|
),
|
|
|
|
Ordering::Greater => (
|
2021-06-28 18:51:47 +04:00
|
|
|
ensure_grapheme_boundary_next(slice, self.anchor),
|
|
|
|
ensure_grapheme_boundary_prev(slice, self.head),
|
2021-06-28 22:40:07 +04:00
|
|
|
),
|
2021-06-28 18:51:47 +04:00
|
|
|
};
|
|
|
|
Range {
|
|
|
|
anchor: new_anchor,
|
|
|
|
head: new_head,
|
|
|
|
horiz: if new_anchor == self.anchor {
|
|
|
|
self.horiz
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
2021-06-27 02:37:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 04:24:39 +04:00
|
|
|
/// 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]
|
2021-07-09 03:45:19 +04:00
|
|
|
pub fn put(self, text: RopeSlice, char_idx: usize, extend: bool) -> Range {
|
2021-07-08 04:24:39 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
// groupAt
|
2020-09-28 20:01:27 +04:00
|
|
|
|
|
|
|
#[inline]
|
2021-02-22 10:14:02 +04:00
|
|
|
pub fn fragment<'a, 'b: 'a>(&'a self, text: RopeSlice<'b>) -> Cow<'b, str> {
|
2021-06-25 12:40:23 +04:00
|
|
|
text.slice(self.from()..self.to()).into()
|
2020-09-28 20:01:27 +04:00
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
|
2021-07-03 05:07:49 +04:00
|
|
|
impl From<(usize, usize)> for Range {
|
|
|
|
fn from(tuple: (usize, usize)) -> Self {
|
|
|
|
Self {
|
|
|
|
anchor: tuple.0,
|
|
|
|
head: tuple.1,
|
|
|
|
horiz: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
/// A selection consists of one or more selection ranges.
|
2021-03-22 07:21:33 +04:00
|
|
|
/// invariant: A selection can never be empty (always contains at least primary range).
|
2021-03-29 11:32:42 +04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2020-05-25 08:02:21 +04:00
|
|
|
pub struct Selection {
|
2020-09-13 18:38:54 +04:00
|
|
|
ranges: SmallVec<[Range; 1]>,
|
|
|
|
primary_index: usize,
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
|
2021-03-22 07:26:04 +04:00
|
|
|
#[allow(clippy::len_without_is_empty)] // a Selection is never empty
|
2020-05-25 08:02:21 +04:00
|
|
|
impl Selection {
|
|
|
|
// eq
|
2020-05-28 09:45:44 +04:00
|
|
|
|
|
|
|
#[must_use]
|
2020-05-25 08:02:21 +04:00
|
|
|
pub fn primary(&self) -> Range {
|
|
|
|
self.ranges[self.primary_index]
|
|
|
|
}
|
|
|
|
|
2020-09-07 12:08:28 +04:00
|
|
|
#[must_use]
|
|
|
|
pub fn cursor(&self) -> usize {
|
|
|
|
self.primary().head
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
/// Ensure selection containing only the primary selection.
|
2020-05-28 09:45:44 +04:00
|
|
|
pub fn into_single(self) -> Self {
|
2020-05-25 08:02:21 +04:00
|
|
|
if self.ranges.len() == 1 {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
Self {
|
2020-05-26 13:11:11 +04:00
|
|
|
ranges: smallvec![self.ranges[self.primary_index]],
|
2020-05-25 08:02:21 +04:00
|
|
|
primary_index: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 07:01:49 +04:00
|
|
|
pub fn push(mut self, range: Range) -> Self {
|
2021-04-09 19:21:13 +04:00
|
|
|
self.ranges.push(range);
|
2021-06-30 18:45:15 +04:00
|
|
|
self.normalize()
|
2021-04-09 19:21:13 +04:00
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
// replace_range
|
|
|
|
|
2020-05-28 09:59:50 +04:00
|
|
|
/// Map selections over a set of changes. Useful for adjusting the selection position after
|
|
|
|
/// applying changes to a document.
|
|
|
|
pub fn map(self, changes: &ChangeSet) -> Self {
|
|
|
|
if changes.is_empty() {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
Self::new(
|
|
|
|
self.ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|range| range.map(changes))
|
|
|
|
.collect(),
|
|
|
|
self.primary_index,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-07 12:08:28 +04:00
|
|
|
pub fn ranges(&self) -> &[Range] {
|
|
|
|
&self.ranges
|
|
|
|
}
|
|
|
|
|
2021-03-22 07:18:48 +04:00
|
|
|
pub fn primary_index(&self) -> usize {
|
|
|
|
self.primary_index
|
|
|
|
}
|
|
|
|
|
2020-05-28 09:45:44 +04:00
|
|
|
#[must_use]
|
2020-05-25 08:02:21 +04:00
|
|
|
/// Constructs a selection holding a single range.
|
|
|
|
pub fn single(anchor: usize, head: usize) -> Self {
|
|
|
|
Self {
|
2021-02-12 11:49:24 +04:00
|
|
|
ranges: smallvec![Range {
|
|
|
|
anchor,
|
|
|
|
head,
|
|
|
|
horiz: None
|
|
|
|
}],
|
2020-05-25 08:02:21 +04:00
|
|
|
primary_index: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 07:09:40 +04:00
|
|
|
/// Constructs a selection holding a single cursor.
|
|
|
|
pub fn point(pos: usize) -> Self {
|
|
|
|
Self::single(pos, pos)
|
|
|
|
}
|
|
|
|
|
2021-06-28 22:40:07 +04:00
|
|
|
/// Normalizes a `Selection`.
|
2021-07-01 20:51:24 +04:00
|
|
|
fn normalize(mut self) -> Self {
|
2021-06-28 22:40:07 +04:00
|
|
|
let primary = self.ranges[self.primary_index];
|
|
|
|
self.ranges.sort_unstable_by_key(Range::from);
|
|
|
|
self.primary_index = self
|
|
|
|
.ranges
|
|
|
|
.iter()
|
|
|
|
.position(|&range| range == primary)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut prev_i = 0;
|
|
|
|
for i in 1..self.ranges.len() {
|
|
|
|
if self.ranges[prev_i].overlaps(&self.ranges[i]) {
|
|
|
|
if i == self.primary_index {
|
|
|
|
self.primary_index = prev_i;
|
2021-04-09 19:21:13 +04:00
|
|
|
}
|
2021-06-28 22:40:07 +04:00
|
|
|
self.ranges[prev_i] = self.ranges[prev_i].merge(self.ranges[i]);
|
|
|
|
} else {
|
|
|
|
prev_i += 1;
|
|
|
|
self.ranges[prev_i] = self.ranges[i];
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 22:40:07 +04:00
|
|
|
self.ranges.truncate(prev_i + 1);
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-09 19:21:13 +04:00
|
|
|
// TODO: consume an iterator or a vec to reduce allocations?
|
|
|
|
#[must_use]
|
|
|
|
pub fn new(ranges: SmallVec<[Range; 1]>, primary_index: usize) -> Self {
|
|
|
|
assert!(!ranges.is_empty());
|
2021-06-28 22:40:07 +04:00
|
|
|
debug_assert!(primary_index < ranges.len());
|
2021-04-09 19:21:13 +04:00
|
|
|
|
2021-06-28 22:40:07 +04:00
|
|
|
let mut selection = Self {
|
|
|
|
ranges,
|
|
|
|
primary_index,
|
|
|
|
};
|
|
|
|
|
|
|
|
if selection.ranges.len() > 1 {
|
|
|
|
// TODO: only normalize if needed (any ranges out of order)
|
2021-06-30 18:45:15 +04:00
|
|
|
selection = selection.normalize();
|
2020-09-13 18:38:54 +04:00
|
|
|
}
|
|
|
|
|
2021-06-28 22:40:07 +04:00
|
|
|
selection
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
2020-09-07 12:08:28 +04:00
|
|
|
|
2021-06-28 22:40:07 +04:00
|
|
|
/// Takes a closure and maps each `Range` over the closure.
|
2021-06-30 18:45:15 +04:00
|
|
|
pub fn transform<F>(mut self, f: F) -> Self
|
2020-09-07 12:08:28 +04:00
|
|
|
where
|
|
|
|
F: Fn(Range) -> Range,
|
|
|
|
{
|
2021-06-28 22:40:07 +04:00
|
|
|
for range in self.ranges.iter_mut() {
|
|
|
|
*range = f(*range)
|
|
|
|
}
|
|
|
|
|
2021-07-01 20:51:24 +04:00
|
|
|
self.normalize()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A convenience short-cut for `transform(|r| r.min_width_1(text))`.
|
2021-07-02 06:40:37 +04:00
|
|
|
pub fn min_width_1(self, text: RopeSlice) -> Self {
|
2021-07-01 20:51:24 +04:00
|
|
|
self.transform(|r| r.min_width_1(text))
|
2021-06-28 22:40:07 +04:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:34:22 +04:00
|
|
|
pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a {
|
2020-09-28 20:01:27 +04:00
|
|
|
self.ranges.iter().map(move |range| range.fragment(text))
|
|
|
|
}
|
2021-03-19 06:14:13 +04:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn iter(&self) -> std::slice::Iter<'_, Range> {
|
|
|
|
self.ranges.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.ranges.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoIterator for &'a Selection {
|
|
|
|
type Item = &'a Range;
|
|
|
|
type IntoIter = std::slice::Iter<'a, Range>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> std::slice::Iter<'a, Range> {
|
|
|
|
self.ranges().iter()
|
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
|
2021-03-22 07:21:33 +04:00
|
|
|
// TODO: checkSelection -> check if valid for doc length && sorted
|
2020-05-25 08:02:21 +04:00
|
|
|
|
2021-03-15 12:09:18 +04:00
|
|
|
pub fn keep_matches(
|
|
|
|
text: RopeSlice,
|
|
|
|
selection: &Selection,
|
|
|
|
regex: &crate::regex::Regex,
|
|
|
|
) -> Option<Selection> {
|
|
|
|
let result: SmallVec<_> = selection
|
|
|
|
.iter()
|
|
|
|
.filter(|range| regex.is_match(&range.fragment(text)))
|
|
|
|
.copied()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// TODO: figure out a new primary index
|
|
|
|
if !result.is_empty() {
|
|
|
|
return Some(Selection::new(result, 0));
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-01-22 12:13:14 +04:00
|
|
|
pub fn select_on_matches(
|
2021-02-18 13:34:22 +04:00
|
|
|
text: RopeSlice,
|
2021-03-15 12:09:18 +04:00
|
|
|
selection: &Selection,
|
2021-01-22 12:13:14 +04:00
|
|
|
regex: &crate::regex::Regex,
|
2021-02-17 09:41:43 +04:00
|
|
|
) -> Option<Selection> {
|
2021-03-19 06:14:13 +04:00
|
|
|
let mut result = SmallVec::with_capacity(selection.len());
|
2021-01-22 12:13:14 +04:00
|
|
|
|
2021-03-19 06:14:13 +04:00
|
|
|
for sel in selection {
|
2021-01-22 12:13:14 +04:00
|
|
|
// TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
|
2021-02-18 13:34:22 +04:00
|
|
|
let fragment = sel.fragment(text);
|
2021-01-22 12:13:14 +04:00
|
|
|
|
2021-07-01 23:55:18 +04:00
|
|
|
let sel_start = sel.from();
|
|
|
|
let start_byte = text.char_to_byte(sel_start);
|
2021-01-22 12:13:14 +04:00
|
|
|
|
|
|
|
for mat in regex.find_iter(&fragment) {
|
|
|
|
// TODO: retain range direction
|
|
|
|
|
|
|
|
let start = text.byte_to_char(start_byte + mat.start());
|
|
|
|
let end = text.byte_to_char(start_byte + mat.end());
|
2021-06-25 12:40:23 +04:00
|
|
|
result.push(Range::new(start, end));
|
2021-01-22 12:13:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: figure out a new primary index
|
2021-02-17 09:41:43 +04:00
|
|
|
if !result.is_empty() {
|
|
|
|
return Some(Selection::new(result, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2021-01-22 12:13:14 +04:00
|
|
|
}
|
|
|
|
|
2020-09-28 20:01:27 +04:00
|
|
|
// TODO: support to split on capture #N instead of whole match
|
|
|
|
pub fn split_on_matches(
|
2021-02-18 13:34:22 +04:00
|
|
|
text: RopeSlice,
|
2021-03-15 12:09:18 +04:00
|
|
|
selection: &Selection,
|
2020-09-28 20:01:27 +04:00
|
|
|
regex: &crate::regex::Regex,
|
|
|
|
) -> Selection {
|
2021-03-19 06:14:13 +04:00
|
|
|
let mut result = SmallVec::with_capacity(selection.len());
|
2020-09-28 20:01:27 +04:00
|
|
|
|
2021-03-19 06:14:13 +04:00
|
|
|
for sel in selection {
|
2021-06-25 12:40:23 +04:00
|
|
|
// Special case: zero-width selection.
|
|
|
|
if sel.from() == sel.to() {
|
|
|
|
result.push(*sel);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-28 20:01:27 +04:00
|
|
|
// TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
|
2021-02-18 13:34:22 +04:00
|
|
|
let fragment = sel.fragment(text);
|
2020-09-28 20:01:27 +04:00
|
|
|
|
2021-07-01 23:55:18 +04:00
|
|
|
let sel_start = sel.from();
|
2020-09-28 20:01:27 +04:00
|
|
|
let sel_end = sel.to();
|
|
|
|
|
2021-07-01 23:55:18 +04:00
|
|
|
let start_byte = text.char_to_byte(sel_start);
|
2020-09-28 20:01:27 +04:00
|
|
|
|
|
|
|
let mut start = sel_start;
|
|
|
|
|
|
|
|
for mat in regex.find_iter(&fragment) {
|
|
|
|
// TODO: retain range direction
|
|
|
|
let end = text.byte_to_char(start_byte + mat.start());
|
2021-06-25 12:40:23 +04:00
|
|
|
result.push(Range::new(start, end));
|
2020-09-28 20:01:27 +04:00
|
|
|
start = text.byte_to_char(start_byte + mat.end());
|
|
|
|
}
|
|
|
|
|
2021-06-25 12:40:23 +04:00
|
|
|
if start < sel_end {
|
2020-09-28 20:01:27 +04:00
|
|
|
result.push(Range::new(start, sel_end));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: figure out a new primary index
|
|
|
|
Selection::new(result, 0)
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2021-07-01 23:24:22 +04:00
|
|
|
use crate::Rope;
|
2020-05-25 08:02:21 +04:00
|
|
|
|
2021-03-22 07:21:33 +04:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_new_empty() {
|
2021-07-01 23:55:18 +04:00
|
|
|
let _ = Selection::new(smallvec![], 0);
|
2021-03-22 07:21:33 +04:00
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
#[test]
|
|
|
|
fn test_create_normalizes_and_merges() {
|
|
|
|
let sel = Selection::new(
|
2020-05-26 13:11:11 +04:00
|
|
|
smallvec![
|
2020-05-25 08:02:21 +04:00
|
|
|
Range::new(10, 12),
|
|
|
|
Range::new(6, 7),
|
|
|
|
Range::new(4, 5),
|
|
|
|
Range::new(3, 4),
|
|
|
|
Range::new(0, 6),
|
|
|
|
Range::new(7, 8),
|
|
|
|
Range::new(9, 13),
|
|
|
|
Range::new(13, 14),
|
|
|
|
],
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
|
|
|
let res = sel
|
|
|
|
.ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|range| format!("{}/{}", range.anchor, range.head))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(",");
|
|
|
|
|
|
|
|
assert_eq!(res, "0/6,6/7,7/8,9/13,13/14");
|
2021-06-30 11:08:41 +04:00
|
|
|
|
|
|
|
// it correctly calculates a new primary index
|
|
|
|
let sel = Selection::new(
|
|
|
|
smallvec![Range::new(0, 2), Range::new(1, 5), Range::new(4, 7)],
|
|
|
|
2,
|
|
|
|
);
|
|
|
|
|
|
|
|
let res = sel
|
|
|
|
.ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|range| format!("{}/{}", range.anchor, range.head))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(",");
|
|
|
|
|
|
|
|
assert_eq!(res, "0/7");
|
|
|
|
assert_eq!(sel.primary_index, 0);
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_merges_adjacent_points() {
|
|
|
|
let sel = Selection::new(
|
2020-05-26 13:11:11 +04:00
|
|
|
smallvec![
|
2020-05-25 08:02:21 +04:00
|
|
|
Range::new(10, 12),
|
|
|
|
Range::new(12, 12),
|
|
|
|
Range::new(12, 12),
|
|
|
|
Range::new(10, 10),
|
|
|
|
Range::new(8, 10),
|
|
|
|
],
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
|
|
|
let res = sel
|
|
|
|
.ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|range| format!("{}/{}", range.anchor, range.head))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(",");
|
|
|
|
|
2021-06-25 12:40:23 +04:00
|
|
|
assert_eq!(res, "8/10,10/12,12/12");
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
2020-09-28 20:00:35 +04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_contains() {
|
|
|
|
let range = Range::new(10, 12);
|
|
|
|
|
|
|
|
assert_eq!(range.contains(9), false);
|
|
|
|
assert_eq!(range.contains(10), true);
|
|
|
|
assert_eq!(range.contains(11), true);
|
|
|
|
assert_eq!(range.contains(12), false);
|
|
|
|
assert_eq!(range.contains(13), false);
|
|
|
|
|
|
|
|
let range = Range::new(9, 6);
|
2021-06-25 12:40:23 +04:00
|
|
|
assert_eq!(range.contains(9), false);
|
2020-09-28 20:00:35 +04:00
|
|
|
assert_eq!(range.contains(7), true);
|
2021-06-25 12:40:23 +04:00
|
|
|
assert_eq!(range.contains(6), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_overlaps() {
|
2021-06-28 18:51:47 +04:00
|
|
|
fn overlaps(a: (usize, usize), b: (usize, usize)) -> bool {
|
|
|
|
Range::new(a.0, a.1).overlaps(&Range::new(b.0, b.1))
|
|
|
|
}
|
|
|
|
|
2021-06-25 12:40:23 +04:00
|
|
|
// Two non-zero-width ranges, no overlap.
|
2021-06-28 18:51:47 +04:00
|
|
|
assert!(!overlaps((0, 3), (3, 6)));
|
|
|
|
assert!(!overlaps((0, 3), (6, 3)));
|
|
|
|
assert!(!overlaps((3, 0), (3, 6)));
|
|
|
|
assert!(!overlaps((3, 0), (6, 3)));
|
|
|
|
assert!(!overlaps((3, 6), (0, 3)));
|
|
|
|
assert!(!overlaps((3, 6), (3, 0)));
|
|
|
|
assert!(!overlaps((6, 3), (0, 3)));
|
|
|
|
assert!(!overlaps((6, 3), (3, 0)));
|
2021-06-25 12:40:23 +04:00
|
|
|
|
|
|
|
// Two non-zero-width ranges, overlap.
|
2021-06-28 18:51:47 +04:00
|
|
|
assert!(overlaps((0, 4), (3, 6)));
|
|
|
|
assert!(overlaps((0, 4), (6, 3)));
|
|
|
|
assert!(overlaps((4, 0), (3, 6)));
|
|
|
|
assert!(overlaps((4, 0), (6, 3)));
|
|
|
|
assert!(overlaps((3, 6), (0, 4)));
|
|
|
|
assert!(overlaps((3, 6), (4, 0)));
|
|
|
|
assert!(overlaps((6, 3), (0, 4)));
|
|
|
|
assert!(overlaps((6, 3), (4, 0)));
|
2021-06-25 12:40:23 +04:00
|
|
|
|
|
|
|
// Zero-width and non-zero-width range, no overlap.
|
2021-06-28 18:51:47 +04:00
|
|
|
assert!(!overlaps((0, 3), (3, 3)));
|
|
|
|
assert!(!overlaps((3, 0), (3, 3)));
|
|
|
|
assert!(!overlaps((3, 3), (0, 3)));
|
|
|
|
assert!(!overlaps((3, 3), (3, 0)));
|
2021-06-25 12:40:23 +04:00
|
|
|
|
|
|
|
// Zero-width and non-zero-width range, overlap.
|
2021-06-28 18:51:47 +04:00
|
|
|
assert!(overlaps((1, 4), (1, 1)));
|
|
|
|
assert!(overlaps((4, 1), (1, 1)));
|
|
|
|
assert!(overlaps((1, 1), (1, 4)));
|
|
|
|
assert!(overlaps((1, 1), (4, 1)));
|
2021-06-25 12:40:23 +04:00
|
|
|
|
2021-06-28 18:51:47 +04:00
|
|
|
assert!(overlaps((1, 4), (3, 3)));
|
|
|
|
assert!(overlaps((4, 1), (3, 3)));
|
|
|
|
assert!(overlaps((3, 3), (1, 4)));
|
|
|
|
assert!(overlaps((3, 3), (4, 1)));
|
2021-06-25 12:40:23 +04:00
|
|
|
|
|
|
|
// Two zero-width ranges, no overlap.
|
2021-06-28 18:51:47 +04:00
|
|
|
assert!(!overlaps((0, 0), (1, 1)));
|
|
|
|
assert!(!overlaps((1, 1), (0, 0)));
|
2021-06-25 12:40:23 +04:00
|
|
|
|
|
|
|
// Two zero-width ranges, overlap.
|
2021-06-28 18:51:47 +04:00
|
|
|
assert!(overlaps((1, 1), (1, 1)));
|
2020-09-28 20:00:35 +04:00
|
|
|
}
|
|
|
|
|
2021-06-27 02:37:32 +04:00
|
|
|
#[test]
|
2021-06-28 18:51:47 +04:00
|
|
|
fn test_graphem_aligned() {
|
2021-06-27 02:37:32 +04:00
|
|
|
let r = Rope::from_str("\r\nHi\r\n");
|
|
|
|
let s = r.slice(..);
|
|
|
|
|
2021-06-28 18:51:47 +04:00
|
|
|
// Zero-width.
|
|
|
|
assert_eq!(Range::new(0, 0).grapheme_aligned(s), Range::new(0, 0));
|
|
|
|
assert_eq!(Range::new(1, 1).grapheme_aligned(s), Range::new(0, 0));
|
|
|
|
assert_eq!(Range::new(2, 2).grapheme_aligned(s), Range::new(2, 2));
|
|
|
|
assert_eq!(Range::new(3, 3).grapheme_aligned(s), Range::new(3, 3));
|
|
|
|
assert_eq!(Range::new(4, 4).grapheme_aligned(s), Range::new(4, 4));
|
|
|
|
assert_eq!(Range::new(5, 5).grapheme_aligned(s), Range::new(4, 4));
|
|
|
|
assert_eq!(Range::new(6, 6).grapheme_aligned(s), Range::new(6, 6));
|
|
|
|
|
|
|
|
// Forward.
|
|
|
|
assert_eq!(Range::new(0, 1).grapheme_aligned(s), Range::new(0, 2));
|
|
|
|
assert_eq!(Range::new(1, 2).grapheme_aligned(s), Range::new(0, 2));
|
|
|
|
assert_eq!(Range::new(2, 3).grapheme_aligned(s), Range::new(2, 3));
|
|
|
|
assert_eq!(Range::new(3, 4).grapheme_aligned(s), Range::new(3, 4));
|
|
|
|
assert_eq!(Range::new(4, 5).grapheme_aligned(s), Range::new(4, 6));
|
|
|
|
assert_eq!(Range::new(5, 6).grapheme_aligned(s), Range::new(4, 6));
|
|
|
|
|
|
|
|
assert_eq!(Range::new(0, 2).grapheme_aligned(s), Range::new(0, 2));
|
|
|
|
assert_eq!(Range::new(1, 3).grapheme_aligned(s), Range::new(0, 3));
|
|
|
|
assert_eq!(Range::new(2, 4).grapheme_aligned(s), Range::new(2, 4));
|
|
|
|
assert_eq!(Range::new(3, 5).grapheme_aligned(s), Range::new(3, 6));
|
|
|
|
assert_eq!(Range::new(4, 6).grapheme_aligned(s), Range::new(4, 6));
|
|
|
|
|
|
|
|
// Reverse.
|
|
|
|
assert_eq!(Range::new(1, 0).grapheme_aligned(s), Range::new(2, 0));
|
|
|
|
assert_eq!(Range::new(2, 1).grapheme_aligned(s), Range::new(2, 0));
|
|
|
|
assert_eq!(Range::new(3, 2).grapheme_aligned(s), Range::new(3, 2));
|
|
|
|
assert_eq!(Range::new(4, 3).grapheme_aligned(s), Range::new(4, 3));
|
|
|
|
assert_eq!(Range::new(5, 4).grapheme_aligned(s), Range::new(6, 4));
|
|
|
|
assert_eq!(Range::new(6, 5).grapheme_aligned(s), Range::new(6, 4));
|
|
|
|
|
|
|
|
assert_eq!(Range::new(2, 0).grapheme_aligned(s), Range::new(2, 0));
|
|
|
|
assert_eq!(Range::new(3, 1).grapheme_aligned(s), Range::new(3, 0));
|
|
|
|
assert_eq!(Range::new(4, 2).grapheme_aligned(s), Range::new(4, 2));
|
|
|
|
assert_eq!(Range::new(5, 3).grapheme_aligned(s), Range::new(6, 3));
|
|
|
|
assert_eq!(Range::new(6, 4).grapheme_aligned(s), Range::new(6, 4));
|
2021-06-27 02:37:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-06-28 18:51:47 +04:00
|
|
|
fn test_min_width_1() {
|
2021-06-27 02:37:32 +04:00
|
|
|
let r = Rope::from_str("\r\nHi\r\n");
|
|
|
|
let s = r.slice(..);
|
|
|
|
|
2021-06-28 18:51:47 +04:00
|
|
|
// Zero-width.
|
|
|
|
assert_eq!(Range::new(0, 0).min_width_1(s), Range::new(0, 2));
|
|
|
|
assert_eq!(Range::new(1, 1).min_width_1(s), Range::new(1, 2));
|
|
|
|
assert_eq!(Range::new(2, 2).min_width_1(s), Range::new(2, 3));
|
|
|
|
assert_eq!(Range::new(3, 3).min_width_1(s), Range::new(3, 4));
|
|
|
|
assert_eq!(Range::new(4, 4).min_width_1(s), Range::new(4, 6));
|
|
|
|
assert_eq!(Range::new(5, 5).min_width_1(s), Range::new(5, 6));
|
|
|
|
assert_eq!(Range::new(6, 6).min_width_1(s), Range::new(6, 6));
|
|
|
|
|
|
|
|
// Forward.
|
|
|
|
assert_eq!(Range::new(0, 1).min_width_1(s), Range::new(0, 1));
|
|
|
|
assert_eq!(Range::new(1, 2).min_width_1(s), Range::new(1, 2));
|
|
|
|
assert_eq!(Range::new(2, 3).min_width_1(s), Range::new(2, 3));
|
|
|
|
assert_eq!(Range::new(3, 4).min_width_1(s), Range::new(3, 4));
|
|
|
|
assert_eq!(Range::new(4, 5).min_width_1(s), Range::new(4, 5));
|
|
|
|
assert_eq!(Range::new(5, 6).min_width_1(s), Range::new(5, 6));
|
|
|
|
|
|
|
|
// Reverse.
|
|
|
|
assert_eq!(Range::new(1, 0).min_width_1(s), Range::new(1, 0));
|
|
|
|
assert_eq!(Range::new(2, 1).min_width_1(s), Range::new(2, 1));
|
|
|
|
assert_eq!(Range::new(3, 2).min_width_1(s), Range::new(3, 2));
|
|
|
|
assert_eq!(Range::new(4, 3).min_width_1(s), Range::new(4, 3));
|
|
|
|
assert_eq!(Range::new(5, 4).min_width_1(s), Range::new(5, 4));
|
|
|
|
assert_eq!(Range::new(6, 5).min_width_1(s), Range::new(6, 5));
|
2021-06-27 02:37:32 +04:00
|
|
|
}
|
|
|
|
|
2020-09-28 20:01:27 +04:00
|
|
|
#[test]
|
|
|
|
fn test_split_on_matches() {
|
|
|
|
use crate::regex::Regex;
|
|
|
|
|
2021-06-25 12:40:23 +04:00
|
|
|
let text = Rope::from(" abcd efg wrs xyz 123 456");
|
2020-09-28 20:01:27 +04:00
|
|
|
|
2021-06-25 12:40:23 +04:00
|
|
|
let selection = Selection::new(smallvec![Range::new(0, 9), Range::new(11, 20),], 0);
|
2020-09-28 20:01:27 +04:00
|
|
|
|
2021-03-15 12:09:18 +04:00
|
|
|
let result = split_on_matches(text.slice(..), &selection, &Regex::new(r"\s+").unwrap());
|
2020-09-28 20:01:27 +04:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
result.ranges(),
|
|
|
|
&[
|
2021-06-27 02:37:32 +04:00
|
|
|
// TODO: rather than this behavior, maybe we want it
|
|
|
|
// to be based on which side is the anchor?
|
|
|
|
//
|
2021-06-25 12:40:23 +04:00
|
|
|
// We get a leading zero-width range when there's
|
|
|
|
// a leading match because ranges are inclusive on
|
|
|
|
// the left. Imagine, for example, if the entire
|
|
|
|
// selection range were matched: you'd still want
|
|
|
|
// at least one range to remain after the split.
|
|
|
|
Range::new(0, 0),
|
|
|
|
Range::new(1, 5),
|
|
|
|
Range::new(6, 9),
|
|
|
|
Range::new(11, 13),
|
|
|
|
Range::new(16, 19),
|
|
|
|
// In contrast to the comment above, there is no
|
|
|
|
// _trailing_ zero-width range despite the trailing
|
|
|
|
// match, because ranges are exclusive on the right.
|
2020-09-28 20:01:27 +04:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-02-18 13:34:22 +04:00
|
|
|
result.fragments(text.slice(..)).collect::<Vec<_>>(),
|
2021-06-25 12:40:23 +04:00
|
|
|
&["", "abcd", "efg", "rs", "xyz"]
|
2020-09-28 20:01:27 +04:00
|
|
|
);
|
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|