Skip to content

Commit

Permalink
Sort the buffer picker by most recent access (#2980)
Browse files Browse the repository at this point in the history
  • Loading branch information
estin committed Apr 28, 2023
1 parent 9c6c63a commit 6a1bb81
Show file tree
Hide file tree
Showing 3 changed files with 30 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 @@ -2473,6 +2473,7 @@ fn buffer_picker(cx: &mut Context) {
path: Option<PathBuf>,
is_modified: bool,
is_current: bool,
focused_at: std::time::Instant,
}

impl ui::menu::Item for BufferMeta {
Expand Down Expand Up @@ -2505,14 +2506,21 @@ fn buffer_picker(cx: &mut Context) {
path: doc.path().cloned(),
is_modified: doc.is_modified(),
is_current: doc.id() == current,
focused_at: doc.focused_at,
};

let mut items = cx
.editor
.documents
.values()
.map(|doc| new_meta(doc))
.collect::<Vec<BufferMeta>>();

// mru
items.sort_unstable_by_key(|item| std::cmp::Reverse(item.focused_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 @@ -169,6 +169,9 @@ pub struct Document {

diff_handle: Option<DiffHandle>,
version_control_head: Option<Arc<ArcSwap<Box<str>>>>,

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

/// Inlay hints for a single `(Document, View)` combo.
Expand Down Expand Up @@ -496,6 +499,7 @@ impl Document {
diff_handle: None,
config,
version_control_head: None,
focused_at: std::time::Instant::now(),
}
}
pub fn default(config: Arc<dyn DynAccess<Config>>) -> Self {
Expand Down Expand Up @@ -908,6 +912,11 @@ impl Document {
}
}

/// Mark document as recent used for MRU sorting
pub fn mark_as_focused(&mut self) {
self.focused_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);
Expand Down
8 changes: 8 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_focused();

align_view(doc, view, Align::Center);
}
Expand Down Expand Up @@ -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_focused();
return;
}
Action::HorizontalSplit | Action::VerticalSplit => {
Expand All @@ -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_focused();
}
}

Expand Down Expand Up @@ -1417,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();
Expand Down Expand Up @@ -1471,6 +1475,10 @@ impl Editor {
view.sync_changes(doc);
}
}

let view = view!(self, view_id);
let doc = doc_mut!(self, &view.doc);
doc.mark_as_focused();
}

pub fn focus_next(&mut self) {
Expand Down

0 comments on commit 6a1bb81

Please sign in to comment.