From 0cc04124e7a041f00ec730192c788ae8a40db053 Mon Sep 17 00:00:00 2001 From: Evgeniy Tatarkin Date: Tue, 5 Jul 2022 23:46:05 +0300 Subject: [PATCH 1/5] most recent used buffers picker --- helix-term/src/commands.rs | 18 +++++++++++++----- helix-view/src/document.rs | 9 +++++++++ helix-view/src/editor.rs | 3 +++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 5a75553cd6da..11b9bb0c038e 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2473,6 +2473,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 { @@ -2505,14 +2506,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 + .values() + .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 - .values() - .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 eca6002653f5..fef344e2cc97 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -169,6 +169,9 @@ pub struct Document { diff_handle: Option, version_control_head: Option>>>, + + // when document was used for most-recent-used buffer picker + pub used_at: std::time::Instant, } /// Inlay hints for a single `(Document, View)` combo. @@ -496,6 +499,7 @@ impl Document { diff_handle: None, config, version_control_head: None, + used_at: std::time::Instant::now(), } } pub fn default(config: Arc>) -> Self { @@ -908,6 +912,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 and inlay hints 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 fd0abe91dbcb..91f396646754 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1176,6 +1176,7 @@ impl Editor { let doc = doc_mut!(self, &doc_id); doc.ensure_view_init(view.id); view.sync_changes(doc); + doc.mark_as_used(); align_view(doc, view, Align::Center); } @@ -1246,6 +1247,7 @@ impl Editor { let view_id = view!(self).id; let doc = doc_mut!(self, &id); doc.ensure_view_init(view_id); + doc.mark_as_used(); return; } Action::HorizontalSplit | Action::VerticalSplit => { @@ -1267,6 +1269,7 @@ impl Editor { // initialize selection for view let doc = doc_mut!(self, &id); doc.ensure_view_init(view_id); + doc.mark_as_used(); } } From cf66bde411f166ef06fd35bcf0d08916d22c589a Mon Sep 17 00:00:00 2001 From: Evgeniy Tatarkin Date: Mon, 7 Nov 2022 09:17:44 +0300 Subject: [PATCH 2/5] rename used_at to focused_at, use sort_unstable_by_key --- helix-term/src/commands.rs | 4 ++-- helix-view/src/document.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 11b9bb0c038e..0493e5218e4a 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2506,7 +2506,7 @@ 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, + used_at: doc.focused_at, }; let mut items = cx @@ -2514,7 +2514,7 @@ fn buffer_picker(cx: &mut Context) { .documents .values() .map(|doc| new_meta(doc)) - .collect(); + .collect::>(); // mru items.sort_by(|a, b| b.used_at.cmp(&a.used_at)); diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index fef344e2cc97..84d654f42554 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -171,7 +171,7 @@ pub struct Document { version_control_head: Option>>>, // when document was used for most-recent-used buffer picker - pub used_at: std::time::Instant, + pub focused_at: std::time::Instant, } /// Inlay hints for a single `(Document, View)` combo. @@ -499,7 +499,7 @@ impl Document { diff_handle: None, config, version_control_head: None, - used_at: std::time::Instant::now(), + focused_at: std::time::Instant::now(), } } pub fn default(config: Arc>) -> Self { @@ -914,7 +914,7 @@ impl Document { /// Mark document as recent used for MRU sorting pub fn mark_as_used(&mut self) { - self.used_at = std::time::Instant::now(); + self.focused_at = std::time::Instant::now(); } /// Remove a view's selection and inlay hints from this document. From 847831b59ef3b12370d62a812c449a3dddbaa197 Mon Sep 17 00:00:00 2001 From: Evgeniy Tatarkin Date: Mon, 7 Nov 2022 09:35:53 +0300 Subject: [PATCH 3/5] mark document as focused on Editor::focus and Editor::focus_next --- helix-view/src/document.rs | 2 +- helix-view/src/editor.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 84d654f42554..5ede5bc60776 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -913,7 +913,7 @@ impl Document { } /// Mark document as recent used for MRU sorting - pub fn mark_as_used(&mut self) { + pub fn mark_as_focused(&mut self) { self.focused_at = std::time::Instant::now(); } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 91f396646754..076e5befed3d 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1176,7 +1176,7 @@ impl Editor { let doc = doc_mut!(self, &doc_id); doc.ensure_view_init(view.id); view.sync_changes(doc); - doc.mark_as_used(); + doc.mark_as_focused(); align_view(doc, view, Align::Center); } @@ -1247,7 +1247,7 @@ impl Editor { let view_id = view!(self).id; let doc = doc_mut!(self, &id); doc.ensure_view_init(view_id); - doc.mark_as_used(); + doc.mark_as_focused(); return; } Action::HorizontalSplit | Action::VerticalSplit => { @@ -1269,7 +1269,7 @@ impl Editor { // initialize selection for view let doc = doc_mut!(self, &id); doc.ensure_view_init(view_id); - doc.mark_as_used(); + doc.mark_as_focused(); } } @@ -1474,6 +1474,10 @@ impl Editor { view.sync_changes(doc); } } + + let view = self.tree.get_mut(view_id); + let doc = doc_mut!(self, &view.doc); + doc.mark_as_focused(); } pub fn focus_next(&mut self) { From 48bcb68c07645ab7cf533c67db09369018288573 Mon Sep 17 00:00:00 2001 From: Evgeniy Tatarkin Date: Tue, 8 Nov 2022 09:13:13 +0300 Subject: [PATCH 4/5] get view by view! macro --- helix-view/src/editor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 076e5befed3d..fedb2ff44472 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1475,7 +1475,7 @@ impl Editor { } } - let view = self.tree.get_mut(view_id); + let view = view!(self, view_id); let doc = doc_mut!(self, &view.doc); doc.mark_as_focused(); } From 1de79359adc7ebdff6693d507530156ff4dd3c34 Mon Sep 17 00:00:00 2001 From: Evgeniy Tatarkin Date: Fri, 2 Dec 2022 08:25:27 +0300 Subject: [PATCH 5/5] fix --- helix-term/src/commands.rs | 6 +++--- helix-view/src/editor.rs | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 0493e5218e4a..88393ff4cd8d 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2473,7 +2473,7 @@ fn buffer_picker(cx: &mut Context) { path: Option, is_modified: bool, is_current: bool, - used_at: std::time::Instant, + focused_at: std::time::Instant, } impl ui::menu::Item for BufferMeta { @@ -2506,7 +2506,7 @@ fn buffer_picker(cx: &mut Context) { path: doc.path().cloned(), is_modified: doc.is_modified(), is_current: doc.id() == current, - used_at: doc.focused_at, + focused_at: doc.focused_at, }; let mut items = cx @@ -2517,7 +2517,7 @@ fn buffer_picker(cx: &mut Context) { .collect::>(); // mru - items.sort_by(|a, b| b.used_at.cmp(&a.used_at)); + items.sort_unstable_by_key(|item| std::cmp::Reverse(item.focused_at)); let picker = FilePicker::new( items, diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index fedb2ff44472..005c66674483 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1420,6 +1420,7 @@ impl Editor { let view_id = self.tree.insert(view); let doc = doc_mut!(self, &doc_id); doc.ensure_view_init(view_id); + doc.mark_as_focused(); } self._refresh();