properly working vim like movements in the read only mode

This commit is contained in:
filipriec
2025-02-25 13:22:52 +01:00
parent 584ee2f651
commit 5e11ea2585

View File

@@ -575,25 +575,46 @@ impl EventHandler {
// Find the end of the current/next word // Find the end of the current/next word
fn find_word_end(&self, text: &str, current_pos: usize) -> usize { fn find_word_end(&self, text: &str, current_pos: usize) -> usize {
let chars: Vec<char> = text.chars().collect(); let chars: Vec<char> = text.chars().collect();
if chars.is_empty() || current_pos >= chars.len() - 1 { if chars.is_empty() {
return if chars.is_empty() { 0 } else { chars.len() - 1 }; 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; let mut pos = current_pos;
// Skip whitespace to find the next word // If we're on whitespace, move forward to the next word
while pos < chars.len() && Self::get_char_type(chars[pos]) == CharType::Whitespace { if Self::get_char_type(chars[pos]) == CharType::Whitespace {
pos += 1; // 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() { // If we've reached the end of the text, return the last position
return pos.saturating_sub(1); 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]); let word_type = Self::get_char_type(chars[pos]);
// Move to the end of this word // 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; pos += 1;
} }