@@ -472,10 +472,12 @@ pub fn replace(cx: &mut Context) {
473 let transaction =
473 let transaction =
474 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
474 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);
476 range.from(),
478 range.from(),
477− range.to() + 1,
479+ to,
478− Some(text.repeat(range.to() - range.from() + 1).into()),
480+ Some(text.repeat(to - range.from()).into()),