Skip to content

Commit

Permalink
fix(evil): avoid panic when deleting a character (x) at the EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
usagi-flow committed Jun 1, 2024
1 parent 4676b91 commit 47dea64
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions helix-term/src/commands/evil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ impl EvilCommands {

fn get_character_based_selection(cx: &mut Context) -> Selection {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);

// For each cursor, select one or more characters forward or backward according
// to the count in the evil context and the motion respectively.
Expand All @@ -250,15 +251,15 @@ impl EvilCommands {

let head = head + count;

Range::new(anchor, head)
Range::new(text.len_chars().min(anchor), text.len_chars().min(head))
});
}

fn get_bidirectional_word_based_selection(cx: &mut Context) -> Result<Selection, String> {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);

Ok(doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..);
let range = move_prev_word_start(text, range, 1);
let range = move_next_word_end(text, range, 1);
return range;
Expand All @@ -268,6 +269,7 @@ impl EvilCommands {
fn get_word_based_selection(cx: &mut Context, motion: &Motion) -> Result<Selection, String> {
let (view, doc) = current!(cx.editor);
let mut error: Option<String> = None;
let text = doc.text().slice(..);

// For each cursor, select one or more words forward or backward according
// to the count in the evil context and the motion respectively.
Expand All @@ -281,8 +283,6 @@ impl EvilCommands {
}
};

let text = doc.text().slice(..);

let char_current = text.char(range.anchor);
let char_previous = match range.anchor > 0 {
true => Some(text.char(range.anchor - 1)),
Expand Down Expand Up @@ -327,7 +327,10 @@ impl EvilCommands {
false => move_prev_word_start(text, range, count),
};

Range::new(anchor, range.head)
Range::new(
text.len_chars().min(anchor),
text.len_chars().min(range.head),
)
});

if error.is_none() {
Expand Down

0 comments on commit 47dea64

Please sign in to comment.