diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index ac86356f8672b..601a9fb61c84f 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -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> { @@ -63,13 +67,13 @@ 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 prev_jump == &cur_jump { self.current = self.current.saturating_sub(1);