on master help sign in
Add a helper function for applying transactions

It is easy to forget to call `Document::apply` and/or `View::apply` in the correct order. This commit introduces a helper function which closes over both calls.

commitc388e16e09b36c665c4f92db4f7a071e7c1d9761 authorMichael Davis <mcarsondavis@gmail.com> dateMon 10 Oct 2022, 15:15 (4 years ago) parent0aedef0
7 files changed+65−87
helix-term/src/commands.rs+30−61 helix-term/src/commands/lsp.rs+2−3 helix-term/src/commands/typed.rs+8−9 helix-term/src/ui/completion.rs+4−7 helix-term/src/ui/editor.rs+2−2 helix-view/src/document.rs+6−5 helix-view/src/lib.rs+13
helix-term/src/commands.rs+30−61
@@ -27,6 +27,7 @@ use helix_core::{
2727 SmallVec, Tendril, Transaction,
2828};
2929use helix_view::{
30+ apply_transaction,
3031 clipboard::ClipboardType,
3132 document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
3233 editor::{Action, Motion},
@@ -859,8 +860,7 @@ fn align_selections(cx: &mut Context) {
859860 changes.sort_unstable_by_key(|(from, _, _)| *from);
860861
861862 let transaction = Transaction::change(doc.text(), changes.into_iter());
862− doc.apply(&transaction, view.id);
863− view.apply(&transaction, doc);
863+ apply_transaction(&transaction, doc, view);
864864}
865865
866866fn goto_window(cx: &mut Context, align: Align) {
@@ -1290,8 +1290,7 @@ fn replace(cx: &mut Context) {
12901290 }
12911291 });
12921292
1293− doc.apply(&transaction, view.id);
1294− view.apply(&transaction, doc);
1293+ apply_transaction(&transaction, doc, view);
12951294 }
12961295 })
12971296}
@@ -1308,8 +1307,7 @@ where
13081307 (range.from(), range.to(), Some(text))
13091308 });
13101309
1311− doc.apply(&transaction, view.id);
1312− view.apply(&transaction, doc);
1310+ apply_transaction(&transaction, doc, view);
13131311}
13141312
13151313fn switch_case(cx: &mut Context) {
@@ -2115,8 +2113,7 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
21152113 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
21162114 (range.from(), range.to(), None)
21172115 });
2118− doc.apply(&transaction, view.id);
2119− view.apply(&transaction, doc);
2116+ apply_transaction(&transaction, doc, view);
21202117
21212118 match op {
21222119 Operation::Delete => {
@@ -2131,14 +2128,10 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
21312128
21322129#[inline]
21332130fn delete_selection_insert_mode(doc: &mut Document, view: &mut View, selection: &Selection) {
2134− let view_id = view.id;
2135−
2136− // then delete
21372131 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
21382132 (range.from(), range.to(), None)
21392133 });
2140− doc.apply(&transaction, view_id);
2141− view.apply(&transaction, doc);
2134+ apply_transaction(&transaction, doc, view);
21422135}
21432136
21442137fn delete_selection(cx: &mut Context) {
@@ -2234,8 +2227,7 @@ fn append_mode(cx: &mut Context) {
22342227 doc.text(),
22352228 [(end, end, Some(doc.line_ending.as_str().into()))].into_iter(),
22362229 );
2237− doc.apply(&transaction, view.id);
2238− view.apply(&transaction, doc);
2230+ apply_transaction(&transaction, doc, view);
22392231 }
22402232
22412233 let selection = doc.selection(view.id).clone().transform(|range| {
@@ -2535,8 +2527,7 @@ async fn make_format_callback(
25352527 let doc = doc_mut!(editor, &doc_id);
25362528 let view = view_mut!(editor);
25372529 if doc.version() == doc_version {
2538− doc.apply(&format, view.id);
2539− view.apply(&format, doc);
2530+ apply_transaction(&format, doc, view);
25402531 doc.append_changes_to_history(view.id);
25412532 doc.detect_indent_and_line_ending();
25422533 view.ensure_cursor_in_view(doc, scrolloff);
@@ -2623,8 +2614,7 @@ fn open(cx: &mut Context, open: Open) {
26232614
26242615 transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
26252616
2626− doc.apply(&transaction, view.id);
2627− view.apply(&transaction, doc);
2617+ apply_transaction(&transaction, doc, view);
26282618}
26292619
26302620// 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) {
26922682 let line_start_pos = text.line_to_char(range.cursor_line(text));
26932683 (line_start_pos, pos, None)
26942684 });
2695− doc.apply(&transaction, view.id);
2696− view.apply(&transaction, doc);
2685+ apply_transaction(&transaction, doc, view);
26972686 }
26982687}
26992688
@@ -3007,8 +2996,7 @@ pub mod insert {
30072996
30082997 let (view, doc) = current!(cx.editor);
30092998 if let Some(t) = transaction {
3010− doc.apply(&t, view.id);
3011− view.apply(&t, doc);
2999+ apply_transaction(&t, doc, view);
30123000 }
30133001
30143002 // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
@@ -3030,8 +3018,7 @@ pub mod insert {
30303018 &doc.selection(view.id).clone().cursors(doc.text().slice(..)),
30313019 indent,
30323020 );
3033− doc.apply(&transaction, view.id);
3034− view.apply(&transaction, doc);
3021+ apply_transaction(&transaction, doc, view);
30353022 }
30363023
30373024 pub fn insert_newline(cx: &mut Context) {
@@ -3118,8 +3105,7 @@ pub mod insert {
31183105 transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
31193106
31203107 let (view, doc) = current!(cx.editor);
3121− doc.apply(&transaction, view.id);
3122− view.apply(&transaction, doc);
3108+ apply_transaction(&transaction, doc, view);
31233109 }
31243110
31253111 pub fn delete_char_backward(cx: &mut Context) {
@@ -3213,8 +3199,7 @@ pub mod insert {
32133199 }
32143200 });
32153201 let (view, doc) = current!(cx.editor);
3216− doc.apply(&transaction, view.id);
3217− view.apply(&transaction, doc);
3202+ apply_transaction(&transaction, doc, view);
32183203
32193204 lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
32203205 }
@@ -3232,8 +3217,7 @@ pub mod insert {
32323217 None,
32333218 )
32343219 });
3235− doc.apply(&transaction, view.id);
3236− view.apply(&transaction, doc);
3220+ apply_transaction(&transaction, doc, view);
32373221
32383222 lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
32393223 }
@@ -3470,8 +3454,7 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
34703454 };
34713455 (pos, pos, values.next())
34723456 });
3473− doc.apply(&transaction, view.id);
3474− view.apply(&transaction, doc);
3457+ apply_transaction(&transaction, doc, view);
34753458}
34763459
34773460pub(crate) fn paste_bracketed_value(cx: &mut Context, contents: String) {
@@ -3563,8 +3546,7 @@ fn replace_with_yanked(cx: &mut Context) {
35633546 }
35643547 });
35653548
3566− doc.apply(&transaction, view.id);
3567− view.apply(&transaction, doc);
3549+ apply_transaction(&transaction, doc, view);
35683550 }
35693551 }
35703552}
@@ -3587,8 +3569,7 @@ fn replace_selections_with_clipboard_impl(
35873569 )
35883570 });
35893571
3590− doc.apply(&transaction, view.id);
3591− view.apply(&transaction, doc);
3572+ apply_transaction(&transaction, doc, view);
35923573 doc.append_changes_to_history(view.id);
35933574 Ok(())
35943575 }
@@ -3658,8 +3639,7 @@ fn indent(cx: &mut Context) {
36583639 Some((pos, pos, Some(indent.clone())))
36593640 }),
36603641 );
3661− doc.apply(&transaction, view.id);
3662− view.apply(&transaction, doc);
3642+ apply_transaction(&transaction, doc, view);
36633643}
36643644
36653645fn unindent(cx: &mut Context) {
@@ -3698,8 +3678,7 @@ fn unindent(cx: &mut Context) {
36983678
36993679 let transaction = Transaction::change(doc.text(), changes.into_iter());
37003680
3701− doc.apply(&transaction, view.id);
3702− view.apply(&transaction, doc);
3681+ apply_transaction(&transaction, doc, view);
37033682}
37043683
37053684fn format_selections(cx: &mut Context) {
@@ -3746,8 +3725,7 @@ fn format_selections(cx: &mut Context) {
37463725 // language_server.offset_encoding(),
37473726 // );
37483727
3749− // doc.apply(&transaction, view.id);
3750− // view.apply(&transaction, doc);
3728+ // apply_transaction(&transaction, doc, view);
37513729 }
37523730}
37533731
@@ -3802,8 +3780,7 @@ fn join_selections_inner(cx: &mut Context, select_space: bool) {
38023780 Transaction::change(doc.text(), changes.into_iter())
38033781 };
38043782
3805− doc.apply(&transaction, view.id);
3806− view.apply(&transaction, doc);
3783+ apply_transaction(&transaction, doc, view);
38073784}
38083785
38093786fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
@@ -3956,8 +3933,7 @@ fn toggle_comments(cx: &mut Context) {
39563933 .map(|tc| tc.as_ref());
39573934 let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token);
39583935
3959− doc.apply(&transaction, view.id);
3960− view.apply(&transaction, doc);
3936+ apply_transaction(&transaction, doc, view);
39613937 exit_select_mode(cx);
39623938}
39633939
@@ -4013,8 +3989,7 @@ fn rotate_selection_contents(cx: &mut Context, direction: Direction) {
40133989 .map(|(range, fragment)| (range.from(), range.to(), Some(fragment))),
40143990 );
40153991
4016− doc.apply(&transaction, view.id);
4017− view.apply(&transaction, doc);
3992+ apply_transaction(&transaction, doc, view);
40183993}
40193994
40203995fn rotate_selection_contents_forward(cx: &mut Context) {
@@ -4510,8 +4485,7 @@ fn surround_add(cx: &mut Context) {
45104485 }
45114486
45124487 let transaction = Transaction::change(doc.text(), changes.into_iter());
4513− doc.apply(&transaction, view.id);
4514− view.apply(&transaction, doc);
4488+ apply_transaction(&transaction, doc, view);
45154489 })
45164490}
45174491
@@ -4550,8 +4524,7 @@ fn surround_replace(cx: &mut Context) {
45504524 (pos, pos + 1, Some(t))
45514525 }),
45524526 );
4553− doc.apply(&transaction, view.id);
4554− view.apply(&transaction, doc);
4527+ apply_transaction(&transaction, doc, view);
45554528 });
45564529 })
45574530}
@@ -4578,8 +4551,7 @@ fn surround_delete(cx: &mut Context) {
45784551
45794552 let transaction =
45804553 Transaction::change(doc.text(), change_pos.into_iter().map(|p| (p, p + 1, None)));
4581− doc.apply(&transaction, view.id);
4582− view.apply(&transaction, doc);
4554+ apply_transaction(&transaction, doc, view);
45834555 })
45844556}
45854557
@@ -4754,8 +4726,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
47544726
47554727 if behavior != &ShellBehavior::Ignore {
47564728 let transaction = Transaction::change(doc.text(), changes.into_iter());
4757− doc.apply(&transaction, view.id);
4758− view.apply(&transaction, doc);
4729+ apply_transaction(&transaction, doc, view);
47594730 doc.append_changes_to_history(view.id);
47604731 }
47614732
@@ -4818,8 +4789,7 @@ fn add_newline_impl(cx: &mut Context, open: Open) {
48184789 });
48194790
48204791 let transaction = Transaction::change(text, changes);
4821− doc.apply(&transaction, view.id);
4822− view.apply(&transaction, doc);
4792+ apply_transaction(&transaction, doc, view);
48234793}
48244794
48254795/// Increment object under cursor by count.
@@ -4912,8 +4882,7 @@ fn increment_impl(cx: &mut Context, amount: i64) {
49124882 let transaction = Transaction::change(doc.text(), changes);
49134883 let transaction = transaction.with_selection(selection.clone());
49144884
4915− doc.apply(&transaction, view.id);
4916− view.apply(&transaction, doc);
4885+ apply_transaction(&transaction, doc, view);
49174886 }
49184887}
49194888
helix-term/src/commands/lsp.rs+2−3
@@ -9,7 +9,7 @@ use tui::text::{Span, Spans};
99use super::{align_view, push_jump, Align, Context, Editor, Open};
1010
1111use helix_core::{path, Selection};
12−use helix_view::{editor::Action, theme::Style};
12+use helix_view::{apply_transaction, editor::Action, theme::Style};
1313
1414use crate::{
1515 compositor::{self, Compositor},
@@ -617,8 +617,7 @@ pub fn apply_workspace_edit(
617617 text_edits,
618618 offset_encoding,
619619 );
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));
622621 doc.append_changes_to_history(view_id);
623622 };
624623
helix-term/src/commands/typed.rs+8−9
@@ -2,7 +2,10 @@ use std::ops::Deref;
22
33use super::*;
44
5−use helix_view::editor::{Action, CloseError, ConfigEvent};
5+use helix_view::{
6+ apply_transaction,
7+ editor::{Action, CloseError, ConfigEvent},
8+};
69use ui::completers::{self, Completer};
710
811#[derive(Clone)]
@@ -462,8 +465,7 @@ fn set_line_ending(
462465 }
463466 }),
464467 );
465− doc.apply(&transaction, view.id);
466− view.apply(&transaction, doc);
468+ apply_transaction(&transaction, doc, view);
467469 doc.append_changes_to_history(view.id);
468470
469471 Ok(())
@@ -884,8 +886,7 @@ fn replace_selections_with_clipboard_impl(
884886 (range.from(), range.to(), Some(contents.as_str().into()))
885887 });
886888
887− doc.apply(&transaction, view.id);
888− view.apply(&transaction, doc);
889+ apply_transaction(&transaction, doc, view);
889890 doc.append_changes_to_history(view.id);
890891 Ok(())
891892 }
@@ -1400,8 +1401,7 @@ fn sort_impl(
14001401 .map(|(s, fragment)| (s.from(), s.to(), Some(fragment))),
14011402 );
14021403
1403− doc.apply(&transaction, view.id);
1404− view.apply(&transaction, doc);
1404+ apply_transaction(&transaction, doc, view);
14051405 doc.append_changes_to_history(view.id);
14061406
14071407 Ok(())
@@ -1445,8 +1445,7 @@ fn reflow(
14451445 (range.from(), range.to(), Some(reflowed_text))
14461446 });
14471447
1448− doc.apply(&transaction, view.id);
1449− view.apply(&transaction, doc);
1448+ apply_transaction(&transaction, doc, view);
14501449 doc.append_changes_to_history(view.id);
14511450 view.ensure_cursor_in_view(doc, scrolloff);
14521451
helix-term/src/ui/completion.rs+4−7
@@ -1,5 +1,5 @@
11use crate::compositor::{Component, Context, Event, EventResult};
2−use helix_view::editor::CompleteAction;
2+use helix_view::{apply_transaction, editor::CompleteAction};
33use tui::buffer::Buffer as Surface;
44use tui::text::Spans;
55
@@ -164,8 +164,7 @@ impl Completion {
164164
165165 // initialize a savepoint
166166 doc.savepoint();
167− doc.apply(&transaction, view.id);
168− view.apply(&transaction, doc);
167+ apply_transaction(&transaction, doc, view);
169168
170169 editor.last_completion = Some(CompleteAction {
171170 trigger_offset,
@@ -184,8 +183,7 @@ impl Completion {
184183 trigger_offset,
185184 );
186185
187− doc.apply(&transaction, view.id);
188− view.apply(&transaction, doc);
186+ apply_transaction(&transaction, doc, view);
189187
190188 editor.last_completion = Some(CompleteAction {
191189 trigger_offset,
@@ -215,8 +213,7 @@ impl Completion {
215213 additional_edits.clone(),
216214 offset_encoding, // TODO: should probably transcode in Client
217215 );
218− doc.apply(&transaction, view.id);
219− view.apply(&transaction, doc);
216+ apply_transaction(&transaction, doc, view);
220217 }
221218 }
222219 }
helix-term/src/ui/editor.rs+2−2
@@ -16,6 +16,7 @@ use helix_core::{
1616 visual_coords_at_pos, LineEnding, Position, Range, Selection, Transaction,
1717};
1818use helix_view::{
19+ apply_transaction,
1920 document::{Mode, SCRATCH_BUFFER_NAME},
2021 editor::{CompleteAction, CursorShapeConfig},
2122 graphics::{Color, CursorKind, Modifier, Rect, Style},
@@ -1002,8 +1003,7 @@ impl EditorView {
10021003 (shift_position(start), shift_position(end), t)
10031004 }),
10041005 );
1005− doc.apply(&tx, view.id);
1006− view.apply(&tx, doc);
1006+ apply_transaction(&tx, doc, view);
10071007 }
10081008 InsertEvent::TriggerCompletion => {
10091009 let (_, doc) = current!(cxt.editor);
helix-view/src/document.rs+6−5
@@ -24,7 +24,7 @@ use helix_core::{
2424 DEFAULT_LINE_ENDING,
2525};
2626
27−use crate::{DocumentId, Editor, View, ViewId};
27+use crate::{apply_transaction, DocumentId, Editor, View, ViewId};
2828
2929/// 8kB of buffer space for encoding and decoding `Rope`s.
3030const BUF_SIZE: usize = 8192;
@@ -617,8 +617,7 @@ impl Document {
617617 // This is not considered a modification of the contents of the file regardless
618618 // of the encoding.
619619 let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
620− self.apply(&transaction, view.id);
621− view.apply(&transaction, self);
620+ apply_transaction(&transaction, self, view);
622621 self.append_changes_to_history(view.id);
623622 self.reset_modified();
624623
@@ -811,6 +810,9 @@ impl Document {
811810 }
812811
813812 /// 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.
814816 pub fn apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
815817 // store the state just before any changes are made. This allows us to undo to the
816818 // state just before a transaction was applied.
@@ -865,8 +867,7 @@ impl Document {
865867
866868 pub fn restore(&mut self, view: &mut View) {
867869 if let Some(revert) = self.savepoint.take() {
868− self.apply(&revert, view.id);
869− view.apply(&revert, self);
870+ apply_transaction(&revert, self, view);
870871 }
871872 }
872873
helix-view/src/lib.rs+13
@@ -64,6 +64,19 @@ pub fn align_view(doc: &Document, view: &mut View, align: Align) {
6464 view.offset.row = line.saturating_sub(relative);
6565}
6666
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+
6780pub use document::Document;
6881pub use editor::Editor;
6982pub use theme::Theme;