From 47e8bd5f4f57617840055244cbb935d9f75d25c5 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 25 Jul 2022 15:06:04 +0200 Subject: [PATCH] Introduce `MultiBuffer::clip_offset_utf16` --- crates/editor/src/multi_buffer.rs | 19 +++++++++++++++++++ crates/text/src/text.rs | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index 71d0053e56..78d7e94ae5 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -1692,6 +1692,25 @@ impl MultiBufferSnapshot { *cursor.start() + overshoot } + pub fn clip_offset_utf16(&self, offset: OffsetUtf16, bias: Bias) -> OffsetUtf16 { + if let Some((_, _, buffer)) = self.as_singleton() { + return buffer.clip_offset_utf16(offset, bias); + } + + let mut cursor = self.excerpts.cursor::(); + cursor.seek(&offset, Bias::Right, &()); + let overshoot = if let Some(excerpt) = cursor.item() { + let excerpt_start = excerpt.range.context.start.to_offset_utf16(&excerpt.buffer); + let buffer_offset = excerpt + .buffer + .clip_offset_utf16(excerpt_start + (offset - cursor.start()), bias); + OffsetUtf16(buffer_offset.0.saturating_sub(excerpt_start.0)) + } else { + OffsetUtf16(0) + }; + *cursor.start() + overshoot + } + pub fn clip_point_utf16(&self, point: PointUtf16, bias: Bias) -> PointUtf16 { if let Some((_, _, buffer)) = self.as_singleton() { return buffer.clip_point_utf16(point, bias); diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index 1e56439fcc..7da77b22ff 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -1901,6 +1901,10 @@ impl BufferSnapshot { self.visible_text.clip_point(point, bias) } + pub fn clip_offset_utf16(&self, offset: OffsetUtf16, bias: Bias) -> OffsetUtf16 { + self.visible_text.clip_offset_utf16(offset, bias) + } + pub fn clip_point_utf16(&self, point: PointUtf16, bias: Bias) -> PointUtf16 { self.visible_text.clip_point_utf16(point, bias) }