From bbd7cb7bfb3af6206d27cab181d2d17508438834 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Sun, 2 Feb 2025 09:31:43 +0000 Subject: [PATCH] fix: incorrect order of index additions --- helix-core/src/comment.rs | 5 +-- helix-term/tests/test/commands.rs | 58 +++++++++++++++++++++++++++++++ lol.html | 4 +-- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs index 5a458ac2d..e5a17a79d 100644 --- a/helix-core/src/comment.rs +++ b/helix-core/src/comment.rs @@ -251,6 +251,7 @@ pub fn create_block_comment_transaction( changes.push((from + start_pos, keep_from, None)); let keep_until = from + end_pos - end_token.len() - end_margin as usize + 1; changes.push((keep_until, from + end_pos + 1, None)); + // The range of characters keep_from..keep_until remain in the document ranges.push(Range::new(keep_from, keep_until).with_direction(range.direction())); } } else { @@ -311,12 +312,12 @@ pub fn toggle_block_comments( // to 1 element in `new_ranges` let (from, to, _) = changes[i * 2]; let (from2, to2, _) = changes[i * 2 + 1]; - *added_chars -= to2 as isize - from2 as isize; + *added_chars -= to as isize - from as isize; selections.push(Range::new( (range.anchor as isize + *added_chars).try_into().unwrap(), (range.head as isize + *added_chars).try_into().unwrap(), )); - *added_chars -= to as isize - from as isize; + *added_chars -= to2 as isize - from2 as isize; } changes diff --git a/helix-term/tests/test/commands.rs b/helix-term/tests/test/commands.rs index acaf4b714..3330eb597 100644 --- a/helix-term/tests/test/commands.rs +++ b/helix-term/tests/test/commands.rs @@ -852,6 +852,64 @@ async fn test_injected_comment_tokens_multiple_selections() -> anyhow::Result<() )) .await?; + // Many selections on the same line + test(( + indoc! {r#"\ +
C#[|o]#mment t#(|o)#ggle #(|o)#n this line sh#(|o)#uld use the HTML c#(|o)#mment t#(|o)#ken(s).
+ + "#}, + ":lang htmlC#[|]#mment t#(|)#ggle #(|)#n this line sh#(|)#uld use the HTML c#(|)#mment t#(|)#ken(s).
+ + "#}, + )) + .await?; + test(( + indoc! {r#"\ +C#[|]#mment t#(|)#ggle #(|)#n this line sh#(|)#uld use the HTML c#(|)#mment t#(|)#ken(s).
+ + "#}, + ":lang htmlC#[|o]#mment t#(|o)#ggle #(|o)#n this line sh#(|o)#uld use the HTML c#(|o)#mment t#(|o)#ken(s).
+ + "#}, + )) + .await?; + Ok(()) } diff --git a/lol.html b/lol.html index 7a20664e4..4ad14046f 100644 --- a/lol.html +++ b/lol.html @@ -1,10 +1,10 @@ - +Comment toggle on this line should use the HTML comment token(s).