Clamp UTF-16 to point conversions

This commit is contained in:
Julia
2022-11-21 14:25:01 -05:00
parent bb32599ded
commit 074e3cfbd6
6 changed files with 65 additions and 45 deletions
+1 -1
View File
@@ -55,7 +55,7 @@ use link_go_to_definition::{
};
pub use multi_buffer::{
Anchor, AnchorRangeExt, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot, ToOffset,
ToPoint,
ToOffsetClamped, ToPoint,
};
use multi_buffer::{MultiBufferChunks, ToOffsetUtf16};
use ordered_float::OrderedFloat;
+13 -9
View File
@@ -72,6 +72,10 @@ pub trait ToOffset: 'static + fmt::Debug {
fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> usize;
}
pub trait ToOffsetClamped: 'static + fmt::Debug {
fn to_offset_clamped(&self, snapshot: &MultiBufferSnapshot) -> usize;
}
pub trait ToOffsetUtf16: 'static + fmt::Debug {
fn to_offset_utf16(&self, snapshot: &MultiBufferSnapshot) -> OffsetUtf16;
}
@@ -1945,9 +1949,9 @@ impl MultiBufferSnapshot {
}
}
pub fn point_utf16_to_offset(&self, point: PointUtf16) -> usize {
pub fn point_utf16_to_offset_clamped(&self, point: PointUtf16) -> usize {
if let Some((_, _, buffer)) = self.as_singleton() {
return buffer.point_utf16_to_offset(point);
return buffer.point_utf16_to_offset_clamped(point);
}
let mut cursor = self.excerpts.cursor::<(PointUtf16, usize)>();
@@ -1961,7 +1965,7 @@ impl MultiBufferSnapshot {
.offset_to_point_utf16(excerpt.range.context.start.to_offset(&excerpt.buffer));
let buffer_offset = excerpt
.buffer
.point_utf16_to_offset(excerpt_start_point + overshoot);
.point_utf16_to_offset_clamped(excerpt_start_point + overshoot);
*start_offset + (buffer_offset - excerpt_start_offset)
} else {
self.excerpts.summary().text.len
@@ -3274,12 +3278,6 @@ impl ToOffset for Point {
}
}
impl ToOffset for PointUtf16 {
fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
snapshot.point_utf16_to_offset(*self)
}
}
impl ToOffset for usize {
fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
assert!(*self <= snapshot.len(), "offset is out of range");
@@ -3293,6 +3291,12 @@ impl ToOffset for OffsetUtf16 {
}
}
impl ToOffsetClamped for PointUtf16 {
fn to_offset_clamped<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
snapshot.point_utf16_to_offset_clamped(*self)
}
}
impl ToOffsetUtf16 for OffsetUtf16 {
fn to_offset_utf16(&self, _snapshot: &MultiBufferSnapshot) -> OffsetUtf16 {
*self
+25 -2
View File
@@ -14,6 +14,7 @@ use util::post_inc;
use crate::{
display_map::{DisplayMap, DisplaySnapshot, ToDisplayPoint},
Anchor, DisplayPoint, ExcerptId, MultiBuffer, MultiBufferSnapshot, SelectMode, ToOffset,
ToOffsetClamped,
};
#[derive(Clone)]
@@ -544,11 +545,33 @@ impl<'a> MutableSelectionsCollection<'a> {
T: ToOffset,
{
let buffer = self.buffer.read(self.cx).snapshot(self.cx);
let ranges = ranges
.into_iter()
.map(|range| range.start.to_offset(&buffer)..range.end.to_offset(&buffer));
self.select_offset_ranges(ranges);
}
pub fn select_clamped_ranges<I, T>(&mut self, ranges: I)
where
I: IntoIterator<Item = Range<T>>,
T: ToOffsetClamped,
{
let buffer = self.buffer.read(self.cx).snapshot(self.cx);
let ranges = ranges.into_iter().map(|range| {
range.start.to_offset_clamped(&buffer)..range.end.to_offset_clamped(&buffer)
});
self.select_offset_ranges(ranges);
}
fn select_offset_ranges<I>(&mut self, ranges: I)
where
I: IntoIterator<Item = Range<usize>>,
{
let selections = ranges
.into_iter()
.map(|range| {
let mut start = range.start.to_offset(&buffer);
let mut end = range.end.to_offset(&buffer);
let mut start = range.start;
let mut end = range.end;
let reversed = if start > end {
mem::swap(&mut start, &mut end);
true