1
0
зеркало из https://github.com/glebtv/tui-textarea.git synced 2026-09-03 22:35:50 +03:00

Merge pull request #75 from achristmascarl/cursor-word-end

add `CursorMove` for next end of word
Этот коммит содержится в:
Linda_pp
2024-07-31 22:51:12 +09:00
коммит произвёл GitHub
родитель e61c177cf9 eae80347d9
Коммит d9571ca50e
5 изменённых файлов: 66 добавлений и 2 удалений

Просмотреть файл

@@ -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,20 @@ 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 let Some(col) = (row + 1 < lines.len())
.then(|| find_word_end_next(&lines[row + 1], 0))
.unwrap_or_default()
{
Some((row + 1, col))
} else if row + 1 < lines.len() {
Some((row + 1, lines[row + 1].chars().count().saturating_sub(1)))
} else {
Some((row, lines[row].chars().count().saturating_sub(1)))
}
}
WordForward => {
if let Some(col) = find_word_start_forward(&lines[row], col) {
Some((row, col))

Просмотреть файл

@@ -43,6 +43,26 @@ pub fn find_word_end_forward(line: &str, start_col: usize) -> Option<usize> {
None
}
pub fn find_word_end_next(line: &str, start_col: usize) -> Option<usize> {
let mut it = line.chars().enumerate().skip(start_col);
let (mut cur_col, cur_char) = it.next()?;
let mut cur = CharKind::new(cur_char);
for (next_col, c) in it {
let next = CharKind::new(c);
// if cursor started at the end of a word, don't stop
if next_col.saturating_sub(start_col) > 1 && cur != CharKind::Space && next != cur {
return Some(next_col.saturating_sub(1));
}
cur = next;
cur_col = next_col;
}
// if end of line is whitespace, don't stop the cursor
if cur != CharKind::Space && cur_col.saturating_sub(start_col) >= 1 {
return Some(cur_col);
}
None
}
pub fn find_word_start_backward(line: &str, start_col: usize) -> Option<usize> {
let idx = line
.char_indices()