on master help sign in
Sync changes between doc and view on switch
commit056a19a003a290b7914c7b442ebd640b93eaba0c authorMichael Davis <mcarsondavis@gmail.com> dateSun 27 Nov 2022, 10:45 (4 years ago) parent4d1f538
2 files changed+42−5
helix-view/src/editor.rs+6−1 helix-view/src/view.rs+36−4
helix-view/src/editor.rs+6−1
@@ -959,7 +959,8 @@ impl Editor {
959959 fn _refresh(&mut self) {
960960 let config = self.config();
961961 for (view, _) in self.tree.views_mut() {
962− let doc = &self.documents[&view.doc];
962+ let doc = doc_mut!(self, &view.doc);
963+ view.sync_changes(doc);
963964 view.ensure_cursor_in_view(doc, config.scrolloff)
964965 }
965966 }
@@ -971,6 +972,7 @@ impl Editor {
971972
972973 let doc = doc_mut!(self, &doc_id);
973974 doc.ensure_view_init(view.id);
975+ view.sync_changes(doc);
974976
975977 align_view(doc, view, Align::Center);
976978 }
@@ -1239,6 +1241,9 @@ impl Editor {
12391241 // within view
12401242 if prev_id != view_id {
12411243 self.mode = Mode::Normal;
1244+ let view = view_mut!(self, view_id);
1245+ let doc = doc_mut!(self, &view.doc);
1246+ view.sync_changes(doc);
12421247 self.ensure_cursor_in_view(view_id);
12431248 }
12441249 }
helix-view/src/view.rs+36−4
@@ -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