on master help sign in
Limit the number of items in the jumplist (#4750)
8102c32Michael Davis
helix-term/src/commands.rs−1
@@ -2427,7 +2427,6 @@ fn jumplist_picker(cx: &mut Context) {
2427 .views()
2427 .views()
2428 .flat_map(|(view, _)| {
2428 .flat_map(|(view, _)| {
2429 view.jumps
2429 view.jumps
2430− .get()
2431 .iter()
2430 .iter()
2432 .map(|(doc_id, selection)| new_meta(view, *doc_id, selection.clone()))
2431 .map(|(doc_id, selection)| new_meta(view, *doc_id, selection.clone()))
2433 })
2432 })
helix-view/src/view.rs+16−10
@@ -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,
4};
4};
5
5
6−use std::fmt;
6+use std::{collections::VecDeque, fmt};
7+
8+const JUMP_LIST_CAPACITY: usize = 30;
7
9
8type Jump = (DocumentId, Selection);
10type Jump = (DocumentId, Selection);
9
11
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,
14}
16}
15
17
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 }
21− }
22 }
23 }
23
24
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();
32+ }
33+
28− self.jumps.push(jump);
34+ self.jumps.push_back(jump);
29 self.current = self.jumps.len();
35 self.current = self.jumps.len();
30 }
36 }
31 }
37 }
@@ -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);
58 }
64 }
59
65
60− pub fn get(&self) -> &[Jump] {
66+ pub fn iter(&self) -> impl Iterator<Item = &Jump> {
61− &self.jumps
67+ self.jumps.iter()
62 }
68 }
63
69
64 /// Applies a [`Transaction`] of changes to the jumplist.
70 /// Applies a [`Transaction`] of changes to the jumplist.