make sure to sync views when applying edits to unfocused views (#9173)

This commit is contained in:
Gabriel Dinner-David 2024-01-08 20:21:16 -05:00 committed by GitHub
parent 65d0412880
commit 84e24b33dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 34 deletions

View file

@ -896,7 +896,6 @@ pub fn apply_workspace_edit(
} }
}; };
let current_view_id = view!(editor).id;
let doc_id = match editor.open(&path, Action::Load) { let doc_id = match editor.open(&path, Action::Load) {
Ok(doc_id) => doc_id, Ok(doc_id) => doc_id,
Err(err) => { Err(err) => {
@ -907,7 +906,7 @@ pub fn apply_workspace_edit(
} }
}; };
let doc = doc_mut!(editor, &doc_id); let doc = doc!(editor, &doc_id);
if let Some(version) = version { if let Some(version) = version {
if version != doc.version() { if version != doc.version() {
let err = format!("outdated workspace edit for {path:?}"); let err = format!("outdated workspace edit for {path:?}");
@ -918,18 +917,8 @@ pub fn apply_workspace_edit(
} }
// Need to determine a view for apply/append_changes_to_history // Need to determine a view for apply/append_changes_to_history
let selections = doc.selections(); let view_id = editor.get_synced_view_id(doc_id);
let view_id = if selections.contains_key(&current_view_id) { let doc = doc_mut!(editor, &doc_id);
// use current if possible
current_view_id
} else {
// Hack: we take the first available view_id
selections
.keys()
.next()
.copied()
.expect("No view_id available")
};
let transaction = helix_lsp::util::generate_transaction_from_edits( let transaction = helix_lsp::util::generate_transaction_from_edits(
doc.text(), doc.text(),

View file

@ -674,13 +674,15 @@ pub fn write_all_impl(
let mut errors: Vec<&'static str> = Vec::new(); let mut errors: Vec<&'static str> = Vec::new();
let config = cx.editor.config(); let config = cx.editor.config();
let jobs = &mut cx.jobs; let jobs = &mut cx.jobs;
let current_view = view!(cx.editor);
let saves: Vec<_> = cx let saves: Vec<_> = cx
.editor .editor
.documents .documents
.values_mut() .keys()
.filter_map(|doc| { .cloned()
.collect::<Vec<_>>()
.into_iter()
.filter_map(|id| {
let doc = doc!(cx.editor, &id);
if !doc.is_modified() { if !doc.is_modified() {
return None; return None;
} }
@ -691,22 +693,9 @@ pub fn write_all_impl(
return None; return None;
} }
// Look for a view to apply the formatting change to. If the document // Look for a view to apply the formatting change to.
// is in the current view, just use that. Otherwise, since we don't let target_view = cx.editor.get_synced_view_id(doc.id());
// have any other metric available for better selection, just pick Some((id, target_view))
// the first view arbitrarily so that we still commit the document
// state for undos. If somehow we have a document that has not been
// initialized with any view, initialize it with the current view.
let target_view = if doc.selections().contains_key(&current_view.id) {
current_view.id
} else if let Some(view) = doc.selections().keys().next() {
*view
} else {
doc.ensure_view_init(current_view.id);
current_view.id
};
Some((doc.id(), target_view))
}) })
.collect(); .collect();

View file

@ -1910,6 +1910,30 @@ impl Editor {
.as_ref() .as_ref()
.and_then(|debugger| debugger.current_stack_frame()) .and_then(|debugger| debugger.current_stack_frame())
} }
/// Returns the id of a view that this doc contains a selection for,
/// making sure it is synced with the current changes
/// if possible or there are no selections returns current_view
/// otherwise uses an arbitrary view
pub fn get_synced_view_id(&mut self, id: DocumentId) -> ViewId {
let current_view = view_mut!(self);
let doc = self.documents.get_mut(&id).unwrap();
if doc.selections().contains_key(&current_view.id) {
// only need to sync current view if this is not the current doc
if current_view.doc != id {
current_view.sync_changes(doc);
}
current_view.id
} else if let Some(view_id) = doc.selections().keys().next() {
let view_id = *view_id;
let view = self.tree.get_mut(view_id);
view.sync_changes(doc);
view_id
} else {
doc.ensure_view_init(current_view.id);
current_view.id
}
}
} }
fn try_restore_indent(doc: &mut Document, view: &mut View) { fn try_restore_indent(doc: &mut Document, view: &mut View) {