@@ -3,29 +3,35 @@ use helix_core::{
3 pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection, Transaction,
3 pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection, Transaction,
6−use std::fmt;
6+use std::{collections::VecDeque, fmt};
8+const JUMP_LIST_CAPACITY: usize = 30;
8type Jump = (DocumentId, Selection);
10type Jump = (DocumentId, Selection);
10#[derive(Debug, Clone)]
12#[derive(Debug, Clone)]
11pub struct JumpList {
13pub struct JumpList {
12− jumps: Vec<Jump>,
14+ jumps: VecDeque<Jump>,
13 current: usize,
15 current: usize,
16impl JumpList {
18impl JumpList {
17 pub fn new(initial: Jump) -> Self {
19 pub fn new(initial: Jump) -> Self {
18− Self {
20+ let mut jumps = VecDeque::with_capacity(JUMP_LIST_CAPACITY);
19− jumps: vec![initial],
21+ jumps.push_back(initial);
20− current: 0,
22+ Self { jumps, current: 0 }
24 pub fn push(&mut self, jump: Jump) {
25 pub fn push(&mut self, jump: Jump) {
25 self.jumps.truncate(self.current);
26 self.jumps.truncate(self.current);
26 // don't push duplicates
27 // don't push duplicates
27− if self.jumps.last() != Some(&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();
28− self.jumps.push(jump);
34+ self.jumps.push_back(jump);
29 self.current = self.jumps.len();
35 self.current = self.jumps.len();
@@ -57,8 +63,8 @@ impl JumpList {
57 self.jumps.retain(|(other_id, _)| other_id != doc_id);
63 self.jumps.retain(|(other_id, _)| other_id != doc_id);
60− pub fn get(&self) -> &[Jump] {
66+ pub fn iter(&self) -> impl Iterator<Item = &Jump> {
61− &self.jumps
67+ self.jumps.iter()
64 /// Applies a [`Transaction`] of changes to the jumplist.
70 /// Applies a [`Transaction`] of changes to the jumplist.