on master help sign in
Sort the buffer picker by most recent access (#2980)
commit6a1bb81f1022533c226395aaa41439ab1ea41dd4 authorEvgeniy Tatarkin <tatarkin.evg@gmail.com> dateFri 28 Apr 2023, 19:05 (4 years ago) parent9c6c63a
3 files changed+30−5
helix-term/src/commands.rs+13−5 helix-view/src/document.rs+9 helix-view/src/editor.rs+8
helix-term/src/commands.rs+13−5
@@ -2473,6 +2473,7 @@ fn buffer_picker(cx: &mut Context) {
24732473 path: Option<PathBuf>,
24742474 is_modified: bool,
24752475 is_current: bool,
2476+ focused_at: std::time::Instant,
24762477 }
24772478
24782479 impl ui::menu::Item for BufferMeta {
@@ -2505,14 +2506,21 @@ fn buffer_picker(cx: &mut Context) {
25052506 path: doc.path().cloned(),
25062507 is_modified: doc.is_modified(),
25072508 is_current: doc.id() == current,
2509+ focused_at: doc.focused_at,
25082510 };
25092511
2512+ let mut items = cx
2513+ .editor
2514+ .documents
2515+ .values()
2516+ .map(|doc| new_meta(doc))
2517+ .collect::<Vec<BufferMeta>>();
2518+
2519+ // mru
2520+ items.sort_unstable_by_key(|item| std::cmp::Reverse(item.focused_at));
2521+
25102522 let picker = FilePicker::new(
2511− cx.editor
2512− .documents
2513− .values()
2514− .map(|doc| new_meta(doc))
2515− .collect(),
2523+ items,
25162524 (),
25172525 |cx, meta, action| {
25182526 cx.editor.switch(meta.id, action);
helix-view/src/document.rs+9
@@ -169,6 +169,9 @@ pub struct Document {
169169
170170 diff_handle: Option<DiffHandle>,
171171 version_control_head: Option<Arc<ArcSwap<Box<str>>>>,
172+
173+ // when document was used for most-recent-used buffer picker
174+ pub focused_at: std::time::Instant,
172175}
173176
174177/// Inlay hints for a single `(Document, View)` combo.
@@ -496,6 +499,7 @@ impl Document {
496499 diff_handle: None,
497500 config,
498501 version_control_head: None,
502+ focused_at: std::time::Instant::now(),
499503 }
500504 }
501505 pub fn default(config: Arc<dyn DynAccess<Config>>) -> Self {
@@ -908,6 +912,11 @@ impl Document {
908912 }
909913 }
910914
915+ /// Mark document as recent used for MRU sorting
916+ pub fn mark_as_focused(&mut self) {
917+ self.focused_at = std::time::Instant::now();
918+ }
919+
911920 /// Remove a view's selection and inlay hints from this document.
912921 pub fn remove_view(&mut self, view_id: ViewId) {
913922 self.selections.remove(&view_id);
helix-view/src/editor.rs+8
@@ -1176,6 +1176,7 @@ impl Editor {
11761176 let doc = doc_mut!(self, &doc_id);
11771177 doc.ensure_view_init(view.id);
11781178 view.sync_changes(doc);
1179+ doc.mark_as_focused();
11791180
11801181 align_view(doc, view, Align::Center);
11811182 }
@@ -1246,6 +1247,7 @@ impl Editor {
12461247 let view_id = view!(self).id;
12471248 let doc = doc_mut!(self, &id);
12481249 doc.ensure_view_init(view_id);
1250+ doc.mark_as_focused();
12491251 return;
12501252 }
12511253 Action::HorizontalSplit | Action::VerticalSplit => {
@@ -1267,6 +1269,7 @@ impl Editor {
12671269 // initialize selection for view
12681270 let doc = doc_mut!(self, &id);
12691271 doc.ensure_view_init(view_id);
1272+ doc.mark_as_focused();
12701273 }
12711274 }
12721275
@@ -1417,6 +1420,7 @@ impl Editor {
14171420 let view_id = self.tree.insert(view);
14181421 let doc = doc_mut!(self, &doc_id);
14191422 doc.ensure_view_init(view_id);
1423+ doc.mark_as_focused();
14201424 }
14211425
14221426 self._refresh();
@@ -1471,6 +1475,10 @@ impl Editor {
14711475 view.sync_changes(doc);
14721476 }
14731477 }
1478+
1479+ let view = view!(self, view_id);
1480+ let doc = doc_mut!(self, &view.doc);
1481+ doc.mark_as_focused();
14741482 }
14751483
14761484 pub fn focus_next(&mut self) {