@@ -667,7 +667,7 @@ impl Editor {
667667 view.jumps.push(jump);
668668 // Set last accessed doc if it is a different document
669669 if doc.id != id {
670− view.last_accessed_doc = Some(view.doc);
670+ view.add_to_history(view.doc);
671671 // Set last modified doc if modified and last modified doc is different
672672 if std::mem::take(&mut doc.modified_since_accessed)
673673 && view.last_modified_docs[0] != Some(view.doc)
@@ -785,20 +785,41 @@ impl Editor {
785785 tokio::spawn(language_server.text_document_did_close(doc.identifier()));
786786 }
787787
788− let views_to_close = self
788+ enum Action {
789+ Close(ViewId),
790+ ReplaceDoc(ViewId, DocumentId),
791+ }
792+
793+ let actions: Vec<Action> = self
789794 .tree
790− .views()
795+ .views_mut()
791796 .filter_map(|(view, _focus)| {
797+ // remove the document from jump list of all views
798+ view.jumps.remove(&doc_id);
799+
792800 if view.doc == doc_id {
793− Some(view.id)
801+ // something was previously open in the view, switch to previous doc
802+ if let Some(prev_doc) = view.docs_access_history.pop() {
803+ Some(Action::ReplaceDoc(view.id, prev_doc))
804+ } else {
805+ // only the document that is being closed was in the view, close it
806+ Some(Action::Close(view.id))
807+ }
794808 } else {
795809 None
796810 }
797811 })
798− .collect::<Vec<_>>();
812+ .collect();
799813
800− for view_id in views_to_close {
801− self.close(view_id);
814+ for action in actions {
815+ match action {
816+ Action::Close(view_id) => {
817+ self.close(view_id);
818+ }
819+ Action::ReplaceDoc(view_id, doc_id) => {
820+ self.replace_document_in_view(view_id, doc_id);
821+ }
822+ }
802823 }
803824
804825 self.documents.remove(&doc_id);