2020-05-25 08:02:21 +04:00
|
|
|
//! Selections are the primary editing construct. Even a single cursor is defined as an empty
|
|
|
|
//! single selection range.
|
|
|
|
//!
|
|
|
|
//! All positioning is done via `char` offsets into the buffer.
|
2020-09-28 20:01:27 +04:00
|
|
|
use crate::{Assoc, ChangeSet, Rope, RopeSlice};
|
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
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn abs_difference(x: usize, y: usize) -> usize {
|
|
|
|
if x < y {
|
|
|
|
y - x
|
|
|
|
} else {
|
|
|
|
x - y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single selection range. Anchor-inclusive, head-exclusive.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub struct Range {
|
|
|
|
// TODO: optimize into u32
|
|
|
|
/// 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>,
|
2020-09-28 20:01:27 +04:00
|
|
|
} // TODO: might be cheaper to store normalized as from/to and an inverted flag
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
|
|
|
// cursor overlap is checked differently
|
2021-04-10 07:01:49 +04:00
|
|
|
if self.is_empty() {
|
|
|
|
let pos = self.head;
|
|
|
|
pos >= other.from() && other.to() >= pos
|
|
|
|
} else {
|
|
|
|
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 {
|
|
|
|
if self.is_empty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.anchor < self.head {
|
|
|
|
self.anchor <= pos && pos < self.head
|
|
|
|
} else {
|
|
|
|
self.head < pos && pos <= self.anchor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// TODO: possibly unnecessary
|
|
|
|
if self.anchor == anchor && self.head == head {
|
|
|
|
return self;
|
|
|
|
}
|
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 {
|
|
|
|
if from <= self.anchor && to >= self.anchor {
|
2021-03-24 09:52:13 +04:00
|
|
|
return Self {
|
2020-05-25 08:02:21 +04:00
|
|
|
anchor: from,
|
|
|
|
head: to,
|
2021-02-12 11:49:24 +04:00
|
|
|
horiz: None,
|
2020-05-25 08:02:21 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:52:13 +04:00
|
|
|
Self {
|
2020-05-25 08:02:21 +04:00
|
|
|
anchor: self.anchor,
|
|
|
|
head: if abs_difference(from, self.anchor) > abs_difference(to, self.anchor) {
|
|
|
|
from
|
|
|
|
} else {
|
|
|
|
to
|
|
|
|
},
|
2021-02-12 11:49:24 +04:00
|
|
|
horiz: None,
|
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> {
|
2020-10-06 11:00:23 +04:00
|
|
|
Cow::from(text.slice(self.from()..self.to() + 1))
|
2020-09-28 20:01:27 +04:00
|
|
|
}
|
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
|
|
|
let index = self.ranges.len();
|
|
|
|
self.ranges.push(range);
|
|
|
|
|
|
|
|
Self::normalize(self.ranges, index)
|
|
|
|
}
|
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-04-09 19:21:13 +04:00
|
|
|
fn normalize(mut ranges: SmallVec<[Range; 1]>, mut primary_index: usize) -> Selection {
|
|
|
|
let primary = ranges[primary_index];
|
|
|
|
ranges.sort_unstable_by_key(Range::from);
|
|
|
|
primary_index = ranges.iter().position(|&range| range == primary).unwrap();
|
2021-03-22 07:21:33 +04:00
|
|
|
|
2021-04-09 19:21:13 +04:00
|
|
|
let mut result = SmallVec::with_capacity(ranges.len()); // approx
|
|
|
|
|
|
|
|
// TODO: we could do with one vec by removing elements as we mutate
|
|
|
|
|
|
|
|
for (i, range) in ranges.into_iter().enumerate() {
|
|
|
|
// if previous value exists
|
|
|
|
if let Some(prev) = result.last_mut() {
|
|
|
|
// and we overlap it
|
2021-04-10 07:01:49 +04:00
|
|
|
|
|
|
|
// TODO: we used to simply check range.from() <(=) prev.to()
|
|
|
|
// avoiding two comparisons
|
2021-04-09 19:21:13 +04:00
|
|
|
if range.overlaps(prev) {
|
|
|
|
let from = prev.from();
|
|
|
|
let to = std::cmp::max(range.to(), prev.to());
|
|
|
|
|
|
|
|
if i <= primary_index {
|
|
|
|
primary_index -= 1
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
|
2021-04-09 19:21:13 +04:00
|
|
|
// merge into previous
|
|
|
|
if range.anchor > range.head {
|
|
|
|
prev.anchor = to;
|
|
|
|
prev.head = from;
|
|
|
|
} else {
|
|
|
|
prev.anchor = from;
|
|
|
|
prev.head = to;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
|
2021-04-09 19:21:13 +04:00
|
|
|
result.push(range)
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
|
2021-04-09 19:21:13 +04:00
|
|
|
Selection {
|
|
|
|
ranges: result,
|
|
|
|
primary_index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
2020-09-13 18:38:54 +04:00
|
|
|
// fast path for a single selection (cursor)
|
|
|
|
if ranges.len() == 1 {
|
2021-03-24 09:52:13 +04:00
|
|
|
return Self {
|
2020-09-13 18:38:54 +04:00
|
|
|
ranges,
|
|
|
|
primary_index: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
// TODO: only normalize if needed (any ranges out of order)
|
2021-04-09 19:21:13 +04:00
|
|
|
Self::normalize(ranges, primary_index)
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
2020-09-07 12:08:28 +04:00
|
|
|
|
|
|
|
/// Takes a closure and maps each selection over the closure.
|
2020-09-12 18:52:38 +04:00
|
|
|
pub fn transform<F>(&self, f: F) -> Self
|
2020-09-07 12:08:28 +04:00
|
|
|
where
|
|
|
|
F: Fn(Range) -> Range,
|
|
|
|
{
|
2020-09-12 18:52:38 +04:00
|
|
|
Self::new(
|
|
|
|
self.ranges.iter().copied().map(f).collect(),
|
|
|
|
self.primary_index,
|
|
|
|
)
|
2020-09-07 12:08:28 +04:00
|
|
|
}
|
2020-09-28 20:01:27 +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
|
|
|
|
|
|
|
let mut sel_start = sel.from();
|
|
|
|
let sel_end = sel.to();
|
|
|
|
|
|
|
|
let mut start_byte = text.char_to_byte(sel_start);
|
|
|
|
|
|
|
|
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());
|
|
|
|
result.push(Range::new(start, end - 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
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
|
|
|
|
|
|
|
let mut sel_start = sel.from();
|
|
|
|
let sel_end = sel.to();
|
|
|
|
|
|
|
|
let mut start_byte = text.char_to_byte(sel_start);
|
|
|
|
|
|
|
|
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());
|
2020-10-04 12:42:36 +04:00
|
|
|
result.push(Range::new(start, end - 1));
|
2020-09-28 20:01:27 +04:00
|
|
|
start = text.byte_to_char(start_byte + mat.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
if start <= sel_end {
|
|
|
|
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-03-22 07:21:33 +04:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_new_empty() {
|
|
|
|
let sel = Selection::new(smallvec![], 0);
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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(",");
|
|
|
|
|
|
|
|
assert_eq!(res, "8/10,10/12");
|
|
|
|
}
|
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);
|
|
|
|
assert_eq!(range.contains(9), true);
|
|
|
|
assert_eq!(range.contains(7), true);
|
|
|
|
assert_eq!(range.contains(6), false);
|
|
|
|
}
|
|
|
|
|
2020-09-28 20:01:27 +04:00
|
|
|
#[test]
|
|
|
|
fn test_split_on_matches() {
|
|
|
|
use crate::regex::Regex;
|
|
|
|
|
|
|
|
let text = Rope::from("abcd efg wrs xyz 123 456");
|
|
|
|
|
2021-03-15 12:09:18 +04:00
|
|
|
let selection = Selection::new(smallvec![Range::new(0, 8), Range::new(10, 19),], 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(),
|
|
|
|
&[
|
2020-10-14 04:38:52 +04:00
|
|
|
Range::new(0, 3),
|
|
|
|
Range::new(5, 7),
|
|
|
|
Range::new(10, 11),
|
|
|
|
Range::new(15, 17),
|
2020-09-28 20:01:27 +04:00
|
|
|
Range::new(19, 19),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-02-18 13:34:22 +04:00
|
|
|
result.fragments(text.slice(..)).collect::<Vec<_>>(),
|
2020-10-14 04:38:52 +04:00
|
|
|
&["abcd", "efg", "rs", "xyz", "1"]
|
2020-09-28 20:01:27 +04:00
|
|
|
);
|
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|