From 311b185ff2febcfc56ea73cac5137ffe6fa47658 Mon Sep 17 00:00:00 2001 From: achristmascarl Date: Tue, 30 Jul 2024 15:08:29 -0400 Subject: [PATCH] new WordEnd enum member for CursorMove --- src/cursor.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/cursor.rs b/src/cursor.rs index e9b084b..ca28ea4 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -1,5 +1,5 @@ use crate::widget::Viewport; -use crate::word::{find_word_start_backward, find_word_start_forward}; +use crate::word::{find_word_end_next, find_word_start_backward, find_word_start_forward}; #[cfg(feature = "arbitrary")] use arbitrary::Arbitrary; #[cfg(feature = "serde")] @@ -121,6 +121,29 @@ pub enum CursorMove { /// assert_eq!(textarea.cursor(), (0, 8)); /// ``` WordForward, + /// Move cursor forward to the next end of word. Word boundary appears at spaces, punctuations, and others. For example + /// `fn foo(a)` consists of words `fn`, `foo`, `(`, `a`, `)`. When the cursor is at the end of line, it moves to the + /// end of the first word of the next line. + /// ``` + /// use tui_textarea::{TextArea, CursorMove}; + /// + /// let mut textarea = TextArea::from(["aaa bbb (c)", " dd"]); + /// + /// + /// textarea.move_cursor(CursorMove::WordEnd); + /// assert_eq!(textarea.cursor(), (0, 2)); + /// textarea.move_cursor(CursorMove::WordEnd); + /// assert_eq!(textarea.cursor(), (0, 6)); + /// textarea.move_cursor(CursorMove::WordEnd); + /// assert_eq!(textarea.cursor(), (0, 8)); + /// textarea.move_cursor(CursorMove::WordEnd); + /// assert_eq!(textarea.cursor(), (0, 9)); + /// textarea.move_cursor(CursorMove::WordEnd); + /// assert_eq!(textarea.cursor(), (0, 10)); + /// textarea.move_cursor(CursorMove::WordEnd); + /// assert_eq!(textarea.cursor(), (1, 2)); + /// ``` + WordEnd, /// Move cursor backward by one word. Word boundary appears at spaces, punctuations, and others. For example /// `fn foo(a)` consists of words `fn`, `foo`, `(`, `a`, `)`.When the cursor is at the head of line, it moves to /// the end of previous line. @@ -263,6 +286,19 @@ impl CursorMove { let row = lines.len() - 1; Some((row, fit_col(col, &lines[row]))) } + WordEnd => { + if let Some(col) = find_word_end_next(&lines[row], col) { + Some((row, col)) + } else if row + 1 < lines.len() { + if let Some(col) = find_word_end_next(&lines[row + 1], 0) { + Some((row + 1, col)) + } else { + Some((row + 1, lines[row + 1].chars().count())) + } + } else { + Some((row, lines[row].chars().count())) + } + } WordForward => { if let Some(col) = find_word_start_forward(&lines[row], col) { Some((row, col))