Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

rope: Fix `point_utf16_to_offset` returning relative offset instead of absolute (#48862)

Jordi Villar4ed2b3d04139
diff --git a/crates/rope/src/chunk.rs b/crates/rope/src/chunk.rs
index 49bc8846217..1f94465282c 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::<Vec<_>>(), 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,);
+ }
}