on master help sign in
Fully drop State references.
commit9eaef6e33376407931162780cd402297c44f26c4 authorBlaž Hrastnik <blaz@mxxn.io> dateWed 31 Mar 2021, 15:45 (6 years ago) parentdfc17be
5 files changed+48−37
helix-core/src/comment.rs+4−2 helix-core/src/history.rs+8−5 helix-core/src/syntax.rs+3−3 helix-core/src/transaction.rs+23−25 helix-view/src/document.rs+10−2
helix-core/src/comment.rs+4−2
@@ -96,13 +96,15 @@ mod test {
9696
9797 // comment
9898 let transaction = toggle_line_comments(&state.doc, &state.selection);
99− transaction.apply(&mut state);
99+ transaction.apply(&mut state.doc);
100+ state.selection = state.selection.clone().map(transaction.changes());
100101
101102 assert_eq!(state.doc, " // 1\n\n // 2\n // 3");
102103
103104 // uncomment
104105 let transaction = toggle_line_comments(&state.doc, &state.selection);
105− transaction.apply(&mut state);
106+ transaction.apply(&mut state.doc);
107+ state.selection = state.selection.clone().map(transaction.changes());
106108 assert_eq!(state.doc, " 1\n\n 2\n 3");
107109
108110 // TODO: account for no margin after comment
helix-core/src/history.rs+8−5
@@ -33,7 +33,10 @@ impl Default for History {
3333impl History {
3434 pub fn commit_revision(&mut self, transaction: &Transaction, original: &State) {
3535 // TODO: could store a single transaction, if deletes also stored the text they delete
36− let revert = transaction.invert(original);
36+ let revert = transaction
37+ .invert(&original.doc)
38+ // Store the current cursor position
39+ .with_selection(original.selection.clone());
3740
3841 let new_cursor = self.revisions.len();
3942 self.revisions.push(Revision {
@@ -100,7 +103,7 @@ mod test {
100103
101104 // Need to commit before applying!
102105 history.commit_revision(&transaction1, &state);
103− transaction1.apply(&mut state);
106+ transaction1.apply(&mut state.doc);
104107 assert_eq!("hello world!", state.doc);
105108
106109 // ---
@@ -110,18 +113,18 @@ mod test {
110113
111114 // Need to commit before applying!
112115 history.commit_revision(&transaction2, &state);
113− transaction2.apply(&mut state);
116+ transaction2.apply(&mut state.doc);
114117 assert_eq!("hello 世界!", state.doc);
115118
116119 // ---
117120 fn undo(history: &mut History, state: &mut State) {
118121 if let Some(transaction) = history.undo() {
119− transaction.apply(state);
122+ transaction.apply(&mut state.doc);
120123 }
121124 }
122125 fn redo(history: &mut History, state: &mut State) {
123126 if let Some(transaction) = history.redo() {
124− transaction.apply(state);
127+ transaction.apply(&mut state.doc);
125128 }
126129 }
127130
helix-core/src/syntax.rs+3−3
@@ -1593,7 +1593,7 @@ fn test_input_edits() {
15931593 &state.doc,
15941594 vec![(6, 11, Some("test".into())), (12, 17, None)].into_iter(),
15951595 );
1596− let edits = LanguageLayer::generate_edits(state.doc.slice(..), &transaction.changes);
1596+ let edits = LanguageLayer::generate_edits(state.doc.slice(..), transaction.changes());
15971597 // transaction.apply(&mut state);
15981598
15991599 assert_eq!(
@@ -1622,8 +1622,8 @@ fn test_input_edits() {
16221622 let mut state = State::new("fn test() {}".into());
16231623 let transaction =
16241624 Transaction::change(&state.doc, vec![(8, 8, Some("a: u32".into()))].into_iter());
1625− let edits = LanguageLayer::generate_edits(state.doc.slice(..), &transaction.changes);
1626− transaction.apply(&mut state);
1625+ let edits = LanguageLayer::generate_edits(state.doc.slice(..), transaction.changes());
1626+ transaction.apply(&mut state.doc);
16271627
16281628 assert_eq!(state.doc, "fn test(a: u32) {}");
16291629 assert_eq!(
helix-core/src/transaction.rs+23−25
@@ -400,9 +400,7 @@ impl ChangeSet {
400400/// a single transaction.
401401#[derive(Debug, Clone)]
402402pub struct Transaction {
403− /// Changes made to the buffer.
404− pub(crate) changes: ChangeSet,
405− /// When set, explicitly updates the selection.
403+ changes: ChangeSet,
406404 selection: Option<Selection>,
407405 // effects, annotations
408406 // scroll_into_view
@@ -417,40 +415,35 @@ impl Transaction {
417415 }
418416 }
419417
418+ /// Changes made to the buffer.
420419 pub fn changes(&self) -> &ChangeSet {
421420 &self.changes
422421 }
423422
423+ /// When set, explicitly updates the selection.
424+ pub fn selection(&self) -> Option<&Selection> {
425+ self.selection.as_ref()
426+ }
427+
424428 /// Returns true if applied successfully.
425− pub fn apply(&self, state: &mut State) -> bool {
429+ pub fn apply(&self, doc: &mut Rope) -> bool {
426430 if !self.changes.is_empty() {
427431 // apply changes to the document
428− if !self.changes.apply(&mut state.doc) {
432+ if !self.changes.apply(doc) {
429433 return false;
430434 }
431435 }
432436
433− // TODO: also avoid mapping the selection if not necessary
434−
435− // update the selection: either take the selection specified in the transaction, or map the
436− // current selection through changes.
437− state.selection = self
438− .selection
439− .clone()
440− .unwrap_or_else(|| state.selection.clone().map(&self.changes));
441−
442437 true
443438 }
444439
445440 /// Generate a transaction that reverts this one.
446− pub fn invert(&self, original: &State) -> Self {
447− let changes = self.changes.invert(&original.doc);
448− // Store the current cursor position
449− let selection = original.selection.clone();
441+ pub fn invert(&self, original: &Rope) -> Self {
442+ let changes = self.changes.invert(original);
450443
451444 Self {
452445 changes,
453− selection: Some(selection),
446+ selection: None,
454447 }
455448 }
456449
@@ -675,7 +668,7 @@ mod test {
675668 // (1, 1, None) is a useless 0-width delete
676669 vec![(1, 1, None), (6, 11, Some("void".into())), (12, 17, None)].into_iter(),
677670 );
678− transaction.apply(&mut state);
671+ transaction.apply(&mut state.doc);
679672 assert_eq!(state.doc, Rope::from_str("hello void! 123"));
680673 }
681674
@@ -691,15 +684,20 @@ mod test {
691684 fn optimized_composition() {
692685 let mut state = State::new("".into());
693686 let t1 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('h'));
694− t1.apply(&mut state);
687+ t1.apply(&mut state.doc);
688+ state.selection = state.selection.clone().map(t1.changes());
695689 let t2 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('e'));
696− t2.apply(&mut state);
690+ t2.apply(&mut state.doc);
691+ state.selection = state.selection.clone().map(t2.changes());
697692 let t3 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('l'));
698− t3.apply(&mut state);
693+ t3.apply(&mut state.doc);
694+ state.selection = state.selection.clone().map(t3.changes());
699695 let t4 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('l'));
700− t4.apply(&mut state);
696+ t4.apply(&mut state.doc);
697+ state.selection = state.selection.clone().map(t4.changes());
701698 let t5 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('o'));
702− t5.apply(&mut state);
699+ t5.apply(&mut state.doc);
700+ state.selection = state.selection.clone().map(t5.changes());
703701
704702 assert_eq!(state.doc, Rope::from_str("hello"));
705703
helix-view/src/document.rs+10−2
@@ -21,6 +21,7 @@ pub struct Document {
2121 // rope + selection
2222 pub(crate) id: DocumentId,
2323 state: State,
24+
2425 path: Option<PathBuf>,
2526
2627 /// Current editing mode.
@@ -185,9 +186,16 @@ impl Document {
185186 fn _apply(&mut self, transaction: &Transaction) -> bool {
186187 let old_doc = self.text().clone();
187188
188− let success = transaction.apply(&mut self.state);
189+ let success = transaction.changes().apply(&mut self.state.doc);
189190
190191 if !transaction.changes().is_empty() {
192+ // update the selection: either take the selection specified in the transaction, or map the
193+ // current selection through changes.
194+ self.state.selection = transaction
195+ .selection()
196+ .cloned()
197+ .unwrap_or_else(|| self.selection().clone().map(transaction.changes()));
198+
191199 self.version += 1;
192200
193201 // update tree-sitter syntax tree
@@ -415,7 +423,7 @@ mod test {
415423
416424 // delete
417425
418− let transaction = transaction.invert(&old_doc);
426+ let transaction = transaction.invert(&old_doc.doc);
419427 let old_doc = doc.state.clone();
420428 doc.apply(&transaction);
421429 let changes = Client::changeset_to_changes(&old_doc.doc, doc.text(), transaction.changes());