Add config for trimming trailing whitespace and newlines on write

These match the equivalent options in VSCode. `trim_trailing_whitespace`
is also the name used by EditorConfig.

* `trim-final-newlines` trims any extra line endings after the final one
* `trim-trailing-whitespace` trims any trailing whitespace (but not
  empty lines)
This commit is contained in:
Michael Davis 2025-03-08 12:58:46 -05:00
parent ee9db440ce
commit aa20eb8e7f
No known key found for this signature in database
4 changed files with 116 additions and 0 deletions

View file

@ -420,6 +420,50 @@ async fn test_write_utf_bom_file() -> anyhow::Result<()> {
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_write_trim_trailing_whitespace() -> anyhow::Result<()> {
let mut file = tempfile::NamedTempFile::new()?;
let mut app = helpers::AppBuilder::new()
.with_config(Config {
editor: helix_view::editor::Config {
trim_trailing_whitespace: true,
..Default::default()
},
..Default::default()
})
.with_file(file.path(), None)
.with_input_text("#[f|]#oo \n\n \nbar ")
.build()?;
test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?;
helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("foo\n\n\nbar"))?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_write_trim_final_newlines() -> anyhow::Result<()> {
let mut file = tempfile::NamedTempFile::new()?;
let mut app = helpers::AppBuilder::new()
.with_config(Config {
editor: helix_view::editor::Config {
trim_final_newlines: true,
..Default::default()
},
..Default::default()
})
.with_file(file.path(), None)
.with_input_text("#[f|]#oo\n \n\n\n")
.build()?;
test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?;
helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("foo\n \n"))?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_write_insert_final_newline_added_if_missing() -> anyhow::Result<()> {
let mut file = tempfile::NamedTempFile::new()?;