From 7f9e7611b6ec8da99c9e1fbfef744db487cb72bc Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Wed, 13 Nov 2024 22:49:52 +0000 Subject: [PATCH] fix: panic which occured if ranges overlapped --- helix-core/src/transaction.rs | 1 - helix-term/src/commands.rs | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index ad08d0c65..c5c94b750 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -615,7 +615,6 @@ impl Transaction { let mut last = 0; for (from, to, tendril) in changes { // Verify ranges are ordered and not overlapping - dbg!(last, from); debug_assert!(last <= from); // Verify ranges are correct debug_assert!( diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 69a68d986..550b4c1c9 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -5865,6 +5865,24 @@ fn surround_delete(cx: &mut Context) { return; } }; + let mut ranges: SmallVec<[Range; 1]> = change_pos + .iter() + .flat_map(|&((opening_tag_range, closing_tag_range), _)| { + vec![opening_tag_range, closing_tag_range] + }) + .collect(); + + ranges.sort_by(|a, b| a.from().cmp(&b.from())); + + let has_overlaps = ranges + .windows(2) + .any(|window| window[0].to() > window[1].from()); + + if has_overlaps { + cx.editor + .set_error("Cursors overlap for a single surround pair range"); + return; + } let transaction = Transaction::change( doc.text(), change_pos.iter().flat_map(|(pos, _)| {