on master help sign in
Fix panic on ctrl-w empty document
5e2ba28Ivan Tham
helix-core/src/words.rs+5−2
@@ -25,7 +25,7 @@ pub fn nth_prev_word_boundary(slice: RopeSlice, mut char_idx: usize, count: usiz
25 }
25 }
26 }
26 }
27
27
28− if with_end {
28+ if with_end || char_idx == 0 {
29 char_idx
29 char_idx
30 } else {
30 } else {
31 char_idx + 1
31 char_idx + 1
@@ -37,7 +37,7 @@ fn different_prev_word_boundary() {
37 use ropey::Rope;
37 use ropey::Rope;
38 let t = |x, y| {
38 let t = |x, y| {
39 let text = Rope::from(x);
39 let text = Rope::from(x);
40− let out = nth_prev_word_boundary(text.slice(..), text.len_chars() - 1, 1);
40+ let out = nth_prev_word_boundary(text.slice(..), text.len_chars().saturating_sub(1), 1);
41 assert_eq!(text.slice(..out), y, r#"from "{}""#, x);
41 assert_eq!(text.slice(..out), y, r#"from "{}""#, x);
42 };
42 };
43 t("abcd\nefg\nwrs", "abcd\nefg\n");
43 t("abcd\nefg\nwrs", "abcd\nefg\n");
@@ -47,6 +47,7 @@ fn different_prev_word_boundary() {
47 t("hello, world", "hello, ");
47 t("hello, world", "hello, ");
48 t("hello, ", "hello");
48 t("hello, ", "hello");
49 t("hello", "");
49 t("hello", "");
50+ t(",", "");
50 t("こんにちは、世界!", "こんにちは、世界");
51 t("こんにちは、世界!", "こんにちは、世界");
51 t("こんにちは、世界", "こんにちは、");
52 t("こんにちは、世界", "こんにちは、");
52 t("こんにちは、", "こんにちは");
53 t("こんにちは、", "こんにちは");
@@ -56,10 +57,12 @@ fn different_prev_word_boundary() {
56 t("お前はもう死んでいる", "");
57 t("お前はもう死んでいる", "");
57 t("その300円です", ""); // TODO: should stop at 300
58 t("その300円です", ""); // TODO: should stop at 300
58 t("唱k", ""); // TODO: should stop at 唱
59 t("唱k", ""); // TODO: should stop at 唱
60+ t(",", "");
59 t("1 + 1 = 2", "1 + 1 = ");
61 t("1 + 1 = 2", "1 + 1 = ");
60 t("1 + 1 =", "1 + 1 ");
62 t("1 + 1 =", "1 + 1 ");
61 t("1 + 1", "1 + ");
63 t("1 + 1", "1 + ");
62 t("1 + ", "1 ");
64 t("1 + ", "1 ");
63 t("1 ", "");
65 t("1 ", "");
64 t("1+1=2", "1+1=");
66 t("1+1=2", "1+1=");
67+ t("", "");
65}
68}