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

Limit the number of items in the jumplist #4750

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2408,7 +2408,6 @@ fn jumplist_picker(cx: &mut Context) {
.views()
.flat_map(|(view, _)| {
view.jumps
.get()
.iter()
.map(|(doc_id, selection)| new_meta(view, *doc_id, selection.clone()))
})
Expand Down
26 changes: 16 additions & 10 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@ use helix_core::{
pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection, Transaction,
};

use std::fmt;
use std::{collections::VecDeque, fmt};

const JUMP_LIST_CAPACITY: usize = 30;

type Jump = (DocumentId, Selection);

#[derive(Debug, Clone)]
pub struct JumpList {
jumps: Vec<Jump>,
jumps: VecDeque<Jump>,
current: usize,
}

impl JumpList {
pub fn new(initial: Jump) -> Self {
Self {
jumps: vec![initial],
current: 0,
}
let mut jumps = VecDeque::with_capacity(JUMP_LIST_CAPACITY);
jumps.push_back(initial);
Self { jumps, current: 0 }
}

pub fn push(&mut self, jump: Jump) {
self.jumps.truncate(self.current);
// don't push duplicates
if self.jumps.last() != Some(&jump) {
self.jumps.push(jump);
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();
}

self.jumps.push_back(jump);
self.current = self.jumps.len();
}
}
Expand Down Expand Up @@ -57,8 +63,8 @@ impl JumpList {
self.jumps.retain(|(other_id, _)| other_id != doc_id);
}

pub fn get(&self) -> &[Jump] {
&self.jumps
pub fn iter(&self) -> impl Iterator<Item = &Jump> {
self.jumps.iter()
}

/// Applies a [`Transaction`] of changes to the jumplist.
Expand Down