fix: incorrect order of index additions

pull/12759/head
Nikita Revenco 2025-02-02 09:31:43 +00:00 committed by Nik Revenco
parent 76b3e6778d
commit bbd7cb7bfb
3 changed files with 63 additions and 4 deletions

View File

@ -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

View File

@ -852,6 +852,64 @@ async fn test_injected_comment_tokens_multiple_selections() -> anyhow::Result<()
))
.await?;
// Many selections on the same line
test((
indoc! {r#"\
<p>C#[|o]#mment t#(|o)#ggle #(|o)#n this line sh#(|o)#uld use the HTML c#(|o)#mment t#(|o)#ken(s).</p>
<script type="text/javascript">
// Comment toggle on this line should use the javascript comment token(s).
foo();
css`
html {
background-color: red;
}
`;
</script>
"#},
":lang html<ret> C",
indoc! {r#"\
<p>C#[|<!-- o -->]#mment t#(|<!-- o -->)#ggle #(|<!-- o -->)#n this line sh#(|<!-- o -->)#uld use the HTML c#(|<!-- o -->)#mment t#(|<!-- o -->)#ken(s).</p>
<script type="text/javascript">
// Comment toggle on this line should use the javascript comment token(s).
foo();
css`
html {
background-color: red;
}
`;
</script>
"#},
))
.await?;
test((
indoc! {r#"\
<p>C#[|<!-- o -->]#mment t#(|<!-- o -->)#ggle #(|<!-- o -->)#n this line sh#(|<!-- o -->)#uld use the HTML c#(|<!-- o -->)#mment t#(|<!-- o -->)#ken(s).</p>
<script type="text/javascript">
// Comment toggle on this line should use the javascript comment token(s).
foo();
css`
html {
background-color: red;
}
`;
</script>
"#},
":lang html<ret> C",
indoc! {r#"\
<p>C#[|o]#mment t#(|o)#ggle #(|o)#n this line sh#(|o)#uld use the HTML c#(|o)#mment t#(|o)#ken(s).</p>
<script type="text/javascript">
// Comment toggle on this line should use the javascript comment token(s).
foo();
css`
html {
background-color: red;
}
`;
</script>
"#},
))
.await?;
Ok(())
}

View File

@ -1,10 +1,10 @@
<!-- <p>Comment toggle on this line should use the HTML comment token(s).</p> -->
<p>Comment toggle on this line should use the HTML comment token(s).</p>
<script type="text/javascript">
// Comment toggle on this line should use the javascript comment token(s).
foo();
css`
html {
background-color: #red;
background-color: red;
}
`;
</script>