Skip to content

Commit cefb611

Browse files
authored
fix: use LF instead of CRLF in insert_newline on Windows (#1023)
* fix: use LF instead of CRLF in insert_newline on Windows * test: lf works for windows
1 parent bdcc842 commit cefb611

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/core_editor/line_buffer.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,13 @@ impl LineBuffer {
422422
self.insertion_point = self.insertion_point() + string.len();
423423
}
424424

425-
/// Inserts the system specific new line character
425+
/// Inserts a newline character (`'\n'`) into the buffer at the current
426+
/// insertion point.
426427
///
427-
/// - On Unix systems LF (`"\n"`)
428-
/// - On Windows CRLF (`"\r\n"`)
428+
/// Only LF is inserted regardless of platform. The painting layer
429+
/// ([`coerce_crlf`]) is responsible for converting LF to CRLF when
430+
/// writing to the terminal in raw mode.
429431
pub fn insert_newline(&mut self) {
430-
#[cfg(target_os = "windows")]
431-
self.insert_str("\r\n");
432-
#[cfg(not(target_os = "windows"))]
433432
self.insert_char('\n');
434433
}
435434

@@ -1129,6 +1128,32 @@ mod test {
11291128
line_buffer.assert_valid();
11301129
}
11311130

1131+
#[rstest]
1132+
#[case("hello", 5, "hello\n", 6)]
1133+
#[case("hello", 0, "\nhello", 1)]
1134+
#[case("hello", 3, "hel\nlo", 4)]
1135+
#[case("line1\nline2", 11, "line1\nline2\n", 12)]
1136+
#[case("", 0, "\n", 1)]
1137+
fn insert_newline_inserts_lf_only(
1138+
#[case] input: &str,
1139+
#[case] in_location: usize,
1140+
#[case] output: &str,
1141+
#[case] out_location: usize,
1142+
) {
1143+
let mut line_buffer = buffer_with(input);
1144+
line_buffer.set_insertion_point(in_location);
1145+
1146+
line_buffer.insert_newline();
1147+
1148+
assert_eq!(line_buffer.get_buffer(), output);
1149+
assert!(
1150+
!line_buffer.get_buffer().contains('\r'),
1151+
"Buffer should never contain CR"
1152+
);
1153+
assert_eq!(line_buffer.insertion_point(), out_location);
1154+
line_buffer.assert_valid();
1155+
}
1156+
11321157
#[rstest]
11331158
#[case("new string", 10)]
11341159
#[case("new line1\nnew line 2", 20)]

0 commit comments

Comments
 (0)