Skip to content

Commit

Permalink
Return num elements removed from front, and use in backward method
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasschafer committed Jun 15, 2024
1 parent fa15699 commit f2721f2
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ impl JumpList {
Self { jumps, current: 0 }
}

pub fn push(&mut self, jump: Jump) {
pub fn push(&mut self, jump: Jump) -> usize {
let mut num_removed_from_front = 0;
self.jumps.truncate(self.current);
// don't push duplicates
if self.jumps.back() != Some(&jump) {
// If the jumplist is full, drop the oldest item.
// If the jumplist is full, drop the oldest items.
while self.jumps.len() >= JUMP_LIST_CAPACITY {
self.jumps.pop_front();
if self.jumps.pop_front().is_some() {
num_removed_from_front += 1;
};
}

self.jumps.push_back(jump);
self.current = self.jumps.len();
}
num_removed_from_front
}

pub fn forward(&mut self, count: usize) -> Option<&Jump> {
Expand All @@ -63,14 +67,16 @@ impl JumpList {

// Taking view and doc to prevent unnecessary cloning when jump is not required.
pub fn backward(&mut self, view_id: ViewId, doc: &mut Document, count: usize) -> Option<&Jump> {
if self.current.checked_sub(count).is_some() {
if let Some(mut current) = self.current.checked_sub(count) {
let cur_jump = (doc.id(), doc.selection(view_id).clone());
if self.current == self.jumps.len() {
self.push(cur_jump.clone());
let num_removed = self.push(cur_jump.clone());
current = current.saturating_sub(num_removed);
}
// We need to do this separately from checked_sub above as pushing to the jumplist might change the position of self.current
self.current = self.current.saturating_sub(count);
self.current = current;
self.jumps.get(self.current).and_then(|prev_jump| {
// If a jumplist position is saved and then `jump_backward` is immediately invoked
// without moving the cursor, jump back one more step to prevent a no-op
if prev_jump == &cur_jump {
self.current = self.current.saturating_sub(1);
self.jumps.get(self.current)
Expand Down

0 comments on commit f2721f2

Please sign in to comment.