From 6c43dc496293594e1f9ed31de1f2da0fa5e5a6fd Mon Sep 17 00:00:00 2001 From: Ryan Mehri <52933714+rmehri01@users.noreply.github.com> Date: Tue, 3 Jun 2025 09:45:19 -0400 Subject: [PATCH] fix: trim whitespace up to the last selection on insert_newline (#13673) --- helix-term/src/commands.rs | 4 +++- helix-term/tests/test/commands/insert.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 604d69243..446337411 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4206,6 +4206,7 @@ pub mod insert { None }; + let mut last_pos = 0; let mut transaction = Transaction::change_by_selection(contents, selection, |range| { // Tracks the number of trailing whitespace characters deleted by this selection. let mut chars_deleted = 0; @@ -4227,7 +4228,8 @@ pub mod insert { let (from, to, local_offs) = if let Some(idx) = text.slice(line_start..pos).last_non_whitespace_char() { - let first_trailing_whitespace_char = (line_start + idx + 1).min(pos); + let first_trailing_whitespace_char = (line_start + idx + 1).clamp(last_pos, pos); + last_pos = pos; let line = text.line(current_line); let indent = match line.first_non_whitespace_char() { diff --git a/helix-term/tests/test/commands/insert.rs b/helix-term/tests/test/commands/insert.rs index edec192cc..9499868e2 100644 --- a/helix-term/tests/test/commands/insert.rs +++ b/helix-term/tests/test/commands/insert.rs @@ -172,6 +172,18 @@ async fn insert_newline_trim_trailing_whitespace() -> anyhow::Result<()> { Ok(()) } +#[tokio::test(flavor = "multi_thread")] +async fn insert_newline_trim_whitespace_to_previous_selection() -> anyhow::Result<()> { + test(( + indoc! {"\"#[a|]# #(a|)# #(a|)#\""}, + "c", + indoc! {"\"\n#[\n|]##(\n|)##(\"|)#"}, + )) + .await?; + + Ok(()) +} + #[tokio::test(flavor = "multi_thread")] async fn insert_newline_continue_line_comment() -> anyhow::Result<()> { // `insert_newline` continues a single line comment