fix quirky file persistence behaviour

pull/9143/head
Ingrid 9 months ago
parent 5f3849dc24
commit 69a13fbba2

@ -247,8 +247,8 @@ impl Application {
)); ));
// align the view to center after all files are loaded, // align the view to center after all files are loaded,
// does not affect views without pos since it is at the top // does not affect views without pos since it is at the top
let (view, doc) = current!(editor); // let (view, doc) = current!(editor);
align_view(doc, view, Align::Center); // align_view(doc, view, Align::Center);
} }
} else { } else {
editor.new_file(Action::VerticalSplit); editor.new_file(Action::VerticalSplit);

@ -134,7 +134,8 @@ fn open(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) ->
doc.set_selection(view.id, pos); doc.set_selection(view.id, pos);
} }
// does not affect opening a buffer without pos // does not affect opening a buffer without pos
align_view(doc, view, Align::Center); // TODO: ensure removing this will not cause problems
// align_view(doc, view, Align::Center);
} }
} }
Ok(()) Ok(())

@ -11,6 +11,7 @@ use crate::{
register::Registers, register::Registers,
theme::{self, Theme}, theme::{self, Theme},
tree::{self, Tree}, tree::{self, Tree},
view::ViewPosition,
Document, DocumentId, View, ViewId, Document, DocumentId, View, ViewId,
}; };
use dap::StackFrame; use dap::StackFrame;
@ -1540,6 +1541,7 @@ impl Editor {
fn replace_document_in_view(&mut self, current_view: ViewId, doc_id: DocumentId) { fn replace_document_in_view(&mut self, current_view: ViewId, doc_id: DocumentId) {
let scrolloff = self.config().scrolloff; let scrolloff = self.config().scrolloff;
let view = self.tree.get_mut(current_view); let view = self.tree.get_mut(current_view);
view.doc = doc_id;
view.doc = doc_id; view.doc = doc_id;
let doc = doc_mut!(self, &doc_id); let doc = doc_mut!(self, &doc_id);
@ -1641,16 +1643,6 @@ impl Editor {
// initialize selection for view // initialize selection for view
let doc = doc_mut!(self, &id); let doc = doc_mut!(self, &id);
if let Some((view_position, selection)) = self
.old_file_locs
.get(doc.path().unwrap())
.map(|x| x.to_owned())
{
let view = self.tree.get_mut(view_id);
view.offset = view_position;
doc.set_selection(view_id, selection);
}
doc.ensure_view_init(view_id); doc.ensure_view_init(view_id);
doc.mark_as_focused(); doc.mark_as_focused();
} }
@ -1711,9 +1703,14 @@ impl Editor {
let path = helix_stdx::path::canonicalize(path); let path = helix_stdx::path::canonicalize(path);
let id = self.document_by_path(&path).map(|doc| doc.id); let id = self.document_by_path(&path).map(|doc| doc.id);
// TODO: surely there's a neater way to do this?
let mut new_doc = false;
let id = if let Some(id) = id { let id = if let Some(id) = id {
id id
} else { } else {
new_doc = true;
let mut doc = Document::open( let mut doc = Document::open(
&path, &path,
None, None,
@ -1737,28 +1734,44 @@ impl Editor {
}; };
self.switch(id, action); self.switch(id, action);
if new_doc {
if let Some((view_position, selection)) =
self.old_file_locs.get(&path).map(|x| x.to_owned())
{
let (view, doc) = current!(self);
view.offset = view_position;
doc.set_selection(view.id, selection);
}
}
Ok(id) Ok(id)
} }
pub fn close(&mut self, id: ViewId) { pub fn close(&mut self, id: ViewId) {
let view = self.tree.get(id); let offset = self.tree.get(id).offset.clone();
// TODO: do something about this unwrap
let doc = self.document(view.doc).unwrap(); let mut file_locs = Vec::new();
if let Some(path) = doc.path() {
// TODO: can the arg here be a reference? would save cloning
persistence::push_file_history(FileHistoryEntry::new(
path.clone(),
view.offset,
doc.selection(id).clone(),
));
self.old_file_locs
.insert(path.to_owned(), (view.offset, doc.selection(id).clone()));
};
// Remove selections for the closed view on all documents. // Remove selections for the closed view on all documents.
for doc in self.documents_mut() { for doc in self.documents_mut() {
if let Some(path) = doc.path() {
file_locs.push((path.clone(), offset, doc.selection(id).clone()));
};
doc.remove_view(id); doc.remove_view(id);
} }
for loc in file_locs {
// TODO: can the arg here be a reference? would save cloning
persistence::push_file_history(FileHistoryEntry::new(
loc.0.clone(),
loc.1,
loc.2.clone(),
));
self.old_file_locs.insert(loc.0, (loc.1, loc.2));
}
self.tree.remove(id); self.tree.remove(id);
self._refresh(); self._refresh();
} }
@ -1780,11 +1793,14 @@ impl Editor {
tokio::spawn(language_server.text_document_did_close(doc.identifier())); tokio::spawn(language_server.text_document_did_close(doc.identifier()));
} }
#[derive(Debug)]
enum Action { enum Action {
Close(ViewId), Close(ViewId),
ReplaceDoc(ViewId, DocumentId), ReplaceDoc(ViewId, DocumentId),
} }
let mut file_locs = Vec::new();
let actions: Vec<Action> = self let actions: Vec<Action> = self
.tree .tree
.views_mut() .views_mut()
@ -1792,6 +1808,10 @@ impl Editor {
view.remove_document(&doc_id); view.remove_document(&doc_id);
if view.doc == doc_id { if view.doc == doc_id {
if let Some(path) = doc.path() {
file_locs.push((path.clone(), view.offset, doc.selection(view.id).clone()));
};
// something was previously open in the view, switch to previous doc // something was previously open in the view, switch to previous doc
if let Some(prev_doc) = view.docs_access_history.pop() { if let Some(prev_doc) = view.docs_access_history.pop() {
Some(Action::ReplaceDoc(view.id, prev_doc)) Some(Action::ReplaceDoc(view.id, prev_doc))
@ -1805,6 +1825,16 @@ impl Editor {
}) })
.collect(); .collect();
for loc in file_locs {
// TODO: can the arg here be a reference? would save cloning
persistence::push_file_history(FileHistoryEntry::new(
loc.0.clone(),
loc.1,
loc.2.clone(),
));
self.old_file_locs.insert(loc.0, (loc.1, loc.2));
}
for action in actions { for action in actions {
match action { match action {
Action::Close(view_id) => { Action::Close(view_id) => {

Loading…
Cancel
Save