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 Dec 2, 2022
1 parent e926518 commit 734489c
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 @@ -2304,6 +2304,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 @@ -2341,14 +2342,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);
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 @@ -138,6 +138,9 @@ pub struct Document {
language_server: Option<Arc<helix_lsp::Client>>,

diff_handle: Option<DiffHandle>,

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

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

Expand Down Expand Up @@ -768,6 +772,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 @@ -993,6 +993,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);
}
Expand Down Expand Up @@ -1058,6 +1059,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 => {
Expand All @@ -1079,6 +1081,7 @@ impl Editor {
// initialize selection for view
let doc = doc_mut!(self, &id);
doc.ensure_view_init(view_id);
doc.mark_as_used();
}
}

Expand Down

0 comments on commit 734489c

Please sign in to comment.