Revert jumplist index changes.

pull/5751/head
Kyle Smith 2 years ago
parent 70ade5f46e
commit af7cdefc2b

@ -21,21 +21,18 @@ type Jump = (DocumentId, Selection);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct JumpList { pub struct JumpList {
jumps: VecDeque<Jump>, jumps: VecDeque<Jump>,
current_idx: usize, current: usize,
} }
impl JumpList { impl JumpList {
pub fn new(initial: Jump) -> Self { pub fn new(initial: Jump) -> Self {
let mut jumps = VecDeque::with_capacity(JUMP_LIST_CAPACITY); let mut jumps = VecDeque::with_capacity(JUMP_LIST_CAPACITY);
jumps.push_back(initial); jumps.push_back(initial);
Self { Self { jumps, current: 0 }
jumps,
current_idx: 0,
}
} }
pub fn push(&mut self, jump: Jump) { pub fn push(&mut self, jump: Jump) {
self.jumps.truncate(self.current_idx + 1); self.jumps.truncate(self.current);
// don't push duplicates // don't push duplicates
if self.jumps.back() != Some(&jump) { if self.jumps.back() != Some(&jump) {
// If the jumplist is full, drop the oldest item. // If the jumplist is full, drop the oldest item.
@ -44,14 +41,14 @@ impl JumpList {
} }
self.jumps.push_back(jump); self.jumps.push_back(jump);
self.current_idx = self.jumps.len() - 1; self.current = self.jumps.len();
} }
} }
pub fn forward(&mut self, count: usize) -> Option<&Jump> { pub fn forward(&mut self, count: usize) -> Option<&Jump> {
if self.current_idx + count < self.jumps.len() { if self.current + count < self.jumps.len() {
self.current_idx += count; self.current += count;
self.jumps.get(self.current_idx) self.jumps.get(self.current)
} else { } else {
None None
} }
@ -59,13 +56,13 @@ impl JumpList {
// Taking view and doc to prevent unnecessary cloning when jump is not required. // 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> { pub fn backward(&mut self, view_id: ViewId, doc: &mut Document, count: usize) -> Option<&Jump> {
if let Some(current) = self.current_idx.checked_sub(count) { if let Some(current) = self.current.checked_sub(count) {
if self.current_idx == self.jumps.len() { if self.current == self.jumps.len() {
let jump = (doc.id(), doc.selection(view_id).clone()); let jump = (doc.id(), doc.selection(view_id).clone());
self.push(jump); self.push(jump);
} }
self.current_idx = current; self.current = current;
self.jumps.get(self.current_idx) self.jumps.get(self.current)
} else { } else {
None None
} }

Loading…
Cancel
Save