on master help sign in
Drop doc.state. Use doc.text + doc.selections.
15c9a33Blaž Hrastnik
helix-view/src/document.rs+18−15
@@ -22,7 +22,7 @@ pub enum Mode {
22pub struct Document {
22pub struct Document {
23 // rope + selection
23 // rope + selection
24 pub(crate) id: DocumentId,
24 pub(crate) id: DocumentId,
25− state: State,
25+ text: Rope,
26 pub(crate) selections: HashMap<ViewId, Selection>,
26 pub(crate) selections: HashMap<ViewId, Selection>,
27
27
28 path: Option<PathBuf>,
28 path: Option<PathBuf>,
@@ -75,7 +75,7 @@ impl Document {
75 Self {
75 Self {
76 id: DocumentId::default(),
76 id: DocumentId::default(),
77 path: None,
77 path: None,
78− state: State::new(text),
78+ text,
79 selections: HashMap::default(),
79 selections: HashMap::default(),
80 mode: Mode::Normal,
80 mode: Mode::Normal,
81 restore_cursor: false,
81 restore_cursor: false,
@@ -160,7 +160,7 @@ impl Document {
160 ) {
160 ) {
161 if let Some(language_config) = language_config {
161 if let Some(language_config) = language_config {
162 if let Some(highlight_config) = language_config.highlight_config(scopes) {
162 if let Some(highlight_config) = language_config.highlight_config(scopes) {
163− let syntax = Syntax::new(&self.state.doc, highlight_config);
163+ let syntax = Syntax::new(&self.text, highlight_config);
164 self.syntax = Some(syntax);
164 self.syntax = Some(syntax);
165 // TODO: config.configure(scopes) is now delayed, is that ok?
165 // TODO: config.configure(scopes) is now delayed, is that ok?
166 }
166 }
@@ -190,7 +190,7 @@ impl Document {
190 fn _apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
190 fn _apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
191 let old_doc = self.text().clone();
191 let old_doc = self.text().clone();
192
192
193− let success = transaction.changes().apply(&mut self.state.doc);
193+ let success = transaction.changes().apply(&mut self.text);
194
194
195 if !transaction.changes().is_empty() {
195 if !transaction.changes().is_empty() {
196 // update the selection: either take the selection specified in the transaction, or map the
196 // update the selection: either take the selection specified in the transaction, or map the
@@ -207,7 +207,7 @@ impl Document {
207 if let Some(syntax) = &mut self.syntax {
207 if let Some(syntax) = &mut self.syntax {
208 // TODO: no unwrap
208 // TODO: no unwrap
209 syntax
209 syntax
210− .update(&old_doc, &self.state.doc, transaction.changes())
210+ .update(&old_doc, &self.text, transaction.changes())
211 .unwrap();
211 .unwrap();
212 }
212 }
213
213
@@ -236,7 +236,10 @@ impl Document {
236 // store the state just before any changes are made. This allows us to undo to the
236 // store the state just before any changes are made. This allows us to undo to the
237 // state just before a transaction was applied.
237 // state just before a transaction was applied.
238 if self.changes.is_empty() && !transaction.changes().is_empty() {
238 if self.changes.is_empty() && !transaction.changes().is_empty() {
239− self.old_state = Some(self.state.clone());
239+ self.old_state = Some(State {
240+ doc: self.text.clone(),
241+ selection: self.selection(view_id).clone(),
242+ });
240 }
243 }
241
244
242 let success = self._apply(&transaction, view_id);
245 let success = self._apply(&transaction, view_id);
@@ -365,7 +368,7 @@ impl Document {
365 }
368 }
366
369
367 pub fn text(&self) -> &Rope {
370 pub fn text(&self) -> &Rope {
368− &self.state.doc
371+ &self.text
369 }
372 }
370
373
371 pub fn selection(&self, view_id: ViewId) -> &Selection {
374 pub fn selection(&self, view_id: ViewId) -> &Selection {
@@ -412,9 +415,9 @@ mod test {
412 // insert
415 // insert
413
416
414 let transaction = Transaction::insert(doc.text(), doc.selection(view), " world".into());
417 let transaction = Transaction::insert(doc.text(), doc.selection(view), " world".into());
415− let old_doc = doc.state.clone();
418+ let old_doc = doc.text().clone();
416 doc.apply(&transaction, view);
419 doc.apply(&transaction, view);
417− let changes = Client::changeset_to_changes(&old_doc.doc, doc.text(), transaction.changes());
420+ let changes = Client::changeset_to_changes(&old_doc, doc.text(), transaction.changes());
418
421
419 assert_eq!(
422 assert_eq!(
420 changes,
423 changes,
@@ -430,10 +433,10 @@ mod test {
430
433
431 // delete
434 // delete
432
435
433− let transaction = transaction.invert(&old_doc.doc);
436+ let transaction = transaction.invert(&old_doc);
434− let old_doc = doc.state.clone();
437+ let old_doc = doc.text().clone();
435 doc.apply(&transaction, view);
438 doc.apply(&transaction, view);
436− let changes = Client::changeset_to_changes(&old_doc.doc, doc.text(), transaction.changes());
439+ let changes = Client::changeset_to_changes(&old_doc, doc.text(), transaction.changes());
437
440
438 // line: 0-based.
441 // line: 0-based.
439 // col: 0-based, gaps between chars.
442 // col: 0-based, gaps between chars.
@@ -459,13 +462,13 @@ mod test {
459
462
460 doc.set_selection(view, Selection::single(0, 5));
463 doc.set_selection(view, Selection::single(0, 5));
461 let transaction = Transaction::change(
464 let transaction = Transaction::change(
462− &doc.state.doc,
465+ doc.text(),
463 vec![(0, 2, Some("aei".into())), (3, 5, Some("ou".into()))].into_iter(),
466 vec![(0, 2, Some("aei".into())), (3, 5, Some("ou".into()))].into_iter(),
464 );
467 );
465 // aeilou
468 // aeilou
469+ let old_doc = doc.text().clone();
466 doc.apply(&transaction, view);
470 doc.apply(&transaction, view);
467− let changes =
468− Client::changeset_to_changes(&doc.state.doc, doc.text(), transaction.changes());
471+ let changes = Client::changeset_to_changes(&old_doc, doc.text(), transaction.changes());
469
472
470 assert_eq!(
473 assert_eq!(
471 changes,
474 changes,