@@ -3,8 +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,
711 graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
12+ line_ending::{get_line_ending, line_end_char_index},
813 pos_at_coords, Position, Range, RopeSlice,
914};
1015
@@ -37,9 +42,8 @@ pub fn move_horizontally(
3742 nth_prev_grapheme_boundary(slice, pos, count).max(start)
3843 }
3944 Direction::Forward => {
40− // Line end is pos at the start of next line - 1
41− let end = slice.line_to_char(line + 1).saturating_sub(1);
42− nth_next_grapheme_boundary(slice, pos, count).min(end)
45+ let end_char_idx = line_end_char_index(&slice, line);
46+ nth_next_grapheme_boundary(slice, pos, count).min(end_char_idx)
4347 }
4448 };
4549 let anchor = match behaviour {
@@ -68,8 +72,11 @@ pub fn move_vertically(
6872 ),
6973 };
7074
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);
75+ // Length of the line sans line-ending.
76+ let new_line_len = {
77+ let line = slice.line(new_line);
78+ line.len_chars() - get_line_ending(&line).map(|le| le.len_chars()).unwrap_or(0)
79+ };
7380
7481 let new_col = std::cmp::min(horiz as usize, new_line_len);
7582
@@ -104,64 +111,6 @@ fn word_move(slice: RopeSlice, mut range: Range, count: usize, target: WordMotio
104111}
105112
106113// ---- 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−}
165114
166115#[inline]
167116/// Returns first index that doesn't satisfy a given predicate when
@@ -235,7 +184,8 @@ impl CharHelpers for Chars<'_> {
235184 let mut phase = WordMotionPhase::Start;
236185 let mut head = origin.head;
237186 let mut anchor: Option<usize> = None;
238− let is_boundary = |a: char, b: Option<char>| categorize(a) != categorize(b.unwrap_or(a));
187+ let is_boundary =
188+ |a: char, b: Option<char>| categorize_char(a) != categorize_char(b.unwrap_or(a));
239189 while let Some(peek) = characters.peek().copied() {
240190 phase = match phase {
241191 WordMotionPhase::Start => {
@@ -244,7 +194,8 @@ impl CharHelpers for Chars<'_> {
244194 break; // We're at the end, so there's nothing to do.
245195 }
246196 // 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) {
197+ if !is_boundary(peek, characters.peek().copied()) && !char_is_line_ending(peek)
198+ {
248199 anchor = Some(head);
249200 }
250201 // First character is always skipped by the head
@@ -252,7 +203,7 @@ impl CharHelpers for Chars<'_> {
252203 WordMotionPhase::SkipNewlines
253204 }
254205 WordMotionPhase::SkipNewlines => {
255− if is_end_of_line(peek) {
206+ if char_is_line_ending(peek) {
256207 characters.next();
257208 if characters.peek().is_some() {
258209 advance(&mut head);
@@ -286,12 +237,12 @@ fn reached_target(target: WordMotionTarget, peek: char, next_peek: Option<&char>
286237
287238 match target {
288239 WordMotionTarget::NextWordStart => {
289− ((categorize(peek) != categorize(*next_peek))
290− && (is_end_of_line(*next_peek) || !next_peek.is_whitespace()))
240+ ((categorize_char(peek) != categorize_char(*next_peek))
241+ && (char_is_line_ending(*next_peek) || !next_peek.is_whitespace()))
291242 }
292243 WordMotionTarget::NextWordEnd | WordMotionTarget::PrevWordStart => {
293− ((categorize(peek) != categorize(*next_peek))
294− && (!peek.is_whitespace() || is_end_of_line(*next_peek)))
244+ ((categorize_char(peek) != categorize_char(*next_peek))
245+ && (!peek.is_whitespace() || char_is_line_ending(*next_peek)))
295246 }
296247 }
297248}
@@ -330,7 +281,7 @@ mod test {
330281 slice,
331282 move_vertically(slice, range, Direction::Forward, 1, Movement::Move).head
332283 ),
333− (1, 2).into()
284+ (1, 3).into()
334285 );
335286 }
336287
@@ -343,12 +294,12 @@ mod test {
343294 let mut range = Range::point(position);
344295
345296 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)),
297+ ((Direction::Forward, 1usize), (0, 1)), // T|his is a simple alphabetic line
298+ ((Direction::Forward, 2usize), (0, 3)), // Thi|s is a simple alphabetic line
299+ ((Direction::Forward, 0usize), (0, 3)), // Thi|s is a simple alphabetic line
300+ ((Direction::Forward, 999usize), (0, 32)), // This is a simple alphabetic line|
301+ ((Direction::Forward, 999usize), (0, 32)), // This is a simple alphabetic line|
302+ ((Direction::Backward, 999usize), (0, 0)), // |This is a simple alphabetic line
352303 ];
353304
354305 for ((direction, amount), coordinates) in IntoIter::new(moves_and_expected_coordinates) {
@@ -366,15 +317,15 @@ mod test {
366317 let mut range = Range::point(position);
367318
368319 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_
320+ ((Direction::Forward, 1usize), (0, 1)), // M|ultiline\n
321+ ((Direction::Forward, 2usize), (0, 3)), // Mul|tiline\n
322+ ((Direction::Backward, 6usize), (0, 0)), // |Multiline\n
323+ ((Direction::Backward, 999usize), (0, 0)), // |Multiline\n
324+ ((Direction::Forward, 3usize), (0, 3)), // Mul|tiline\n
325+ ((Direction::Forward, 0usize), (0, 3)), // Mul|tiline\n
326+ ((Direction::Backward, 0usize), (0, 3)), // Mul|tiline\n
327+ ((Direction::Forward, 999usize), (0, 9)), // Multiline|\n
328+ ((Direction::Forward, 999usize), (0, 9)), // Multiline|\n
378329 ]);
379330
380331 for ((direction, amount), coordinates) in moves_and_expected_coordinates {
@@ -446,7 +397,7 @@ mod test {
446397 // First descent preserves column as the target line is wider
447398 ((Axis::V, Direction::Forward, 1usize), (1, 8)),
448399 // Second descent clamps column as the target line is shorter
449− ((Axis::V, Direction::Forward, 1usize), (2, 4)),
400+ ((Axis::V, Direction::Forward, 1usize), (2, 5)),
450401 // Third descent restores the original column
451402 ((Axis::V, Direction::Forward, 1usize), (3, 8)),
452403 // Behaviour is preserved even through long jumps
@@ -760,45 +711,4 @@ mod test {
760711 }
761712 }
762713 }
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− }
804714}