Skip to content

Commit

Permalink
most recent used buffers picker
Browse files Browse the repository at this point in the history
  • Loading branch information
estin committed Aug 1, 2022
1 parent 07019c3 commit 38ececc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
18 changes: 13 additions & 5 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2216,6 +2216,7 @@ fn buffer_picker(cx: &mut Context) {
path: Option<PathBuf>,
is_modified: bool,
is_current: bool,
used_at: std::time::Instant,
}

impl ui::menu::Item for BufferMeta {
Expand Down Expand Up @@ -2253,14 +2254,21 @@ fn buffer_picker(cx: &mut Context) {
path: doc.path().cloned(),
is_modified: doc.is_modified(),
is_current: doc.id() == current,
used_at: doc.used_at,
};

let mut items = cx
.editor
.documents
.iter()
.map(|(_, doc)| new_meta(doc))
.collect::<Vec<BufferMeta>>();

// mru
items.sort_by(|a, b| b.used_at.cmp(&a.used_at));

let picker = FilePicker::new(
cx.editor
.documents
.iter()
.map(|(_, doc)| new_meta(doc))
.collect(),
items,
(),
|cx, meta, action| {
cx.editor.switch(meta.id, action);
Expand Down
9 changes: 9 additions & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ pub struct Document {

diagnostics: Vec<Diagnostic>,
language_server: Option<Arc<helix_lsp::Client>>,

// when document was used for most-recent-used buffer picker
pub used_at: std::time::Instant,
}

use std::{fmt, mem};
Expand Down Expand Up @@ -361,6 +364,7 @@ impl Document {
last_saved_revision: 0,
modified_since_accessed: false,
language_server: None,
used_at: std::time::Instant::now(),
}
}

Expand Down Expand Up @@ -664,6 +668,11 @@ impl Document {
}
}

/// Mark document as recent used for MRU sorting
pub fn mark_as_used(&mut self) {
self.used_at = std::time::Instant::now();
}

/// Remove a view's selection from this document.
pub fn remove_view(&mut self, view_id: ViewId) {
self.selections.remove(&view_id);
Expand Down
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ impl Editor {

let doc = self.documents.get_mut(&doc_id).unwrap();
doc.ensure_view_init(view.id);
doc.mark_as_used();

// TODO: reuse align_view
let pos = doc
Expand Down Expand Up @@ -878,6 +879,7 @@ impl Editor {
let view_id = view!(self).id;
let doc = self.documents.get_mut(&id).unwrap();
doc.ensure_view_init(view_id);
doc.mark_as_used();
return;
}
Action::HorizontalSplit | Action::VerticalSplit => {
Expand All @@ -899,6 +901,7 @@ impl Editor {
// initialize selection for view
let doc = self.documents.get_mut(&id).unwrap();
doc.ensure_view_init(view_id);
doc.mark_as_used();
}
}

Expand Down

0 comments on commit 38ececc

Please sign in to comment.