Skip to content

Commit c388e16

Browse files
the-mikedavisarchseer
authored andcommitted
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.
1 parent 0aedef0 commit c388e16

File tree

7 files changed

+65
-87
lines changed

7 files changed

+65
-87
lines changed

helix-term/src/commands.rs

Lines changed: 30 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use helix_core::{
2727
SmallVec, Tendril, Transaction,
2828
};
2929
use 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

866866
fn 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

13151313
fn 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]
21332130
fn 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

21442137
fn 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

34773460
pub(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

36653645
fn 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

37053684
fn 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

38093786
fn 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

40203995
fn 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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tui::text::{Span, Spans};
99
use super::{align_view, push_jump, Align, Context, Editor, Open};
1010

1111
use helix_core::{path, Selection};
12-
use helix_view::{editor::Action, theme::Style};
12+
use helix_view::{apply_transaction, editor::Action, theme::Style};
1313

1414
use 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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use std::ops::Deref;
22

33
use super::*;
44

5-
use helix_view::editor::{Action, CloseError, ConfigEvent};
5+
use helix_view::{
6+
apply_transaction,
7+
editor::{Action, CloseError, ConfigEvent},
8+
};
69
use 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

0 commit comments

Comments
 (0)