on master help sign in
Add a helper function for applying transactions
c388e16Michael Davis
helix-term/src/commands.rs+30−61
@@ -27,6 +27,7 @@ use helix_core::{
27 SmallVec, Tendril, Transaction,
27 SmallVec, Tendril, Transaction,
28};
28};
29use helix_view::{
29use helix_view::{
30+ apply_transaction,
30 clipboard::ClipboardType,
31 clipboard::ClipboardType,
31 document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
32 document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
32 editor::{Action, Motion},
33 editor::{Action, Motion},
@@ -859,8 +860,7 @@ fn align_selections(cx: &mut Context) {
859 changes.sort_unstable_by_key(|(from, _, _)| *from);
860 changes.sort_unstable_by_key(|(from, _, _)| *from);
860
861
861 let transaction = Transaction::change(doc.text(), changes.into_iter());
862 let transaction = Transaction::change(doc.text(), changes.into_iter());
862− doc.apply(&transaction, view.id);
863+ apply_transaction(&transaction, doc, view);
863− view.apply(&transaction, doc);
864}
864}
865
865
866fn goto_window(cx: &mut Context, align: Align) {
866fn goto_window(cx: &mut Context, align: Align) {
@@ -1290,8 +1290,7 @@ fn replace(cx: &mut Context) {
1290 }
1290 }
1291 });
1291 });
1292
1292
1293− doc.apply(&transaction, view.id);
1293+ apply_transaction(&transaction, doc, view);
1294− view.apply(&transaction, doc);
1295 }
1294 }
1296 })
1295 })
1297}
1296}
@@ -1308,8 +1307,7 @@ where
1308 (range.from(), range.to(), Some(text))
1307 (range.from(), range.to(), Some(text))
1309 });
1308 });
1310
1309
1311− doc.apply(&transaction, view.id);
1310+ apply_transaction(&transaction, doc, view);
1312− view.apply(&transaction, doc);
1313}
1311}
1314
1312
1315fn switch_case(cx: &mut Context) {
1313fn switch_case(cx: &mut Context) {
@@ -2115,8 +2113,7 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
2115 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
2113 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
2116 (range.from(), range.to(), None)
2114 (range.from(), range.to(), None)
2117 });
2115 });
2118− doc.apply(&transaction, view.id);
2116+ apply_transaction(&transaction, doc, view);
2119− view.apply(&transaction, doc);
2120
2117
2121 match op {
2118 match op {
2122 Operation::Delete => {
2119 Operation::Delete => {
@@ -2131,14 +2128,10 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
2131
2128
2132#[inline]
2129#[inline]
2133fn delete_selection_insert_mode(doc: &mut Document, view: &mut View, selection: &Selection) {
2130fn delete_selection_insert_mode(doc: &mut Document, view: &mut View, selection: &Selection) {
2134− let view_id = view.id;
2135−
2136− // then delete
2137 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
2131 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
2138 (range.from(), range.to(), None)
2132 (range.from(), range.to(), None)
2139 });
2133 });
2140− doc.apply(&transaction, view_id);
2134+ apply_transaction(&transaction, doc, view);
2141− view.apply(&transaction, doc);
2142}
2135}
2143
2136
2144fn delete_selection(cx: &mut Context) {
2137fn delete_selection(cx: &mut Context) {
@@ -2234,8 +2227,7 @@ fn append_mode(cx: &mut Context) {
2234 doc.text(),
2227 doc.text(),
2235 [(end, end, Some(doc.line_ending.as_str().into()))].into_iter(),
2228 [(end, end, Some(doc.line_ending.as_str().into()))].into_iter(),
2236 );
2229 );
2237− doc.apply(&transaction, view.id);
2230+ apply_transaction(&transaction, doc, view);
2238− view.apply(&transaction, doc);
2239 }
2231 }
2240
2232
2241 let selection = doc.selection(view.id).clone().transform(|range| {
2233 let selection = doc.selection(view.id).clone().transform(|range| {
@@ -2535,8 +2527,7 @@ async fn make_format_callback(
2535 let doc = doc_mut!(editor, &doc_id);
2527 let doc = doc_mut!(editor, &doc_id);
2536 let view = view_mut!(editor);
2528 let view = view_mut!(editor);
2537 if doc.version() == doc_version {
2529 if doc.version() == doc_version {
2538− doc.apply(&format, view.id);
2530+ apply_transaction(&format, doc, view);
2539− view.apply(&format, doc);
2540 doc.append_changes_to_history(view.id);
2531 doc.append_changes_to_history(view.id);
2541 doc.detect_indent_and_line_ending();
2532 doc.detect_indent_and_line_ending();
2542 view.ensure_cursor_in_view(doc, scrolloff);
2533 view.ensure_cursor_in_view(doc, scrolloff);
@@ -2623,8 +2614,7 @@ fn open(cx: &mut Context, open: Open) {
2623
2614
2624 transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
2615 transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
2625
2616
2626− doc.apply(&transaction, view.id);
2617+ apply_transaction(&transaction, doc, view);
2627− view.apply(&transaction, doc);
2628}
2618}
2629
2619
2630// o inserts a new line after each line with a selection
2620// o inserts a new line after each line with a selection
@@ -2692,8 +2682,7 @@ fn try_restore_indent(doc: &mut Document, view: &mut View) {
2692 let line_start_pos = text.line_to_char(range.cursor_line(text));
2682 let line_start_pos = text.line_to_char(range.cursor_line(text));
2693 (line_start_pos, pos, None)
2683 (line_start_pos, pos, None)
2694 });
2684 });
2695− doc.apply(&transaction, view.id);
2685+ apply_transaction(&transaction, doc, view);
2696− view.apply(&transaction, doc);
2697 }
2686 }
2698}
2687}
2699
2688
@@ -3007,8 +2996,7 @@ pub mod insert {
3007
2996
3008 let (view, doc) = current!(cx.editor);
2997 let (view, doc) = current!(cx.editor);
3009 if let Some(t) = transaction {
2998 if let Some(t) = transaction {
3010− doc.apply(&t, view.id);
2999+ apply_transaction(&t, doc, view);
3011− view.apply(&t, doc);
3012 }
3000 }
3013
3001
3014 // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
3002 // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
@@ -3030,8 +3018,7 @@ pub mod insert {
3030 &doc.selection(view.id).clone().cursors(doc.text().slice(..)),
3018 &doc.selection(view.id).clone().cursors(doc.text().slice(..)),
3031 indent,
3019 indent,
3032 );
3020 );
3033− doc.apply(&transaction, view.id);
3021+ apply_transaction(&transaction, doc, view);
3034− view.apply(&transaction, doc);
3035 }
3022 }
3036
3023
3037 pub fn insert_newline(cx: &mut Context) {
3024 pub fn insert_newline(cx: &mut Context) {
@@ -3118,8 +3105,7 @@ pub mod insert {
3118 transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
3105 transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
3119
3106
3120 let (view, doc) = current!(cx.editor);
3107 let (view, doc) = current!(cx.editor);
3121− doc.apply(&transaction, view.id);
3108+ apply_transaction(&transaction, doc, view);
3122− view.apply(&transaction, doc);
3123 }
3109 }
3124
3110
3125 pub fn delete_char_backward(cx: &mut Context) {
3111 pub fn delete_char_backward(cx: &mut Context) {
@@ -3213,8 +3199,7 @@ pub mod insert {
3213 }
3199 }
3214 });
3200 });
3215 let (view, doc) = current!(cx.editor);
3201 let (view, doc) = current!(cx.editor);
3216− doc.apply(&transaction, view.id);
3202+ apply_transaction(&transaction, doc, view);
3217− view.apply(&transaction, doc);
3218
3203
3219 lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
3204 lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
3220 }
3205 }
@@ -3232,8 +3217,7 @@ pub mod insert {
3232 None,
3217 None,
3233 )
3218 )
3234 });
3219 });
3235− doc.apply(&transaction, view.id);
3220+ apply_transaction(&transaction, doc, view);
3236− view.apply(&transaction, doc);
3237
3221
3238 lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
3222 lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
3239 }
3223 }
@@ -3470,8 +3454,7 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
3470 };
3454 };
3471 (pos, pos, values.next())
3455 (pos, pos, values.next())
3472 });
3456 });
3473− doc.apply(&transaction, view.id);
3457+ apply_transaction(&transaction, doc, view);
3474− view.apply(&transaction, doc);
3475}
3458}
3476
3459
3477pub(crate) fn paste_bracketed_value(cx: &mut Context, contents: String) {
3460pub(crate) fn paste_bracketed_value(cx: &mut Context, contents: String) {
@@ -3563,8 +3546,7 @@ fn replace_with_yanked(cx: &mut Context) {
3563 }
3546 }
3564 });
3547 });
3565
3548
3566− doc.apply(&transaction, view.id);
3549+ apply_transaction(&transaction, doc, view);
3567− view.apply(&transaction, doc);
3568 }
3550 }
3569 }
3551 }
3570}
3552}
@@ -3587,8 +3569,7 @@ fn replace_selections_with_clipboard_impl(
3587 )
3569 )
3588 });
3570 });
3589
3571
3590− doc.apply(&transaction, view.id);
3572+ apply_transaction(&transaction, doc, view);
3591− view.apply(&transaction, doc);
3592 doc.append_changes_to_history(view.id);
3573 doc.append_changes_to_history(view.id);
3593 Ok(())
3574 Ok(())
3594 }
3575 }
@@ -3658,8 +3639,7 @@ fn indent(cx: &mut Context) {
3658 Some((pos, pos, Some(indent.clone())))
3639 Some((pos, pos, Some(indent.clone())))
3659 }),
3640 }),
3660 );
3641 );
3661− doc.apply(&transaction, view.id);
3642+ apply_transaction(&transaction, doc, view);
3662− view.apply(&transaction, doc);
3663}
3643}
3664
3644
3665fn unindent(cx: &mut Context) {
3645fn unindent(cx: &mut Context) {
@@ -3698,8 +3678,7 @@ fn unindent(cx: &mut Context) {
3698
3678
3699 let transaction = Transaction::change(doc.text(), changes.into_iter());
3679 let transaction = Transaction::change(doc.text(), changes.into_iter());
3700
3680
3701− doc.apply(&transaction, view.id);
3681+ apply_transaction(&transaction, doc, view);
3702− view.apply(&transaction, doc);
3703}
3682}
3704
3683
3705fn format_selections(cx: &mut Context) {
3684fn format_selections(cx: &mut Context) {
@@ -3746,8 +3725,7 @@ fn format_selections(cx: &mut Context) {
3746 // language_server.offset_encoding(),
3725 // language_server.offset_encoding(),
3747 // );
3726 // );
3748
3727
3749− // doc.apply(&transaction, view.id);
3728+ // apply_transaction(&transaction, doc, view);
3750− // view.apply(&transaction, doc);
3751 }
3729 }
3752}
3730}
3753
3731
@@ -3802,8 +3780,7 @@ fn join_selections_inner(cx: &mut Context, select_space: bool) {
3802 Transaction::change(doc.text(), changes.into_iter())
3780 Transaction::change(doc.text(), changes.into_iter())
3803 };
3781 };
3804
3782
3805− doc.apply(&transaction, view.id);
3783+ apply_transaction(&transaction, doc, view);
3806− view.apply(&transaction, doc);
3807}
3784}
3808
3785
3809fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
3786fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
@@ -3956,8 +3933,7 @@ fn toggle_comments(cx: &mut Context) {
3956 .map(|tc| tc.as_ref());
3933 .map(|tc| tc.as_ref());
3957 let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token);
3934 let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token);
3958
3935
3959− doc.apply(&transaction, view.id);
3936+ apply_transaction(&transaction, doc, view);
3960− view.apply(&transaction, doc);
3961 exit_select_mode(cx);
3937 exit_select_mode(cx);
3962}
3938}
3963
3939
@@ -4013,8 +3989,7 @@ fn rotate_selection_contents(cx: &mut Context, direction: Direction) {
4013 .map(|(range, fragment)| (range.from(), range.to(), Some(fragment))),
3989 .map(|(range, fragment)| (range.from(), range.to(), Some(fragment))),
4014 );
3990 );
4015
3991
4016− doc.apply(&transaction, view.id);
3992+ apply_transaction(&transaction, doc, view);
4017− view.apply(&transaction, doc);
4018}
3993}
4019
3994
4020fn rotate_selection_contents_forward(cx: &mut Context) {
3995fn rotate_selection_contents_forward(cx: &mut Context) {
@@ -4510,8 +4485,7 @@ fn surround_add(cx: &mut Context) {
4510 }
4485 }
4511
4486
4512 let transaction = Transaction::change(doc.text(), changes.into_iter());
4487 let transaction = Transaction::change(doc.text(), changes.into_iter());
4513− doc.apply(&transaction, view.id);
4488+ apply_transaction(&transaction, doc, view);
4514− view.apply(&transaction, doc);
4515 })
4489 })
4516}
4490}
4517
4491
@@ -4550,8 +4524,7 @@ fn surround_replace(cx: &mut Context) {
4550 (pos, pos + 1, Some(t))
4524 (pos, pos + 1, Some(t))
4551 }),
4525 }),
4552 );
4526 );
4553− doc.apply(&transaction, view.id);
4527+ apply_transaction(&transaction, doc, view);
4554− view.apply(&transaction, doc);
4555 });
4528 });
4556 })
4529 })
4557}
4530}
@@ -4578,8 +4551,7 @@ fn surround_delete(cx: &mut Context) {
4578
4551
4579 let transaction =
4552 let transaction =
4580 Transaction::change(doc.text(), change_pos.into_iter().map(|p| (p, p + 1, None)));
4553 Transaction::change(doc.text(), change_pos.into_iter().map(|p| (p, p + 1, None)));
4581− doc.apply(&transaction, view.id);
4554+ apply_transaction(&transaction, doc, view);
4582− view.apply(&transaction, doc);
4583 })
4555 })
4584}
4556}
4585
4557
@@ -4754,8 +4726,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
4754
4726
4755 if behavior != &ShellBehavior::Ignore {
4727 if behavior != &ShellBehavior::Ignore {
4756 let transaction = Transaction::change(doc.text(), changes.into_iter());
4728 let transaction = Transaction::change(doc.text(), changes.into_iter());
4757− doc.apply(&transaction, view.id);
4729+ apply_transaction(&transaction, doc, view);
4758− view.apply(&transaction, doc);
4759 doc.append_changes_to_history(view.id);
4730 doc.append_changes_to_history(view.id);
4760 }
4731 }
4761
4732
@@ -4818,8 +4789,7 @@ fn add_newline_impl(cx: &mut Context, open: Open) {
4818 });
4789 });
4819
4790
4820 let transaction = Transaction::change(text, changes);
4791 let transaction = Transaction::change(text, changes);
4821− doc.apply(&transaction, view.id);
4792+ apply_transaction(&transaction, doc, view);
4822− view.apply(&transaction, doc);
4823}
4793}
4824
4794
4825/// Increment object under cursor by count.
4795/// Increment object under cursor by count.
@@ -4912,8 +4882,7 @@ fn increment_impl(cx: &mut Context, amount: i64) {
4912 let transaction = Transaction::change(doc.text(), changes);
4882 let transaction = Transaction::change(doc.text(), changes);
4913 let transaction = transaction.with_selection(selection.clone());
4883 let transaction = transaction.with_selection(selection.clone());
4914
4884
4915− doc.apply(&transaction, view.id);
4885+ apply_transaction(&transaction, doc, view);
4916− view.apply(&transaction, doc);
4917 }
4886 }
4918}
4887}
4919
4888
helix-term/src/commands/lsp.rs+2−3
@@ -9,7 +9,7 @@ use tui::text::{Span, Spans};
9use super::{align_view, push_jump, Align, Context, Editor, Open};
9use super::{align_view, push_jump, Align, Context, Editor, Open};
10
10
11use helix_core::{path, Selection};
11use helix_core::{path, Selection};
12−use helix_view::{editor::Action, theme::Style};
12+use helix_view::{apply_transaction, editor::Action, theme::Style};
13
13
14use crate::{
14use crate::{
15 compositor::{self, Compositor},
15 compositor::{self, Compositor},
@@ -617,8 +617,7 @@ pub fn apply_workspace_edit(
617 text_edits,
617 text_edits,
618 offset_encoding,
618 offset_encoding,
619 );
619 );
620− doc.apply(&transaction, view_id);
621− view_mut!(editor, view_id).apply(&transaction, doc);
620+ apply_transaction(&transaction, doc, view_mut!(editor, view_id));
622 doc.append_changes_to_history(view_id);
621 doc.append_changes_to_history(view_id);
623 };
622 };
624
623
helix-term/src/commands/typed.rs+8−9
@@ -2,7 +2,10 @@ use std::ops::Deref;
2
2
3use super::*;
3use super::*;
4
4
5+use helix_view::{
6+ apply_transaction,
5−use helix_view::editor::{Action, CloseError, ConfigEvent};
7+ editor::{Action, CloseError, ConfigEvent},
8+};
6use ui::completers::{self, Completer};
9use ui::completers::{self, Completer};
7
10
8#[derive(Clone)]
11#[derive(Clone)]
@@ -462,8 +465,7 @@ fn set_line_ending(
462 }
465 }
463 }),
466 }),
464 );
467 );
465− doc.apply(&transaction, view.id);
468+ apply_transaction(&transaction, doc, view);
466− view.apply(&transaction, doc);
467 doc.append_changes_to_history(view.id);
469 doc.append_changes_to_history(view.id);
468
470
469 Ok(())
471 Ok(())
@@ -884,8 +886,7 @@ fn replace_selections_with_clipboard_impl(
884 (range.from(), range.to(), Some(contents.as_str().into()))
886 (range.from(), range.to(), Some(contents.as_str().into()))
885 });
887 });
886
888
887− doc.apply(&transaction, view.id);
889+ apply_transaction(&transaction, doc, view);
888− view.apply(&transaction, doc);
889 doc.append_changes_to_history(view.id);
890 doc.append_changes_to_history(view.id);
890 Ok(())
891 Ok(())
891 }
892 }
@@ -1400,8 +1401,7 @@ fn sort_impl(
1400 .map(|(s, fragment)| (s.from(), s.to(), Some(fragment))),
1401 .map(|(s, fragment)| (s.from(), s.to(), Some(fragment))),
1401 );
1402 );
1402
1403
1403− doc.apply(&transaction, view.id);
1404+ apply_transaction(&transaction, doc, view);
1404− view.apply(&transaction, doc);
1405 doc.append_changes_to_history(view.id);
1405 doc.append_changes_to_history(view.id);
1406
1406
1407 Ok(())
1407 Ok(())
@@ -1445,8 +1445,7 @@ fn reflow(
1445 (range.from(), range.to(), Some(reflowed_text))
1445 (range.from(), range.to(), Some(reflowed_text))
1446 });
1446 });
1447
1447
1448− doc.apply(&transaction, view.id);
1448+ apply_transaction(&transaction, doc, view);
1449− view.apply(&transaction, doc);
1450 doc.append_changes_to_history(view.id);
1449 doc.append_changes_to_history(view.id);
1451 view.ensure_cursor_in_view(doc, scrolloff);
1450 view.ensure_cursor_in_view(doc, scrolloff);
1452
1451
helix-term/src/ui/completion.rs+4−7
@@ -1,5 +1,5 @@
1use crate::compositor::{Component, Context, Event, EventResult};
1use crate::compositor::{Component, Context, Event, EventResult};
2−use helix_view::editor::CompleteAction;
2+use helix_view::{apply_transaction, editor::CompleteAction};
3use tui::buffer::Buffer as Surface;
3use tui::buffer::Buffer as Surface;
4use tui::text::Spans;
4use tui::text::Spans;
5
5
@@ -164,8 +164,7 @@ impl Completion {
164
164
165 // initialize a savepoint
165 // initialize a savepoint
166 doc.savepoint();
166 doc.savepoint();
167− doc.apply(&transaction, view.id);
167+ apply_transaction(&transaction, doc, view);
168− view.apply(&transaction, doc);
169
168
170 editor.last_completion = Some(CompleteAction {
169 editor.last_completion = Some(CompleteAction {
171 trigger_offset,
170 trigger_offset,
@@ -184,8 +183,7 @@ impl Completion {
184 trigger_offset,
183 trigger_offset,
185 );
184 );
186
185
187− doc.apply(&transaction, view.id);
186+ apply_transaction(&transaction, doc, view);
188− view.apply(&transaction, doc);
189
187
190 editor.last_completion = Some(CompleteAction {
188 editor.last_completion = Some(CompleteAction {
191 trigger_offset,
189 trigger_offset,
@@ -215,8 +213,7 @@ impl Completion {
215 additional_edits.clone(),
213 additional_edits.clone(),
216 offset_encoding, // TODO: should probably transcode in Client
214 offset_encoding, // TODO: should probably transcode in Client
217 );
215 );
218− doc.apply(&transaction, view.id);
216+ apply_transaction(&transaction, doc, view);
219− view.apply(&transaction, doc);
220 }
217 }
221 }
218 }
222 }
219 }
helix-term/src/ui/editor.rs+2−2
@@ -16,6 +16,7 @@ use helix_core::{
16 visual_coords_at_pos, LineEnding, Position, Range, Selection, Transaction,
16 visual_coords_at_pos, LineEnding, Position, Range, Selection, Transaction,
17};
17};
18use helix_view::{
18use helix_view::{
19+ apply_transaction,
19 document::{Mode, SCRATCH_BUFFER_NAME},
20 document::{Mode, SCRATCH_BUFFER_NAME},
20 editor::{CompleteAction, CursorShapeConfig},
21 editor::{CompleteAction, CursorShapeConfig},
21 graphics::{Color, CursorKind, Modifier, Rect, Style},
22 graphics::{Color, CursorKind, Modifier, Rect, Style},
@@ -1002,8 +1003,7 @@ impl EditorView {
1002 (shift_position(start), shift_position(end), t)
1003 (shift_position(start), shift_position(end), t)
1003 }),
1004 }),
1004 );
1005 );
1005− doc.apply(&tx, view.id);
1006+ apply_transaction(&tx, doc, view);
1006− view.apply(&tx, doc);
1007 }
1007 }
1008 InsertEvent::TriggerCompletion => {
1008 InsertEvent::TriggerCompletion => {
1009 let (_, doc) = current!(cxt.editor);
1009 let (_, doc) = current!(cxt.editor);
helix-view/src/document.rs+6−5
@@ -24,7 +24,7 @@ use helix_core::{
24 DEFAULT_LINE_ENDING,
24 DEFAULT_LINE_ENDING,
25};
25};
26
26
27−use crate::{DocumentId, Editor, View, ViewId};
27+use crate::{apply_transaction, DocumentId, Editor, View, ViewId};
28
28
29/// 8kB of buffer space for encoding and decoding `Rope`s.
29/// 8kB of buffer space for encoding and decoding `Rope`s.
30const BUF_SIZE: usize = 8192;
30const BUF_SIZE: usize = 8192;
@@ -617,8 +617,7 @@ impl Document {
617 // This is not considered a modification of the contents of the file regardless
617 // This is not considered a modification of the contents of the file regardless
618 // of the encoding.
618 // of the encoding.
619 let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
619 let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
620− self.apply(&transaction, view.id);
620+ apply_transaction(&transaction, self, view);
621− view.apply(&transaction, self);
622 self.append_changes_to_history(view.id);
621 self.append_changes_to_history(view.id);
623 self.reset_modified();
622 self.reset_modified();
624
623
@@ -811,6 +810,9 @@ impl Document {
811 }
810 }
812
811
813 /// Apply a [`Transaction`] to the [`Document`] to change its text.
812 /// Apply a [`Transaction`] to the [`Document`] to change its text.
813+ /// Instead of calling this function directly, use [crate::apply_transaction]
814+ /// to ensure that the transaction is applied to the appropriate [`View`] as
815+ /// well.
814 pub fn apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
816 pub fn apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
815 // store the state just before any changes are made. This allows us to undo to the
817 // store the state just before any changes are made. This allows us to undo to the
816 // state just before a transaction was applied.
818 // state just before a transaction was applied.
@@ -865,8 +867,7 @@ impl Document {
865
867
866 pub fn restore(&mut self, view: &mut View) {
868 pub fn restore(&mut self, view: &mut View) {
867 if let Some(revert) = self.savepoint.take() {
869 if let Some(revert) = self.savepoint.take() {
868− self.apply(&revert, view.id);
870+ apply_transaction(&revert, self, view);
869− view.apply(&revert, self);
870 }
871 }
871 }
872 }
872
873
helix-view/src/lib.rs+13
@@ -64,6 +64,19 @@ pub fn align_view(doc: &Document, view: &mut View, align: Align) {
64 view.offset.row = line.saturating_sub(relative);
64 view.offset.row = line.saturating_sub(relative);
65}
65}
66
66
67+/// Applies a [`helix_core::Transaction`] to the given [`Document`]
68+/// and [`View`].
69+pub fn apply_transaction(
70+ transaction: &helix_core::Transaction,
71+ doc: &mut Document,
72+ view: &mut View,
73+) -> bool {
74+ // This is a short function but it's easy to call `Document::apply`
75+ // without calling `View::apply` or in the wrong order. The transaction
76+ // must be applied to the document before the view.
77+ doc.apply(transaction, view.id) && view.apply(transaction, doc)
78+}
79+
67pub use document::Document;
80pub use document::Document;
68pub use editor::Editor;
81pub use editor::Editor;
69pub use theme::Theme;
82pub use theme::Theme;