This commit is contained in:
filipriec
2025-04-02 22:26:35 +02:00
parent 9e36385e63
commit 1a09624242
2 changed files with 82 additions and 42 deletions

View File

@@ -319,11 +319,20 @@ async fn execute_edit_action<S: CanvasState>(
"move_word_end" => {
let current_input = state.get_current_input();
if !current_input.is_empty() {
let new_pos =
find_word_end(current_input, state.current_cursor_pos());
let final_pos = new_pos.min(current_input.len());
state.set_current_cursor_pos(final_pos);
*ideal_cursor_column = final_pos;
let current_pos = state.current_cursor_pos();
let new_pos = find_word_end(current_input, current_pos);
// If already at word end, jump to next word's end
let final_pos = if new_pos == current_pos {
find_word_end(current_input, new_pos + 1)
} else {
new_pos
};
let max_valid_index = current_input.len().saturating_sub(1);
let clamped_pos = final_pos.min(max_valid_index);
state.set_current_cursor_pos(clamped_pos);
*ideal_cursor_column = clamped_pos;
}
Ok("".to_string())
}
@@ -400,19 +409,17 @@ fn find_word_end(text: &str, current_pos: usize) -> usize {
if len == 0 {
return 0;
}
if current_pos >= len {
return len;
let mut pos = current_pos.min(len - 1);
// If already at whitespace, find next word first
if get_char_type(chars[pos]) == CharType::Whitespace {
pos = find_next_word_start(text, pos);
}
let mut pos = current_pos;
if get_char_type(chars[pos]) == CharType::Whitespace {
while pos < len && get_char_type(chars[pos]) == CharType::Whitespace {
pos += 1;
}
if pos == len {
return len;
}
// Now find end of current/next word
if pos >= len {
return len.saturating_sub(1);
}
let word_type = get_char_type(chars[pos]);
@@ -420,11 +427,8 @@ fn find_word_end(text: &str, current_pos: usize) -> usize {
pos += 1;
}
if pos > current_pos && pos > 0 {
pos - 1
} else {
current_pos.min(len.saturating_sub(1))
}
// Return last character of the word
pos.saturating_sub(1).min(len.saturating_sub(1))
}
fn find_prev_word_start(text: &str, current_pos: usize) -> usize {