From e3b9c1fa4868a1baffca7356d708cc454af0493c Mon Sep 17 00:00:00 2001 From: thomasschafer Date: Sat, 15 Jun 2024 20:09:05 +0100 Subject: [PATCH] Return num elements removed from front, and use in backward method --- helix-view/src/view.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index ac86356f8672..915de1a4ef8f 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. 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,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)