From 29d5ce38b27d00c98c4db6da20453308d45a7507 Mon Sep 17 00:00:00 2001 From: rustbasic <127506429+rustbasic@users.noreply.github.com> Date: Sun, 14 Apr 2024 13:50:24 +0900 Subject: [PATCH] Update text_cursor_state.rs --- .../src/text_selection/text_cursor_state.rs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/egui/src/text_selection/text_cursor_state.rs b/crates/egui/src/text_selection/text_cursor_state.rs index 7aca96f8b19..c9b393a45a5 100644 --- a/crates/egui/src/text_selection/text_cursor_state.rs +++ b/crates/egui/src/text_selection/text_cursor_state.rs @@ -157,15 +157,16 @@ fn select_word_at(text: &str, ccursor: CCursor) -> CCursorRange { let mut it = it.skip(ccursor.index - 1); if let Some(char_before_cursor) = it.next() { if let Some(char_after_cursor) = it.next() { - if is_word_char(char_before_cursor) && is_word_char(char_after_cursor) { + if is_string_component(char_before_cursor) && is_string_component(char_after_cursor) + { let min = ccursor_previous_word(text, ccursor + 1); let max = ccursor_next_word(text, min); CCursorRange::two(min, max) - } else if is_word_char(char_before_cursor) { + } else if is_string_component(char_before_cursor) { let min = ccursor_previous_word(text, ccursor); let max = ccursor_next_word(text, min); CCursorRange::two(min, max) - } else if is_word_char(char_after_cursor) { + } else if is_string_component(char_after_cursor) { let max = ccursor_next_word(text, ccursor); CCursorRange::two(ccursor, max) } else { @@ -259,7 +260,7 @@ fn next_word_boundary_char_index(it: impl Iterator, mut index: usiz if let Some(second) = it.next() { index += 1; for next in it { - if is_word_char(next) != is_word_char(second) { + if is_string_component(next) != is_string_component(second) { break; } index += 1; @@ -291,6 +292,18 @@ pub fn is_word_char(c: char) -> bool { c.is_ascii_alphanumeric() || c == '_' } +/// Determines whether a character is a component of a string. +/// +/// Punctuation marks that are commonly used with strings are considered string components. +pub fn is_string_component(c: char) -> bool { + if c == '_' || c == '-' || c == ':' || c == '/' || c == '.' || c == '\\' || c == '@' || c == '#' + { + return true; + } + + !c.is_ascii_whitespace() && !c.is_ascii_punctuation() +} + fn is_linebreak(c: char) -> bool { c == '\r' || c == '\n' }