diff --git a/crates/rope/src/chunk.rs b/crates/rope/src/chunk.rs index 49bc88462171c271e8adbdeb7d0976e51e2514c7..1f94465282c1de01f5604a1f435831238afe64bc 100644 --- a/crates/rope/src/chunk.rs +++ b/crates/rope/src/chunk.rs @@ -501,7 +501,7 @@ impl<'a> ChunkSlice<'a> { self.text ); } - return line.len(); + return row_offset_range.end; } let mut offset = row_offset_range.start; @@ -1176,4 +1176,19 @@ mod tests { assert_eq!((max_row, max_chars as u32), (longest_row, longest_chars)); assert_eq!(chunk.tabs().collect::>(), expected_tab_positions); } + + #[gpui::test] + fn test_point_utf16_to_offset_clips_to_correct_absolute_offset() { + let text = "abc\nde"; + let chunk = Chunk::new(text); + let slice = chunk.as_slice(); + + // Clipping on row 0 (row_offset_range.start == 0, so relative == absolute) + assert_eq!(slice.point_utf16_to_offset(PointUtf16::new(0, 99), true), 3,); + + // Clipping on row 1 — this is the case that was buggy. + // Row 1 starts at byte offset 4 ("de" is bytes 4..6), so the + // clipped result must be 6, not 2. + assert_eq!(slice.point_utf16_to_offset(PointUtf16::new(1, 99), true), 6,); + } }