use system's appropriate line ending

pull/2359/head
Skyler Hawthorne 3 years ago
parent 28e94fb261
commit ef8fe5a5ce

@ -10,13 +10,14 @@ async fn auto_indent_c() -> anyhow::Result<()> {
Config::default(), Config::default(),
// switches to append mode? // switches to append mode?
( (
"void foo() {#[|}]#\n", helpers::platform_line("void foo() {#[|}]#").as_ref(),
"i<ret><esc>", "i<ret><esc>",
indoc! {"\ helpers::platform_line(indoc! {"\
void foo() { void foo() {
#[|\n]#\ #[|\n]#\
} }
"}, "})
.as_ref(),
), ),
) )
.await?; .await?;

@ -138,3 +138,20 @@ pub fn temp_file_with_contents<S: AsRef<str>>(
temp_file.as_file_mut().sync_all()?; temp_file.as_file_mut().sync_all()?;
Ok(temp_file) Ok(temp_file)
} }
/// Replaces all LF chars with the system's appropriate line feed
/// character, and if one doesn't exist already, appends the system's
/// appropriate line ending to the end of a string.
pub fn platform_line(input: &str) -> String {
let line_end = helix_core::DEFAULT_LINE_ENDING.as_str();
// we can assume that the source files in this code base will always
// be LF, so indoc strings will always insert LF
let mut output = input.replace("\n", line_end);
if !output.ends_with(line_end) {
output.push_str(line_end);
}
output
}

@ -31,7 +31,10 @@ async fn test_write() -> anyhow::Result<()> {
let mut file_content = String::new(); let mut file_content = String::new();
file.as_file_mut().read_to_string(&mut file_content)?; file.as_file_mut().read_to_string(&mut file_content)?;
assert_eq!("i can eat glass, it will not hurt me\n", file_content); assert_eq!(
helpers::platform_line("i can eat glass, it will not hurt me"),
file_content
);
Ok(()) Ok(())
} }

Loading…
Cancel
Save