From 5e11ea2585a8636e5cd435c52ffa4162cd2a3e1b Mon Sep 17 00:00:00 2001 From: filipriec Date: Tue, 25 Feb 2025 13:22:52 +0100 Subject: [PATCH] properly working vim like movements in the read only mode --- client/src/ui/handlers/event.rs | 37 ++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/client/src/ui/handlers/event.rs b/client/src/ui/handlers/event.rs index 5616dab..5f81f5d 100644 --- a/client/src/ui/handlers/event.rs +++ b/client/src/ui/handlers/event.rs @@ -575,25 +575,46 @@ impl EventHandler { // Find the end of the current/next word fn find_word_end(&self, text: &str, current_pos: usize) -> usize { let chars: Vec = text.chars().collect(); - if chars.is_empty() || current_pos >= chars.len() - 1 { - return if chars.is_empty() { 0 } else { chars.len() - 1 }; + if chars.is_empty() { + return 0; + } + + // If already at the end of the text, stay there + if current_pos >= chars.len() - 1 { + return chars.len() - 1; } let mut pos = current_pos; - // Skip whitespace to find the next word - while pos < chars.len() && Self::get_char_type(chars[pos]) == CharType::Whitespace { - pos += 1; + // If we're on whitespace, move forward to the next word + if Self::get_char_type(chars[pos]) == CharType::Whitespace { + // Skip whitespace to find the next word + while pos < chars.len() && Self::get_char_type(chars[pos]) == CharType::Whitespace { + pos += 1; + } + } else { + // We're on a word character - check if we're at the end of the current word + let current_type = Self::get_char_type(chars[pos]); + if pos + 1 < chars.len() && Self::get_char_type(chars[pos + 1]) != current_type { + // We're at the end of the current word, so move to the next word + pos += 1; + // Skip any whitespace + while pos < chars.len() && Self::get_char_type(chars[pos]) == CharType::Whitespace { + pos += 1; + } + } } - if pos == chars.len() { - return pos.saturating_sub(1); + // If we've reached the end of the text, return the last position + if pos >= chars.len() { + return chars.len() - 1; } + // Get the type of the word we're now on let word_type = Self::get_char_type(chars[pos]); // Move to the end of this word - while pos < chars.len() - 1 && Self::get_char_type(chars[pos + 1]) == word_type { + while pos + 1 < chars.len() && Self::get_char_type(chars[pos + 1]) == word_type { pos += 1; }