on master help sign in
Fix crash if appending at end of line on the last line of the file
f99a683Blaž Hrastnik
helix-term/src/commands.rs+4−5
@@ -1145,15 +1145,14 @@ pub fn prepend_to_line(cx: &mut Context) {
1145
1145
1146// A inserts at the end of each line with a selection
1146// A inserts at the end of each line with a selection
1147pub fn append_to_line(cx: &mut Context) {
1147pub fn append_to_line(cx: &mut Context) {
1148− move_line_end(cx);
1149−
1150 let (view, doc) = cx.current();
1148 let (view, doc) = cx.current();
1151 enter_insert_mode(doc);
1149 enter_insert_mode(doc);
1152
1150
1153− // offset by another 1 char since move_line_end will position on the last char, we want to
1154− // append past that
1155 let selection = doc.selection(view.id).transform(|range| {
1151 let selection = doc.selection(view.id).transform(|range| {
1156− let pos = range.head + 1;
1152+ let text = doc.text();
1153+ let line = text.char_to_line(range.head);
1154+ // we can't use line_to_char(line + 1) - 2 because the last line might not contain \n
1155+ let pos = (text.line_to_char(line) + text.line(line).len_chars()).saturating_sub(1);
1157 Range::new(pos, pos)
1156 Range::new(pos, pos)
1158 });
1157 });
1159 doc.set_selection(view.id, selection);
1158 doc.set_selection(view.id, selection);