diff options
author | Blaž Hrastnik | 2020-09-28 16:11:17 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2020-09-28 16:11:17 +0000 |
commit | 13d1ea542e142e130a29266ad1414991e4d2e1e0 (patch) | |
tree | 4215a7dc01a6a66a23359bfee93352b486234165 /helix-core | |
parent | 36e7e2133fe1d472600cfd935b8046b8d50146c2 (diff) |
Clamp character movement to line.
Diffstat (limited to 'helix-core')
-rw-r--r-- | helix-core/src/state.rs | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/helix-core/src/state.rs b/helix-core/src/state.rs index cac52abc..7cecba29 100644 --- a/helix-core/src/state.rs +++ b/helix-core/src/state.rs @@ -147,12 +147,23 @@ impl State { ) -> usize { let text = &self.doc; match (dir, granularity) { - // TODO: clamp movement to line, prevent moving onto \n at the end (Direction::Backward, Granularity::Character) => { - nth_prev_grapheme_boundary(&text.slice(..), pos, count) + // Clamp to line + let line = text.char_to_line(pos); + let start = text.line_to_char(line); + std::cmp::max( + nth_prev_grapheme_boundary(&text.slice(..), pos, count), + start, + ) } (Direction::Forward, Granularity::Character) => { - nth_next_grapheme_boundary(&text.slice(..), pos, count) + // Clamp to line + let line = text.char_to_line(pos); + let start = text.line_to_char(line); + let len = text.line(line).len_chars(); + // convert to 0-indexed, subtract another 1 because len_chars() counts \n + let end = start + len.saturating_sub(2); + std::cmp::min(nth_next_grapheme_boundary(&text.slice(..), pos, count), end) } (Direction::Forward, Granularity::Word) => { Self::move_next_word_start(&text.slice(..), pos) |