@@ -3,7 +3,10 @@ use helix_core::{
33 pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection, Transaction,
44};
55
6−use std::{collections::VecDeque, fmt};
6+use std::{
7+ collections::{HashMap, VecDeque},
8+ fmt,
9+};
710
811const JUMP_LIST_CAPACITY: usize = 30;
912
@@ -102,6 +105,11 @@ pub struct View {
102105 pub object_selections: Vec<Selection>,
103106 /// GutterTypes used to fetch Gutter (constructor) and width for rendering
104107 gutters: Vec<GutterType>,
108+ /// A mapping between documents and the last history revision the view was updated at.
109+ /// Changes between documents and views are synced lazily when switching windows. This
110+ /// mapping keeps track of the last applied history revision so that only new changes
111+ /// are applied.
112+ doc_revisions: HashMap<DocumentId, usize>,
105113}
106114
107115impl fmt::Debug for View {
@@ -126,6 +134,7 @@ impl View {
126134 last_modified_docs: [None, None],
127135 object_selections: Vec::new(),
128136 gutters: gutter_types,
137+ doc_revisions: HashMap::new(),
129138 }
130139 }
131140
@@ -349,10 +358,33 @@ impl View {
349358 /// Applies a [`Transaction`] to the view.
350359 /// Instead of calling this function directly, use [crate::apply_transaction]
351360 /// which applies a transaction to the [`Document`] and view together.
352− pub fn apply(&mut self, transaction: &Transaction, doc: &Document) -> bool {
361+ pub fn apply(&mut self, transaction: &Transaction, doc: &mut Document) {
353362 self.jumps.apply(transaction, doc);
354− // TODO: remove the boolean return. This is unused.
355− true
363+ self.doc_revisions
364+ .insert(doc.id(), doc.get_current_revision());
365+ }
366+
367+ pub fn sync_changes(&mut self, doc: &mut Document) {
368+ let latest_revision = doc.get_current_revision();
369+ let current_revision = *self
370+ .doc_revisions
371+ .entry(doc.id())
372+ .or_insert(latest_revision);
373+
374+ if current_revision == latest_revision {
375+ return;
376+ }
377+
378+ log::debug!(
379+ "Syncing view {:?} between {} and {}",
380+ self.id,
381+ current_revision,
382+ latest_revision
383+ );
384+
385+ if let Some(transaction) = doc.history.get_mut().changes_since(current_revision) {
386+ self.apply(&transaction, doc);
387+ }
356388 }
357389}
358390