Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jump_backwards behaviour when jumplist is at capacity #10968

Merged
26 changes: 20 additions & 6 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 {
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
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() {
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
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,13 +67,23 @@ 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 let Some(current) = self.current.checked_sub(count) {
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() {
let jump = (doc.id(), doc.selection(view_id).clone());
self.push(jump);
let num_removed = self.push(cur_jump.clone());
current = current.saturating_sub(num_removed);
}
self.current = current;
self.jumps.get(self.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)
} else {
Some(prev_jump)
}
})
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
} else {
None
}
Expand Down
Loading