Skip to content

Commit

Permalink
Conserve BOM and properly support UTF16 (#6497)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis-Lapierre authored Apr 30, 2023
1 parent efd09b6 commit b0b3f45
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 37 deletions.
3 changes: 2 additions & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5111,7 +5111,8 @@ async fn shell_impl_async(
let output = if let Some(mut stdin) = process.stdin.take() {
let input_task = tokio::spawn(async move {
if let Some(input) = input {
helix_view::document::to_writer(&mut stdin, encoding::UTF_8, &input).await?;
helix_view::document::to_writer(&mut stdin, (encoding::UTF_8, false), &input)
.await?;
}
Ok::<_, anyhow::Error>(())
});
Expand Down
38 changes: 38 additions & 0 deletions helix-term/tests/test/commands/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,41 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_write_utf_bom_file() -> anyhow::Result<()> {
// "ABC" with utf8 bom
const UTF8_FILE: [u8; 6] = [0xef, 0xbb, 0xbf, b'A', b'B', b'C'];

// "ABC" in UTF16 with bom
const UTF16LE_FILE: [u8; 8] = [0xff, 0xfe, b'A', 0x00, b'B', 0x00, b'C', 0x00];
const UTF16BE_FILE: [u8; 8] = [0xfe, 0xff, 0x00, b'A', 0x00, b'B', 0x00, b'C'];

edit_file_with_content(&UTF8_FILE).await?;
edit_file_with_content(&UTF16LE_FILE).await?;
edit_file_with_content(&UTF16BE_FILE).await?;

Ok(())
}

async fn edit_file_with_content(file_content: &[u8]) -> anyhow::Result<()> {
let mut file = tempfile::NamedTempFile::new()?;

file.as_file_mut().write_all(&file_content)?;

helpers::test_key_sequence(
&mut helpers::AppBuilder::new().build()?,
Some(&format!(":o {}<ret>:x<ret>", file.path().to_string_lossy())),
None,
true,
)
.await?;

file.rewind()?;
let mut new_file_content: Vec<u8> = Vec::new();
file.read_to_end(&mut new_file_content)?;

assert_eq!(file_content, new_file_content);

Ok(())
}
Loading

0 comments on commit b0b3f45

Please sign in to comment.