Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands): better handling of buffer-close #1397

Merged
merged 4 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2472,8 +2472,8 @@ fn goto_last_line(cx: &mut Context) {
}

fn goto_last_accessed_file(cx: &mut Context) {
let alternate_file = view!(cx.editor).last_accessed_doc;
if let Some(alt) = alternate_file {
let view = view_mut!(cx.editor);
if let Some(alt) = view.docs_access_history.pop() {
cx.editor.switch(alt, Action::Replace);
} else {
cx.editor.set_error("no last accessed buffer")
Expand Down Expand Up @@ -3779,10 +3779,6 @@ fn jump_backward(cx: &mut Context) {
let (view, doc) = current!(cx.editor);

if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) {
// manually set the alternate_file as we cannot use the Editor::switch function here.
if view.doc != *id {
view.last_accessed_doc = Some(view.doc)
}
view.doc = *id;
let selection = selection.clone();
let (view, doc) = current!(cx.editor); // refetch doc
Expand Down
35 changes: 28 additions & 7 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ impl Editor {
view.jumps.push(jump);
// Set last accessed doc if it is a different document
if doc.id != id {
view.last_accessed_doc = Some(view.doc);
view.add_to_history(view.doc);
// Set last modified doc if modified and last modified doc is different
if std::mem::take(&mut doc.modified_since_accessed)
&& view.last_modified_docs[0] != Some(view.doc)
Expand Down Expand Up @@ -785,20 +785,41 @@ impl Editor {
tokio::spawn(language_server.text_document_did_close(doc.identifier()));
}

let views_to_close = self
enum Action {
Close(ViewId),
ReplaceDoc(ViewId, DocumentId),
}

let actions: Vec<Action> = self
.tree
.views()
.views_mut()
.filter_map(|(view, _focus)| {
// remove the document from jump list of all views
view.jumps.remove(&doc_id);

if view.doc == doc_id {
Some(view.id)
// something was previously open in the view, switch to previous doc
if let Some(prev_doc) = view.docs_access_history.pop() {
Some(Action::ReplaceDoc(view.id, prev_doc))
} else {
// only the document that is being closed was in the view, close it
Some(Action::Close(view.id))
}
} else {
None
}
})
.collect::<Vec<_>>();
.collect();

for view_id in views_to_close {
self.close(view_id);
for action in actions {
match action {
Action::Close(view_id) => {
self.close(view_id);
}
Action::ReplaceDoc(view_id, doc_id) => {
self.replace_document_in_view(view_id, doc_id);
matoous marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

self.documents.remove(&doc_id);
Expand Down
13 changes: 10 additions & 3 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ pub struct View {
pub area: Rect,
pub doc: DocumentId,
pub jumps: JumpList,
/// the last accessed file before the current one
pub last_accessed_doc: Option<DocumentId>,
// documents accessed from this view from the oldest one to last viewed one
pub docs_access_history: Vec<DocumentId>,
/// the last modified files before the current one
/// ordered from most frequent to least frequent
// uses two docs because we want to be able to swap between the
Expand Down Expand Up @@ -110,13 +110,20 @@ impl View {
offset: Position::new(0, 0),
area: Rect::default(), // will get calculated upon inserting into tree
jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
last_accessed_doc: None,
docs_access_history: Vec::new(),
last_modified_docs: [None, None],
object_selections: Vec::new(),
gutters,
}
}

pub fn add_to_history(&mut self, id: DocumentId) {
if let Some(pos) = self.docs_access_history.iter().position(|&doc| doc == id) {
self.docs_access_history.remove(pos);
}
self.docs_access_history.push(id);
}
Comment on lines +120 to +125
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it could continually grow and leak memory. It would be good to truncate to a certain max length.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if above prevents a single document from being multiple times in history, thus the size should always be <= number of documents and those are un-capped too as far as I am aware, I don't think the limit is strictly necessary here, but I can add it for sure.


pub fn inner_area(&self) -> Rect {
// TODO: cache this
let offset = self
Expand Down