Skip to content

Commit

Permalink
Short-circuit the word and treesitter object movement commands
Browse files Browse the repository at this point in the history
The loop always iterates the number of times the user specified even
if the beginning/end of the document is reached.

For an extreme demonstration try the following commands, Helix will
hang for several seconds.
100000000w
100000000]c
  • Loading branch information
trink committed Feb 6, 2023
1 parent 9c98043 commit 234bc71
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions helix-core/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,15 @@ fn word_move(slice: RopeSlice, range: Range, count: usize, target: WordMotionTar
};

// Do the main work.
(0..count).fold(start_range, |r, _| {
slice.chars_at(r.head).range_to_target(target, r)
})
let mut r = start_range;
for _ in 0..count {
let tmp = slice.chars_at(r.head).range_to_target(target, r);
if r == tmp {
break;
}
r = tmp;
}
r
}

pub fn move_prev_paragraph(
Expand Down Expand Up @@ -523,7 +529,14 @@ pub fn goto_treesitter_object(
// head of range should be at beginning
Some(Range::new(start_char, end_char))
};
(0..count).fold(range, |range, _| get_range(range).unwrap_or(range))
let mut last_range = range;
for _ in 0..count {
match get_range(last_range) {
Some(r) if r != last_range => last_range = r,
_ => break,
}
}
last_range
}

#[cfg(test)]
Expand Down

0 comments on commit 234bc71

Please sign in to comment.