@@ -61,99 +61,110 @@ pub fn move_vertically(
64−pub fn move_next_word_start(slice: RopeSlice, mut pos: usize, count: usize) -> usize {
64+pub fn move_next_word_start(slice: RopeSlice, mut begin: usize, count: usize) -> Option<Range> {
65 for _ in 0..count {
67 for _ in 0..count {
66− if pos + 1 == slice.len_chars() {
68+ if begin + 1 == slice.len_chars() {
67− return pos;
69+ return None;
70− let mut ch = slice.char(pos);
72+ let mut ch = slice.char(begin);
71− let next = slice.char(pos + 1);
73+ let next = slice.char(begin + 1);
73 // if we're at the end of a word, or on whitespce right before new one
75 // if we're at the end of a word, or on whitespce right before new one
74 if categorize(ch) != categorize(next) {
76 if categorize(ch) != categorize(next) {
75− pos += 1;
77+ begin += 1;
80+ // return if not skip while?
81+ skip_over_next(slice, &mut begin, |ch| ch == '\n');
82+ ch = slice.char(begin);
79 if is_word(ch) {
86 if is_word(ch) {
80− skip_over_next(slice, &mut pos, is_word);
87+ skip_over_next(slice, &mut end, is_word);
81 } else if ch.is_ascii_punctuation() {
88 } else if ch.is_ascii_punctuation() {
82− skip_over_next(slice, &mut pos, |ch| ch.is_ascii_punctuation());
89+ skip_over_next(slice, &mut end, |ch| ch.is_ascii_punctuation());
85− // TODO: don't include newline?
86− skip_over_next(slice, &mut pos, |ch| ch.is_ascii_whitespace());
92+ skip_over_next(slice, &mut end, is_horiz_blank);
89− pos
95+ Some(Range::new(begin, end - 1))
92−pub fn move_prev_word_start(slice: RopeSlice, mut pos: usize, count: usize) -> usize {
98+pub fn move_prev_word_start(slice: RopeSlice, mut begin: usize, count: usize) -> Option<Range> {
99+ let mut with_end = false;
100+ let mut end = begin;
93 for _ in 0..count {
102 for _ in 0..count {
94− if pos == 0 {
103+ if begin == 0 {
95− return pos;
104+ return None;
98− let ch = slice.char(pos);
107+ let ch = slice.char(begin);
99− let prev = slice.char(pos - 1);
108+ let prev = slice.char(begin - 1);
101 if categorize(ch) != categorize(prev) {
110 if categorize(ch) != categorize(prev) {
102− pos -= 1;
111+ begin -= 1;
105− // match (category c1, category c2) => {
114+ // return if not skip while?
106− // if c1 != c2 {
115+ skip_over_prev(slice, &mut begin, |ch| ch == '\n');
110− // TODO: skip while eol
117+ end = begin;
112− // TODO: don't include newline?
113− skip_over_prev(slice, &mut pos, |ch| ch.is_ascii_whitespace());
119+ with_end = skip_over_prev(slice, &mut end, is_horiz_blank);
115 // refetch
121 // refetch
116− let ch = slice.char(pos);
122+ let ch = slice.char(end);
118 if is_word(ch) {
124 if is_word(ch) {
119− skip_over_prev(slice, &mut pos, is_word);
125+ with_end = skip_over_prev(slice, &mut end, is_word);
120 } else if ch.is_ascii_punctuation() {
126 } else if ch.is_ascii_punctuation() {
121− skip_over_prev(slice, &mut pos, |ch| ch.is_ascii_punctuation());
127+ with_end = skip_over_prev(slice, &mut end, |ch| ch.is_ascii_punctuation());
123− pos = pos.saturating_add(1)
126− pos
131+ // we want to include begin
132+ Some(Range::new(begin + 1, if with_end { end } else { end + 1 }))
129−pub fn move_next_word_end(slice: RopeSlice, mut pos: usize, count: usize) -> usize {
135+pub fn move_next_word_end(slice: RopeSlice, mut begin: usize, count: usize) -> Option<Range> {
136+ let mut end = begin;
130 for _ in 0..count {
138 for _ in 0..count {
131− if pos + 1 == slice.len_chars() {
139+ if begin + 1 == slice.len_chars() {
132− return pos;
140+ return None;
135− let ch = slice.char(pos);
143+ let ch = slice.char(begin);
136− let next = slice.char(pos + 1);
144+ let next = slice.char(begin + 1);
138 if categorize(ch) != categorize(next) {
146 if categorize(ch) != categorize(next) {
139− pos += 1;
147+ begin += 1;
142− // TODO: don't include newline?
150+ // return if not skip while?
143− skip_over_next(slice, &mut pos, |ch| ch.is_ascii_whitespace());
151+ skip_over_next(slice, &mut begin, |ch| ch == '\n');
155+ skip_over_next(slice, &mut end, is_horiz_blank);
145 // refetch
157 // refetch
146− let ch = slice.char(pos);
158+ let ch = slice.char(end);
148 if is_word(ch) {
160 if is_word(ch) {
149− skip_over_next(slice, &mut pos, is_word);
161+ skip_over_next(slice, &mut end, is_word);
150 } else if ch.is_ascii_punctuation() {
162 } else if ch.is_ascii_punctuation() {
151− skip_over_next(slice, &mut pos, |ch| ch.is_ascii_punctuation());
163+ skip_over_next(slice, &mut end, |ch| ch.is_ascii_punctuation());
156− pos
167+ Some(Range::new(begin, end - 1))
159// ---- util ------------
170// ---- util ------------
@@ -164,6 +175,10 @@ fn is_word(ch: char) -> bool {
164 ch.is_alphanumeric() || ch == '_'
175 ch.is_alphanumeric() || ch == '_'
178+fn is_horiz_blank(ch: char) -> bool {
179+ matches!(ch, ' ' | '\t')
167#[derive(Debug, Eq, PartialEq)]
182#[derive(Debug, Eq, PartialEq)]
168enum Category {
183enum Category {
169 Whitespace,
184 Whitespace,
@@ -201,7 +216,8 @@ where
219+/// Returns true if the final pos matches the predicate.
204−pub fn skip_over_prev<F>(slice: RopeSlice, pos: &mut usize, fun: F)
220+pub fn skip_over_prev<F>(slice: RopeSlice, pos: &mut usize, fun: F) -> bool
206 F: Fn(char) -> bool,
222 F: Fn(char) -> bool,
@@ -214,6 +230,7 @@ where
215 *pos = pos.saturating_sub(1);
231 *pos = pos.saturating_sub(1);
233+ return fun(slice.char(*pos));
219#[cfg(test)]
236#[cfg(test)]