Skip to content

Commit 15d59fc

Browse files
authored
vim: Fix crash when using ‘ge’ motion on multibyte character (#31566)
Closes #30919 - [x] Test Release Notes: - Fixed the issue where using the Vim motion `ge` on multibyte character would cause Zed to crash.
1 parent 6545c5e commit 15d59fc

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

crates/vim/src/motion.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,9 @@ fn previous_word_end(
17011701
let mut point = point.to_point(map);
17021702

17031703
if point.column < map.buffer_snapshot.line_len(MultiBufferRow(point.row)) {
1704-
point.column += 1;
1704+
if let Some(ch) = map.buffer_snapshot.chars_at(point).next() {
1705+
point.column += ch.len_utf8() as u32;
1706+
}
17051707
}
17061708
for _ in 0..times {
17071709
let new_point = movement::find_preceding_boundary_point(
@@ -1874,7 +1876,9 @@ fn previous_subword_end(
18741876
let mut point = point.to_point(map);
18751877

18761878
if point.column < map.buffer_snapshot.line_len(MultiBufferRow(point.row)) {
1877-
point.column += 1;
1879+
if let Some(ch) = map.buffer_snapshot.chars_at(point).next() {
1880+
point.column += ch.len_utf8() as u32;
1881+
}
18781882
}
18791883
for _ in 0..times {
18801884
let new_point = movement::find_preceding_boundary_point(
@@ -3613,6 +3617,16 @@ mod test {
36133617
4;5.6 567 678
36143618
789 890 901
36153619
"});
3620+
3621+
// With multi byte char
3622+
cx.set_shared_state(indoc! {r"
3623+
bar ˇó
3624+
"})
3625+
.await;
3626+
cx.simulate_shared_keystrokes("g e").await;
3627+
cx.shared_state().await.assert_eq(indoc! {"
3628+
baˇr ó
3629+
"});
36163630
}
36173631

36183632
#[gpui::test]

crates/vim/test_data/test_previous_word_end.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@
2727
{"Key":"g"}
2828
{"Key":"shift-e"}
2929
{"Get":{"state":"123 234 34ˇ5\n4;5.6 567 678\n789 890 901\n","mode":"Normal"}}
30+
{"Put":{"state":"bar ˇó\n"}}
31+
{"Key":"g"}
32+
{"Key":"e"}
33+
{"Get":{"state":"baˇr ó\n","mode":"Normal"}}

0 commit comments

Comments
 (0)