@@ -3,9 +3,13 @@ use std::iter::{self, from_fn, Peekable, SkipWhile};
33use ropey::iter::Chars;
44
55use crate::{
6+ chars::{
7+ categorize_char, char_is_line_ending, char_is_punctuation, char_is_whitespace,
8+ char_is_word, CharCategory,
9+ },
610 coords_at_pos, get_line_ending,
711 graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
8− line_end, pos_at_coords, Position, Range, RopeSlice,
12+ line_end_char_index, pos_at_coords, Position, Range, RopeSlice,
913};
1014
1115#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@@ -37,9 +41,8 @@ pub fn move_horizontally(
3741 nth_prev_grapheme_boundary(slice, pos, count).max(start)
3842 }
3943 Direction::Forward => {
40− // Line end is pos at the start of next line - 1
41− let end = line_end(&slice, line);
42− nth_next_grapheme_boundary(slice, pos, count).min(end)
44+ let end_char_idx = line_end_char_index(&slice, line);
45+ nth_next_grapheme_boundary(slice, pos, count).min(end_char_idx)
4346 }
4447 };
4548 let anchor = match behaviour {
@@ -68,8 +71,11 @@ pub fn move_vertically(
6871 ),
6972 };
7073
71− // convert to 0-indexed, subtract another 1 because len_chars() counts \n
72− let new_line_len = slice.line(new_line).len_chars().saturating_sub(2);
74+ // Length of the line sans line-ending.
75+ let new_line_len = {
76+ let line = slice.line(new_line);
77+ line.len_chars() - get_line_ending(&line).map(|le| le.len_chars()).unwrap_or(0)
78+ };
7379
7480 let new_col = std::cmp::min(horiz as usize, new_line_len);
7581
@@ -104,64 +110,6 @@ fn word_move(slice: RopeSlice, mut range: Range, count: usize, target: WordMotio
104110}
105111
106112// ---- util ------------
107−#[inline]
108−pub(crate) fn is_word(ch: char) -> bool {
109− ch.is_alphanumeric() || ch == '_'
110−}
111−
112−#[inline]
113−pub(crate) fn is_end_of_line(ch: char) -> bool {
114− ch == '\n'
115−}
116−
117−#[inline]
118−// Whitespace, but not end of line
119−pub(crate) fn is_strict_whitespace(ch: char) -> bool {
120− ch.is_whitespace() && !is_end_of_line(ch)
121−}
122−
123−#[inline]
124−pub(crate) fn is_punctuation(ch: char) -> bool {
125− use unicode_general_category::{get_general_category, GeneralCategory};
126−
127− matches!(
128− get_general_category(ch),
129− GeneralCategory::OtherPunctuation
130− | GeneralCategory::OpenPunctuation
131− | GeneralCategory::ClosePunctuation
132− | GeneralCategory::InitialPunctuation
133− | GeneralCategory::FinalPunctuation
134− | GeneralCategory::ConnectorPunctuation
135− | GeneralCategory::DashPunctuation
136− | GeneralCategory::MathSymbol
137− | GeneralCategory::CurrencySymbol
138− | GeneralCategory::ModifierSymbol
139− )
140−}
141−
142−#[derive(Debug, Eq, PartialEq)]
143−pub enum Category {
144− Whitespace,
145− Eol,
146− Word,
147− Punctuation,
148− Unknown,
149−}
150−
151−#[inline]
152−pub(crate) fn categorize(ch: char) -> Category {
153− if is_end_of_line(ch) {
154− Category::Eol
155− } else if ch.is_whitespace() {
156− Category::Whitespace
157− } else if is_word(ch) {
158− Category::Word
159− } else if is_punctuation(ch) {
160− Category::Punctuation
161− } else {
162− Category::Unknown
163− }
164−}
165113
166114#[inline]
167115/// Returns first index that doesn't satisfy a given predicate when
@@ -235,7 +183,8 @@ impl CharHelpers for Chars<'_> {
235183 let mut phase = WordMotionPhase::Start;
236184 let mut head = origin.head;
237185 let mut anchor: Option<usize> = None;
238− let is_boundary = |a: char, b: Option<char>| categorize(a) != categorize(b.unwrap_or(a));
186+ let is_boundary =
187+ |a: char, b: Option<char>| categorize_char(a) != categorize_char(b.unwrap_or(a));
239188 while let Some(peek) = characters.peek().copied() {
240189 phase = match phase {
241190 WordMotionPhase::Start => {
@@ -244,7 +193,8 @@ impl CharHelpers for Chars<'_> {
244193 break; // We're at the end, so there's nothing to do.
245194 }
246195 // Anchor may remain here if the head wasn't at a boundary
247− if !is_boundary(peek, characters.peek().copied()) && !is_end_of_line(peek) {
196+ if !is_boundary(peek, characters.peek().copied()) && !char_is_line_ending(peek)
197+ {
248198 anchor = Some(head);
249199 }
250200 // First character is always skipped by the head
@@ -252,7 +202,7 @@ impl CharHelpers for Chars<'_> {
252202 WordMotionPhase::SkipNewlines
253203 }
254204 WordMotionPhase::SkipNewlines => {
255− if is_end_of_line(peek) {
205+ if char_is_line_ending(peek) {
256206 characters.next();
257207 if characters.peek().is_some() {
258208 advance(&mut head);
@@ -286,12 +236,12 @@ fn reached_target(target: WordMotionTarget, peek: char, next_peek: Option<&char>
286236
287237 match target {
288238 WordMotionTarget::NextWordStart => {
289− ((categorize(peek) != categorize(*next_peek))
290− && (is_end_of_line(*next_peek) || !next_peek.is_whitespace()))
239+ ((categorize_char(peek) != categorize_char(*next_peek))
240+ && (char_is_line_ending(*next_peek) || !next_peek.is_whitespace()))
291241 }
292242 WordMotionTarget::NextWordEnd | WordMotionTarget::PrevWordStart => {
293− ((categorize(peek) != categorize(*next_peek))
294− && (!peek.is_whitespace() || is_end_of_line(*next_peek)))
243+ ((categorize_char(peek) != categorize_char(*next_peek))
244+ && (!peek.is_whitespace() || char_is_line_ending(*next_peek)))
295245 }
296246 }
297247}
@@ -330,7 +280,7 @@ mod test {
330280 slice,
331281 move_vertically(slice, range, Direction::Forward, 1, Movement::Move).head
332282 ),
333− (1, 2).into()
283+ (1, 3).into()
334284 );
335285 }
336286
@@ -343,12 +293,12 @@ mod test {
343293 let mut range = Range::point(position);
344294
345295 let moves_and_expected_coordinates = [
346− ((Direction::Forward, 1usize), (0, 1)),
347− ((Direction::Forward, 2usize), (0, 3)),
348− ((Direction::Forward, 0usize), (0, 3)),
349− ((Direction::Forward, 999usize), (0, 31)),
350− ((Direction::Forward, 999usize), (0, 31)),
351− ((Direction::Backward, 999usize), (0, 0)),
296+ ((Direction::Forward, 1usize), (0, 1)), // T|his is a simple alphabetic line
297+ ((Direction::Forward, 2usize), (0, 3)), // Thi|s is a simple alphabetic line
298+ ((Direction::Forward, 0usize), (0, 3)), // Thi|s is a simple alphabetic line
299+ ((Direction::Forward, 999usize), (0, 32)), // This is a simple alphabetic line|
300+ ((Direction::Forward, 999usize), (0, 32)), // This is a simple alphabetic line|
301+ ((Direction::Backward, 999usize), (0, 0)), // |This is a simple alphabetic line
352302 ];
353303
354304 for ((direction, amount), coordinates) in IntoIter::new(moves_and_expected_coordinates) {
@@ -366,15 +316,15 @@ mod test {
366316 let mut range = Range::point(position);
367317
368318 let moves_and_expected_coordinates = IntoIter::new([
369− ((Direction::Forward, 1usize), (0, 1)), // M_ltiline
370− ((Direction::Forward, 2usize), (0, 3)), // Mul_iline
371− ((Direction::Backward, 6usize), (0, 0)), // _ultiline
372− ((Direction::Backward, 999usize), (0, 0)), // _ultiline
373− ((Direction::Forward, 3usize), (0, 3)), // Mul_iline
374− ((Direction::Forward, 0usize), (0, 3)), // Mul_iline
375− ((Direction::Backward, 0usize), (0, 3)), // Mul_iline
376− ((Direction::Forward, 999usize), (0, 9)), // Multilin_
377− ((Direction::Forward, 999usize), (0, 9)), // Multilin_
319+ ((Direction::Forward, 1usize), (0, 1)), // M|ultiline\n
320+ ((Direction::Forward, 2usize), (0, 3)), // Mul|tiline\n
321+ ((Direction::Backward, 6usize), (0, 0)), // |Multiline\n
322+ ((Direction::Backward, 999usize), (0, 0)), // |Multiline\n
323+ ((Direction::Forward, 3usize), (0, 3)), // Mul|tiline\n
324+ ((Direction::Forward, 0usize), (0, 3)), // Mul|tiline\n
325+ ((Direction::Backward, 0usize), (0, 3)), // Mul|tiline\n
326+ ((Direction::Forward, 999usize), (0, 9)), // Multiline|\n
327+ ((Direction::Forward, 999usize), (0, 9)), // Multiline|\n
378328 ]);
379329
380330 for ((direction, amount), coordinates) in moves_and_expected_coordinates {
@@ -446,7 +396,7 @@ mod test {
446396 // First descent preserves column as the target line is wider
447397 ((Axis::V, Direction::Forward, 1usize), (1, 8)),
448398 // Second descent clamps column as the target line is shorter
449− ((Axis::V, Direction::Forward, 1usize), (2, 4)),
399+ ((Axis::V, Direction::Forward, 1usize), (2, 5)),
450400 // Third descent restores the original column
451401 ((Axis::V, Direction::Forward, 1usize), (3, 8)),
452402 // Behaviour is preserved even through long jumps
@@ -760,45 +710,4 @@ mod test {
760710 }
761711 }
762712 }
763−
764− #[test]
765− fn test_categorize() {
766− const WORD_TEST_CASE: &'static str =
767− "_hello_world_あいうえおー12345678901234567890";
768− const PUNCTUATION_TEST_CASE: &'static str =
769− "!\"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~!”#$%&’()*+、。:;<=>?@「」^`{|}~";
770− const WHITESPACE_TEST_CASE: &'static str = " ";
771−
772− assert_eq!(Category::Eol, categorize('\n'));
773−
774− for ch in WHITESPACE_TEST_CASE.chars() {
775− assert_eq!(
776− Category::Whitespace,
777− categorize(ch),
778− "Testing '{}', but got `{:?}` instead of `Category::Whitespace`",
779− ch,
780− categorize(ch)
781− );
782− }
783−
784− for ch in WORD_TEST_CASE.chars() {
785− assert_eq!(
786− Category::Word,
787− categorize(ch),
788− "Testing '{}', but got `{:?}` instead of `Category::Word`",
789− ch,
790− categorize(ch)
791− );
792− }
793−
794− for ch in PUNCTUATION_TEST_CASE.chars() {
795− assert_eq!(
796− Category::Punctuation,
797− categorize(ch),
798− "Testing '{}', but got `{:?}` instead of `Category::Punctuation`",
799− ch,
800− categorize(ch)
801− );
802− }
803− }
804713}