diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index f12c79f937a3..293df976c8c8 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2216,6 +2216,7 @@ fn buffer_picker(cx: &mut Context) { path: Option, is_modified: bool, is_current: bool, + used_at: std::time::Instant, } impl ui::menu::Item for BufferMeta { @@ -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::>(); + + // 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); diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 50c70af68dba..52cc794c76ff 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -121,6 +121,9 @@ pub struct Document { diagnostics: Vec, language_server: Option>, + + // when document was used for most-recent-used buffer picker + pub used_at: std::time::Instant, } use std::{fmt, mem}; @@ -361,6 +364,7 @@ impl Document { last_saved_revision: 0, modified_since_accessed: false, language_server: None, + used_at: std::time::Instant::now(), } } @@ -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); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 501c3069ad19..fc50957e8276 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -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 @@ -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 => { @@ -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(); } }