on master help sign in
Fix backwards selection duplication widening bug (#2945)
2ac1de3A-Walrus
helix-term/src/commands.rs+4−4
@@ -1411,16 +1411,16 @@ fn copy_selection_on_line(cx: &mut Context, direction: Direction) {
1411 let is_primary = *range == selection.primary();
1411 let is_primary = *range == selection.primary();
1412
1412
1413 // The range is always head exclusive
1413 // The range is always head exclusive
1414− let head = if range.anchor < range.head {
1414+ let (head, anchor) = if range.anchor < range.head {
1415− range.head - 1
1415+ (range.head - 1, range.anchor)
1416 } else {
1416 } else {
1417− range.head
1417+ (range.head, range.anchor - 1)
1418 };
1418 };
1419
1419
1420 let tab_width = doc.tab_width();
1420 let tab_width = doc.tab_width();
1421
1421
1422 let head_pos = visual_coords_at_pos(text, head, tab_width);
1422 let head_pos = visual_coords_at_pos(text, head, tab_width);
1423− let anchor_pos = visual_coords_at_pos(text, range.anchor, tab_width);
1423+ let anchor_pos = visual_coords_at_pos(text, anchor, tab_width);
1424
1424
1425 let height = std::cmp::max(head_pos.row, anchor_pos.row)
1425 let height = std::cmp::max(head_pos.row, anchor_pos.row)
1426 - std::cmp::min(head_pos.row, anchor_pos.row)
1426 - std::cmp::min(head_pos.row, anchor_pos.row)
helix-term/tests/test/commands.rs+40
@@ -91,3 +91,43 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> {
91
91
92 Ok(())
92 Ok(())
93}
93}
94+
95+#[tokio::test]
96+async fn test_selection_duplication() -> anyhow::Result<()> {
97+ // Forward
98+ test((
99+ platform_line(indoc! {"\
100+ #[lo|]#rem
101+ ipsum
102+ dolor
103+ "})
104+ .as_str(),
105+ "CC",
106+ platform_line(indoc! {"\
107+ #(lo|)#rem
108+ #(ip|)#sum
109+ #[do|]#lor
110+ "})
111+ .as_str(),
112+ ))
113+ .await?;
114+
115+ // Backward
116+ test((
117+ platform_line(indoc! {"\
118+ #[|lo]#rem
119+ ipsum
120+ dolor
121+ "})
122+ .as_str(),
123+ "CC",
124+ platform_line(indoc! {"\
125+ #(|lo)#rem
126+ #(|ip)#sum
127+ #[|do]#lor
128+ "})
129+ .as_str(),
130+ ))
131+ .await?;
132+ Ok(())
133+}