@@ -3,29 +3,35 @@ use helix_core::{
33 pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection, Transaction,
44};
55
6−use std::fmt;
6+use std::{collections::VecDeque, fmt};
7+
8+const JUMP_LIST_CAPACITY: usize = 30;
79
810type Jump = (DocumentId, Selection);
911
1012#[derive(Debug, Clone)]
1113pub struct JumpList {
12− jumps: Vec<Jump>,
14+ jumps: VecDeque<Jump>,
1315 current: usize,
1416}
1517
1618impl JumpList {
1719 pub fn new(initial: Jump) -> Self {
18− Self {
19− jumps: vec![initial],
20− current: 0,
21− }
20+ let mut jumps = VecDeque::with_capacity(JUMP_LIST_CAPACITY);
21+ jumps.push_back(initial);
22+ Self { jumps, current: 0 }
2223 }
2324
2425 pub fn push(&mut self, jump: Jump) {
2526 self.jumps.truncate(self.current);
2627 // don't push duplicates
27− if self.jumps.last() != Some(&jump) {
28− self.jumps.push(jump);
28+ if self.jumps.back() != Some(&jump) {
29+ // If the jumplist is full, drop the oldest item.
30+ while self.jumps.len() >= JUMP_LIST_CAPACITY {
31+ self.jumps.pop_front();
32+ }
33+
34+ self.jumps.push_back(jump);
2935 self.current = self.jumps.len();
3036 }
3137 }
@@ -57,8 +63,8 @@ impl JumpList {
5763 self.jumps.retain(|(other_id, _)| other_id != doc_id);
5864 }
5965
60− pub fn get(&self) -> &[Jump] {
61− &self.jumps
66+ pub fn iter(&self) -> impl Iterator<Item = &Jump> {
67+ self.jumps.iter()
6268 }
6369
6470 /// Applies a [`Transaction`] of changes to the jumplist.