Add a ToPointUtf16 trait in text and multibuffer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
16acbd2123
commit
4900019e9b
5 changed files with 144 additions and 18 deletions
|
@ -179,6 +179,19 @@ impl Rope {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn point_to_point_utf16(&self, point: Point) -> PointUtf16 {
|
||||
if point >= self.summary().lines {
|
||||
return self.summary().lines_utf16;
|
||||
}
|
||||
let mut cursor = self.chunks.cursor::<(Point, PointUtf16)>();
|
||||
cursor.seek(&point, Bias::Left, &());
|
||||
let overshoot = point - cursor.start().0;
|
||||
cursor.start().1
|
||||
+ cursor.item().map_or(PointUtf16::zero(), |chunk| {
|
||||
chunk.point_to_point_utf16(overshoot)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn point_to_offset(&self, point: Point) -> usize {
|
||||
if point >= self.summary().lines {
|
||||
return self.summary().bytes;
|
||||
|
@ -580,6 +593,27 @@ impl Chunk {
|
|||
offset
|
||||
}
|
||||
|
||||
fn point_to_point_utf16(&self, target: Point) -> PointUtf16 {
|
||||
let mut point = Point::zero();
|
||||
let mut point_utf16 = PointUtf16::new(0, 0);
|
||||
for ch in self.0.chars() {
|
||||
if point >= target {
|
||||
break;
|
||||
}
|
||||
|
||||
if ch == '\n' {
|
||||
point_utf16.row += 1;
|
||||
point_utf16.column = 0;
|
||||
point.row += 1;
|
||||
point.column = 0;
|
||||
} else {
|
||||
point_utf16.column += ch.len_utf16() as u32;
|
||||
point.column += ch.len_utf8() as u32;
|
||||
}
|
||||
}
|
||||
point_utf16
|
||||
}
|
||||
|
||||
fn point_utf16_to_offset(&self, target: PointUtf16) -> usize {
|
||||
let mut offset = 0;
|
||||
let mut point = PointUtf16::new(0, 0);
|
||||
|
|
|
@ -1512,6 +1512,10 @@ impl BufferSnapshot {
|
|||
self.visible_text.offset_to_point_utf16(offset)
|
||||
}
|
||||
|
||||
pub fn point_to_point_utf16(&self, point: Point) -> PointUtf16 {
|
||||
self.visible_text.point_to_point_utf16(point)
|
||||
}
|
||||
|
||||
pub fn version(&self) -> &clock::Global {
|
||||
&self.version
|
||||
}
|
||||
|
@ -2260,6 +2264,34 @@ impl ToPoint for Point {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait ToPointUtf16 {
|
||||
fn to_point_utf16<'a>(&self, snapshot: &BufferSnapshot) -> PointUtf16;
|
||||
}
|
||||
|
||||
impl ToPointUtf16 for Anchor {
|
||||
fn to_point_utf16<'a>(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
|
||||
snapshot.summary_for_anchor(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPointUtf16 for usize {
|
||||
fn to_point_utf16<'a>(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
|
||||
snapshot.offset_to_point_utf16(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPointUtf16 for PointUtf16 {
|
||||
fn to_point_utf16<'a>(&self, _: &BufferSnapshot) -> PointUtf16 {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPointUtf16 for Point {
|
||||
fn to_point_utf16<'a>(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
|
||||
snapshot.point_to_point_utf16(*self)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Clip {
|
||||
fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue