@@ -1,7 +1,8 @@
11use helix_core::{
22 comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, indent,
33 line_ending::{
4− get_line_ending, get_line_ending_of_str, line_end_char_index, str_is_line_ending,
4+ get_line_ending, get_line_ending_of_str, line_end_char_index, rope_end_without_line_ending,
5+ str_is_line_ending,
56 },
67 match_brackets,
78 movement::{self, Direction},
@@ -612,7 +613,7 @@ fn replace(cx: &mut Context) {
612613 if let Some(ch) = ch {
613614 let transaction =
614615 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
615− let max_to = doc.text().len_chars().saturating_sub(1);
616+ let max_to = rope_end_without_line_ending(&doc.text().slice(..));
616617 let to = std::cmp::min(max_to, range.to() + 1);
617618 let text: String = RopeGraphemes::new(doc.text().slice(range.from()..to))
618619 .map(|g| {
@@ -769,7 +770,7 @@ fn extend_line_start(cx: &mut Context) {
769770fn select_all(cx: &mut Context) {
770771 let (view, doc) = current!(cx.editor);
771772
772− let end = doc.text().len_chars().saturating_sub(1);
773+ let end = rope_end_without_line_ending(&doc.text().slice(..));
773774 doc.set_selection(view.id, Selection::single(0, end))
774775}
775776
@@ -900,13 +901,13 @@ fn extend_line(cx: &mut Context) {
900901
901902 let line_start = text.char_to_line(pos.anchor);
902903 let mut line = text.char_to_line(pos.head);
903− let line_end = text.line_to_char(line + 1).saturating_sub(1);
904− if line_start <= pos.anchor && pos.head == line_end && line != text.len_lines() {
904+ let line_end = line_end_char_index(&text.slice(..), line);
905+ if line_start <= pos.anchor && pos.head == line_end && line < (text.len_lines() - 2) {
905906 line += 1;
906907 }
907908
908909 let start = text.line_to_char(line_start);
909− let end = text.line_to_char(line + 1).saturating_sub(1);
910+ let end = line_end_char_index(&text.slice(..), line);
910911
911912 doc.set_selection(view.id, Selection::single(start, end));
912913}
@@ -924,13 +925,9 @@ fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId
924925 // then delete
925926 let transaction =
926927 Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
927− let alltext = doc.text();
928+ let alltext = doc.text().slice(..);
928929 let line = alltext.char_to_line(range.head);
929− let max_to = doc.text().len_chars().saturating_sub(
930− get_line_ending(&alltext.line(line))
931− .map(|le| le.len_chars())
932− .unwrap_or(0),
933− );
930+ let max_to = rope_end_without_line_ending(&alltext);
934931 let to = std::cmp::min(max_to, range.to() + 1);
935932 (range.from(), to, None)
936933 });
@@ -1348,7 +1345,7 @@ mod cmd {
13481345 Ok(contents) => {
13491346 let transaction =
13501347 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
1351− let max_to = doc.text().len_chars().saturating_sub(1);
1348+ let max_to = rope_end_without_line_ending(&doc.text().slice(..));
13521349 let to = std::cmp::min(max_to, range.to() + 1);
13531350 (range.from(), to, Some(contents.as_str().into()))
13541351 });
@@ -1831,7 +1828,10 @@ fn open(cx: &mut Context, open: Open) {
18311828 };
18321829
18331830 // insert newlines after this index for both Above and Below variants
1834− let linend_index = doc.text().line_to_char(line).saturating_sub(1);
1831+ let linend_index = doc.text().line_to_char(line)
1832+ - get_line_ending(&doc.text().line(line))
1833+ .map(|le| le.len_chars())
1834+ .unwrap_or(0);
18351835
18361836 // TODO: share logic with insert_newline for indentation
18371837 let indent_level = indent::suggested_indent_for_pos(
@@ -2702,7 +2702,7 @@ fn replace_with_yanked(cx: &mut Context) {
27022702 if let Some(yank) = values.first() {
27032703 let transaction =
27042704 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
2705− let max_to = doc.text().len_chars().saturating_sub(1);
2705+ let max_to = rope_end_without_line_ending(&doc.text().slice(..));
27062706 let to = std::cmp::min(max_to, range.to() + 1);
27072707 (range.from(), to, Some(yank.as_str().into()))
27082708 });
@@ -2720,7 +2720,7 @@ fn replace_selections_with_clipboard_impl(editor: &mut Editor) {
27202720 Ok(contents) => {
27212721 let transaction =
27222722 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
2723− let max_to = doc.text().len_chars().saturating_sub(1);
2723+ let max_to = rope_end_without_line_ending(&doc.text().slice(..));
27242724 let to = std::cmp::min(max_to, range.to() + 1);
27252725 (range.from(), to, Some(contents.as_str().into()))
27262726 });
@@ -2906,8 +2906,8 @@ fn join_selections(cx: &mut Context) {
29062906 changes.reserve(lines.len());
29072907
29082908 for line in lines {
2909− let mut start = text.line_to_char(line + 1).saturating_sub(1);
2910− let mut end = start + 1;
2909+ let mut start = line_end_char_index(&slice, line);
2910+ let mut end = text.line_to_char(line + 1);
29112911 end = skip_while(slice, end, |ch| matches!(ch, ' ' | '\t')).unwrap_or(end);
29122912
29132913 // need to skip from start, not end
@@ -3371,11 +3371,7 @@ fn surround_add(cx: &mut Context) {
33713371 for (i, range) in selection.iter().enumerate() {
33723372 let from = range.from();
33733373 let line = text.char_to_line(range.to());
3374− let max_to = doc.text().len_chars().saturating_sub(
3375− get_line_ending(&text.line(line))
3376− .map(|le| le.len_chars())
3377− .unwrap_or(0),
3378− );
3374+ let max_to = rope_end_without_line_ending(&text);
33793375 let to = std::cmp::min(range.to() + 1, max_to);
33803376
33813377 changes.push((from, from, Some(Tendril::from_char(open))));