Add bounds checks to replace
commit2e4a338944d31e77b4db5dd7d578aacedef3256f
authorWojciech Kępka <wojtek.kepka@protonmail.com>
dateMon 7 Jun 2021, 19:03 (5 years ago)
parent9c83a98
helix-term/src/commands.rs+4−2
@@ -472,10 +472,12 @@ pub fn replace(cx: &mut Context) {
472472
473473 let transaction =
474474 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
475+ let max_to = doc.text().len_chars().saturating_sub(1);
476+ let to = std::cmp::min(max_to, range.to() + 1);
475477 (
476478 range.from(),
477− range.to() + 1,
478− Some(text.repeat(range.to() - range.from() + 1).into()),
479+ to,
480+ Some(text.repeat(to - range.from()).into()),
479481 )
480482 });
481483