on master help sign in
Sync changes between doc and view on switch
056a19aMichael Davis
helix-view/src/editor.rs+6−1
@@ -959,7 +959,8 @@ impl Editor {
959 fn _refresh(&mut self) {
959 fn _refresh(&mut self) {
960 let config = self.config();
960 let config = self.config();
961 for (view, _) in self.tree.views_mut() {
961 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);
963 view.ensure_cursor_in_view(doc, config.scrolloff)
964 view.ensure_cursor_in_view(doc, config.scrolloff)
964 }
965 }
965 }
966 }
@@ -971,6 +972,7 @@ impl Editor {
971
972
972 let doc = doc_mut!(self, &doc_id);
973 let doc = doc_mut!(self, &doc_id);
973 doc.ensure_view_init(view.id);
974 doc.ensure_view_init(view.id);
975+ view.sync_changes(doc);
974
976
975 align_view(doc, view, Align::Center);
977 align_view(doc, view, Align::Center);
976 }
978 }
@@ -1239,6 +1241,9 @@ impl Editor {
1239 // within view
1241 // within view
1240 if prev_id != view_id {
1242 if prev_id != view_id {
1241 self.mode = Mode::Normal;
1243 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);
1242 self.ensure_cursor_in_view(view_id);
1247 self.ensure_cursor_in_view(view_id);
1243 }
1248 }
1244 }
1249 }
helix-view/src/view.rs+36−4
@@ -3,7 +3,10 @@ use helix_core::{
3 pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection, Transaction,
3 pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection, Transaction,
4};
4};
5
5
6+use std::{
6−use std::{collections::VecDeque, fmt};
7+ collections::{HashMap, VecDeque},
8+ fmt,
9+};
7
10
8const JUMP_LIST_CAPACITY: usize = 30;
11const JUMP_LIST_CAPACITY: usize = 30;
9
12
@@ -102,6 +105,11 @@ pub struct View {
102 pub object_selections: Vec<Selection>,
105 pub object_selections: Vec<Selection>,
103 /// GutterTypes used to fetch Gutter (constructor) and width for rendering
106 /// GutterTypes used to fetch Gutter (constructor) and width for rendering
104 gutters: Vec<GutterType>,
107 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>,
105}
113}
106
114
107impl fmt::Debug for View {
115impl fmt::Debug for View {
@@ -126,6 +134,7 @@ impl View {
126 last_modified_docs: [None, None],
134 last_modified_docs: [None, None],
127 object_selections: Vec::new(),
135 object_selections: Vec::new(),
128 gutters: gutter_types,
136 gutters: gutter_types,
137+ doc_revisions: HashMap::new(),
129 }
138 }
130 }
139 }
131
140
@@ -349,10 +358,33 @@ impl View {
349 /// Applies a [`Transaction`] to the view.
358 /// Applies a [`Transaction`] to the view.
350 /// Instead of calling this function directly, use [crate::apply_transaction]
359 /// Instead of calling this function directly, use [crate::apply_transaction]
351 /// which applies a transaction to the [`Document`] and view together.
360 /// 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) {
353 self.jumps.apply(transaction, doc);
362 self.jumps.apply(transaction, doc);
354− // TODO: remove the boolean return. This is unused.
363+ self.doc_revisions
355− true
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+ }
356 }
388 }
357}
389}
358
390