Skip to content

Commit

Permalink
Adjust buffer close behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
matoous committed Jan 4, 2022
1 parent 20c94dd commit 2a1fa0f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
8 changes: 2 additions & 6 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3839,8 +3839,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".to_owned())
Expand Down Expand Up @@ -5478,10 +5478,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
19 changes: 8 additions & 11 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,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(id)
Expand Down Expand Up @@ -515,18 +515,15 @@ impl Editor {

let actions: Vec<Action> = self
.tree
.views()
.views_mut()
.filter_map(|(view, _focus)| {
if view.doc == doc_id {
match view.last_accessed_doc {
Some(doc_id) => {
// something was previously o[en in the view, switch to previous doc
Some(Action::ReplaceDoc(view.id, doc_id))
}
None => {
// only the document that is being closed was in the view, close it
Some(Action::Close(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
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 @@ -73,8 +73,8 @@ pub struct View {
pub offset: Position,
pub area: Rect,
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 All @@ -90,11 +90,18 @@ 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],
}
}

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);
}

pub fn gutters(&self) -> &[(Gutter, usize)] {
GUTTERS
}
Expand Down

0 comments on commit 2a1fa0f

Please sign in to comment.