Skip to content

Commit

Permalink
Update text_cursor_state.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rustbasic authored Apr 14, 2024
1 parent e46947f commit 29d5ce3
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions crates/egui/src/text_selection/text_cursor_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -259,7 +260,7 @@ fn next_word_boundary_char_index(it: impl Iterator<Item = char>, 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;
Expand Down Expand Up @@ -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'
}
Expand Down

0 comments on commit 29d5ce3

Please sign in to comment.