fix: trim whitespace up to the last selection on insert_newline (#13673)

pull/13403/merge
Ryan Mehri 2025-06-03 09:45:19 -04:00 committed by GitHub
parent 1ea9050a5e
commit 6c43dc4962
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -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() {

View File

@ -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<ret>",
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