on master help sign in
Fix open-new-line command for CRLF, as well as other bugs.

Fixes #363. I set out to fix issue #363, but after fixing it discovered some other things were wrong with the command while testing. In summary: - #363 was because it was still assuming a line ending width of 1 char in its indexing calculations, even when actually inserting CRLF. - Aside from #363, it actually needed to set `line_end_index` to zero for *all* calculations that use it when line == 0, but it was only doing so for a single calculation.

commit8935e7a8791eea391a1de41a78278b4bf5dd241d authorNathan Vegdahl <cessen@cessen.com> dateWed 23 Jun 2021, 23:53 (5 years ago) parent394629a
1 file changed+11−7
helix-term/src/commands.rs+11−7
helix-term/src/commands.rs+11−7
@@ -1825,8 +1825,16 @@ fn open(cx: &mut Context, open: Open) {
18251825 Open::Above => line,
18261826 };
18271827
1828− // insert newlines after this index for both Above and Below variants
1829− let line_end_index = line_end_char_index(&doc.text().slice(..), line.saturating_sub(1));
1828+ // Index to insert newlines after, as well as the char width
1829+ // to use to compensate for those inserted newlines.
1830+ let (line_end_index, line_end_offset_width) = if line == 0 {
1831+ (0, 0)
1832+ } else {
1833+ (
1834+ line_end_char_index(&doc.text().slice(..), line.saturating_sub(1)),
1835+ doc.line_ending.len_chars(),
1836+ )
1837+ };
18301838
18311839 // TODO: share logic with insert_newline for indentation
18321840 let indent_level = indent::suggested_indent_for_pos(
@@ -1844,11 +1852,7 @@ fn open(cx: &mut Context, open: Open) {
18441852 let text = text.repeat(count);
18451853
18461854 // calculate new selection ranges
1847− let pos = if line == 0 {
1848− 0 // Required since text will have a min len of 1 (\n)
1849− } else {
1850− offs + line_end_index + 1
1851− };
1855+ let pos = offs + line_end_index + line_end_offset_width;
18521856 for i in 0..count {
18531857 // pos -> beginning of reference line,
18541858 // + (i * (1+indent_len)) -> beginning of i'th line from pos