on master help sign in
Sort the buffer picker by most recent access (#2980)
6a1bb81Evgeniy Tatarkin
helix-term/src/commands.rs+13−5
@@ -2473,6 +2473,7 @@ fn buffer_picker(cx: &mut Context) {
2473 path: Option<PathBuf>,
2473 path: Option<PathBuf>,
2474 is_modified: bool,
2474 is_modified: bool,
2475 is_current: bool,
2475 is_current: bool,
2476+ focused_at: std::time::Instant,
2476 }
2477 }
2477
2478
2478 impl ui::menu::Item for BufferMeta {
2479 impl ui::menu::Item for BufferMeta {
@@ -2505,14 +2506,21 @@ fn buffer_picker(cx: &mut Context) {
2505 path: doc.path().cloned(),
2506 path: doc.path().cloned(),
2506 is_modified: doc.is_modified(),
2507 is_modified: doc.is_modified(),
2507 is_current: doc.id() == current,
2508 is_current: doc.id() == current,
2509+ focused_at: doc.focused_at,
2508 };
2510 };
2509
2511
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+
2510 let picker = FilePicker::new(
2522 let picker = FilePicker::new(
2511− cx.editor
2523+ items,
2512− .documents
2513− .values()
2514− .map(|doc| new_meta(doc))
2515− .collect(),
2516 (),
2524 (),
2517 |cx, meta, action| {
2525 |cx, meta, action| {
2518 cx.editor.switch(meta.id, action);
2526 cx.editor.switch(meta.id, action);
helix-view/src/document.rs+9
@@ -169,6 +169,9 @@ pub struct Document {
169
169
170 diff_handle: Option<DiffHandle>,
170 diff_handle: Option<DiffHandle>,
171 version_control_head: Option<Arc<ArcSwap<Box<str>>>>,
171 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,
172}
175}
173
176
174/// Inlay hints for a single `(Document, View)` combo.
177/// Inlay hints for a single `(Document, View)` combo.
@@ -496,6 +499,7 @@ impl Document {
496 diff_handle: None,
499 diff_handle: None,
497 config,
500 config,
498 version_control_head: None,
501 version_control_head: None,
502+ focused_at: std::time::Instant::now(),
499 }
503 }
500 }
504 }
501 pub fn default(config: Arc<dyn DynAccess<Config>>) -> Self {
505 pub fn default(config: Arc<dyn DynAccess<Config>>) -> Self {
@@ -908,6 +912,11 @@ impl Document {
908 }
912 }
909 }
913 }
910
914
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+
911 /// Remove a view's selection and inlay hints from this document.
920 /// Remove a view's selection and inlay hints from this document.
912 pub fn remove_view(&mut self, view_id: ViewId) {
921 pub fn remove_view(&mut self, view_id: ViewId) {
913 self.selections.remove(&view_id);
922 self.selections.remove(&view_id);
helix-view/src/editor.rs+8
@@ -1176,6 +1176,7 @@ impl Editor {
1176 let doc = doc_mut!(self, &doc_id);
1176 let doc = doc_mut!(self, &doc_id);
1177 doc.ensure_view_init(view.id);
1177 doc.ensure_view_init(view.id);
1178 view.sync_changes(doc);
1178 view.sync_changes(doc);
1179+ doc.mark_as_focused();
1179
1180
1180 align_view(doc, view, Align::Center);
1181 align_view(doc, view, Align::Center);
1181 }
1182 }
@@ -1246,6 +1247,7 @@ impl Editor {
1246 let view_id = view!(self).id;
1247 let view_id = view!(self).id;
1247 let doc = doc_mut!(self, &id);
1248 let doc = doc_mut!(self, &id);
1248 doc.ensure_view_init(view_id);
1249 doc.ensure_view_init(view_id);
1250+ doc.mark_as_focused();
1249 return;
1251 return;
1250 }
1252 }
1251 Action::HorizontalSplit | Action::VerticalSplit => {
1253 Action::HorizontalSplit | Action::VerticalSplit => {
@@ -1267,6 +1269,7 @@ impl Editor {
1267 // initialize selection for view
1269 // initialize selection for view
1268 let doc = doc_mut!(self, &id);
1270 let doc = doc_mut!(self, &id);
1269 doc.ensure_view_init(view_id);
1271 doc.ensure_view_init(view_id);
1272+ doc.mark_as_focused();
1270 }
1273 }
1271 }
1274 }
1272
1275
@@ -1417,6 +1420,7 @@ impl Editor {
1417 let view_id = self.tree.insert(view);
1420 let view_id = self.tree.insert(view);
1418 let doc = doc_mut!(self, &doc_id);
1421 let doc = doc_mut!(self, &doc_id);
1419 doc.ensure_view_init(view_id);
1422 doc.ensure_view_init(view_id);
1423+ doc.mark_as_focused();
1420 }
1424 }
1421
1425
1422 self._refresh();
1426 self._refresh();
@@ -1471,6 +1475,10 @@ impl Editor {
1471 view.sync_changes(doc);
1475 view.sync_changes(doc);
1472 }
1476 }
1473 }
1477 }
1478+
1479+ let view = view!(self, view_id);
1480+ let doc = doc_mut!(self, &view.doc);
1481+ doc.mark_as_focused();
1474 }
1482 }
1475
1483
1476 pub fn focus_next(&mut self) {
1484 pub fn focus_next(&mut self) {