aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src
diff options
context:
space:
mode:
authorMike Trinkala2024-03-02 14:05:58 +0000
committerGitHub2024-03-02 14:05:58 +0000
commit5bd007266a962a534bd722619821e998735b71e2 (patch)
treeb932cfb1b34ee6eb5f31b036d843f883e24823fc /helix-term/src
parentd769fadde085169c26a850966a6d5d8da7cc1c12 (diff)
Fix panic when using join_selections_space (#9783)
Joining lines with Alt-J does not properly select the inserted spaces when the selection contains blank lines. In the worst case it panics with an out of bounds index. thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Char index out of bounds: char index 11, Rope/RopeSlice char length 10' Steps to reproduce: * Create a new document ``` a b c d e ``` * % (Select all) * Alt-J (join and select the spaces)
Diffstat (limited to 'helix-term/src')
-rw-r--r--helix-term/src/commands.rs23
1 files changed, 17 insertions, 6 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index bd0a60b7..0b2ea0b8 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -4372,16 +4372,27 @@ fn join_selections_impl(cx: &mut Context, select_space: bool) {
// select inserted spaces
let transaction = if select_space {
+ let mut offset: usize = 0;
let ranges: SmallVec<_> = changes
.iter()
- .scan(0, |offset, change| {
- let range = Range::point(change.0 - *offset);
- *offset += change.1 - change.0 - 1; // -1 because cursor is 0-sized
- Some(range)
+ .filter_map(|change| {
+ if change.2.is_some() {
+ let range = Range::point(change.0 - offset);
+ offset += change.1 - change.0 - 1; // -1 adjusts for the replacement of the range by a space
+ Some(range)
+ } else {
+ offset += change.1 - change.0;
+ None
+ }
})
.collect();
- let selection = Selection::new(ranges, 0);
- Transaction::change(text, changes.into_iter()).with_selection(selection)
+ let t = Transaction::change(text, changes.into_iter());
+ if ranges.is_empty() {
+ t
+ } else {
+ let selection = Selection::new(ranges, 0);
+ t.with_selection(selection)
+ }
} else {
Transaction::change(text, changes.into_iter())
};