on master help sign in
Remove apply_transaction helper (#5598)
commita20a96abdcfbea3b3eb890bfc266850938434a90 authorIvan Tham <pickfire@riseup.net> dateSun 22 Jan 2023, 02:13 (4 years ago) parent68fc109
10 files changed+46−67
helix-term/src/commands.rs+28−29 helix-term/src/commands/lsp.rs+2−2 helix-term/src/commands/typed.rs+5−8 helix-term/src/ui/completion.rs+4−4 helix-term/src/ui/editor.rs+1−2 helix-term/tests/test/helpers.rs+2−2 helix-view/src/document.rs+3−6 helix-view/src/editor.rs+1−1 helix-view/src/lib.rs−11 helix-view/src/view.rs−2
helix-term/src/commands.rs+28−29
@@ -26,7 +26,6 @@ use helix_core::{
2626 SmallVec, Tendril, Transaction,
2727};
2828use helix_view::{
29− apply_transaction,
3029 clipboard::ClipboardType,
3130 document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
3231 editor::{Action, Motion},
@@ -864,7 +863,7 @@ fn align_selections(cx: &mut Context) {
864863 changes.sort_unstable_by_key(|(from, _, _)| *from);
865864
866865 let transaction = Transaction::change(doc.text(), changes.into_iter());
867− apply_transaction(&transaction, doc, view);
866+ doc.apply(&transaction, view.id);
868867}
869868
870869fn goto_window(cx: &mut Context, align: Align) {
@@ -1316,7 +1315,7 @@ fn replace(cx: &mut Context) {
13161315 }
13171316 });
13181317
1319− apply_transaction(&transaction, doc, view);
1318+ doc.apply(&transaction, view.id);
13201319 exit_select_mode(cx);
13211320 }
13221321 })
@@ -1334,7 +1333,7 @@ where
13341333 (range.from(), range.to(), Some(text))
13351334 });
13361335
1337− apply_transaction(&transaction, doc, view);
1336+ doc.apply(&transaction, view.id);
13381337}
13391338
13401339fn switch_case(cx: &mut Context) {
@@ -2159,7 +2158,7 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
21592158 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
21602159 (range.from(), range.to(), None)
21612160 });
2162− apply_transaction(&transaction, doc, view);
2161+ doc.apply(&transaction, view.id);
21632162
21642163 match op {
21652164 Operation::Delete => {
@@ -2177,7 +2176,7 @@ fn delete_selection_insert_mode(doc: &mut Document, view: &mut View, selection:
21772176 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
21782177 (range.from(), range.to(), None)
21792178 });
2180− apply_transaction(&transaction, doc, view);
2179+ doc.apply(&transaction, view.id);
21812180}
21822181
21832182fn delete_selection(cx: &mut Context) {
@@ -2273,7 +2272,7 @@ fn append_mode(cx: &mut Context) {
22732272 doc.text(),
22742273 [(end, end, Some(doc.line_ending.as_str().into()))].into_iter(),
22752274 );
2276− apply_transaction(&transaction, doc, view);
2275+ doc.apply(&transaction, view.id);
22772276 }
22782277
22792278 let selection = doc.selection(view.id).clone().transform(|range| {
@@ -2583,7 +2582,7 @@ async fn make_format_callback(
25832582
25842583 if let Ok(format) = format {
25852584 if doc.version() == doc_version {
2586− apply_transaction(&format, doc, view);
2585+ doc.apply(&format, view.id);
25872586 doc.append_changes_to_history(view);
25882587 doc.detect_indent_and_line_ending();
25892588 view.ensure_cursor_in_view(doc, scrolloff);
@@ -2676,7 +2675,7 @@ fn open(cx: &mut Context, open: Open) {
26762675
26772676 transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
26782677
2679− apply_transaction(&transaction, doc, view);
2678+ doc.apply(&transaction, view.id);
26802679}
26812680
26822681// o inserts a new line after each line with a selection
@@ -3104,7 +3103,7 @@ pub mod insert {
31043103
31053104 let (view, doc) = current!(cx.editor);
31063105 if let Some(t) = transaction {
3107− apply_transaction(&t, doc, view);
3106+ doc.apply(&t, view.id);
31083107 }
31093108
31103109 // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
@@ -3126,7 +3125,7 @@ pub mod insert {
31263125 &doc.selection(view.id).clone().cursors(doc.text().slice(..)),
31273126 indent,
31283127 );
3129− apply_transaction(&transaction, doc, view);
3128+ doc.apply(&transaction, view.id);
31303129 }
31313130
31323131 pub fn insert_newline(cx: &mut Context) {
@@ -3231,7 +3230,7 @@ pub mod insert {
32313230 transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
32323231
32333232 let (view, doc) = current!(cx.editor);
3234− apply_transaction(&transaction, doc, view);
3233+ doc.apply(&transaction, view.id);
32353234 }
32363235
32373236 pub fn delete_char_backward(cx: &mut Context) {
@@ -3326,7 +3325,7 @@ pub mod insert {
33263325 }
33273326 });
33283327 let (view, doc) = current!(cx.editor);
3329− apply_transaction(&transaction, doc, view);
3328+ doc.apply(&transaction, view.id);
33303329
33313330 lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
33323331 }
@@ -3344,7 +3343,7 @@ pub mod insert {
33443343 None,
33453344 )
33463345 });
3347− apply_transaction(&transaction, doc, view);
3346+ doc.apply(&transaction, view.id);
33483347
33493348 lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
33503349 }
@@ -3625,7 +3624,7 @@ fn paste_impl(
36253624 transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
36263625 }
36273626
3628− apply_transaction(&transaction, doc, view);
3627+ doc.apply(&transaction, view.id);
36293628}
36303629
36313630pub(crate) fn paste_bracketed_value(cx: &mut Context, contents: String) {
@@ -3717,7 +3716,7 @@ fn replace_with_yanked(cx: &mut Context) {
37173716 }
37183717 });
37193718
3720− apply_transaction(&transaction, doc, view);
3719+ doc.apply(&transaction, view.id);
37213720 exit_select_mode(cx);
37223721 }
37233722 }
@@ -3741,7 +3740,7 @@ fn replace_selections_with_clipboard_impl(
37413740 )
37423741 });
37433742
3744− apply_transaction(&transaction, doc, view);
3743+ doc.apply(&transaction, view.id);
37453744 doc.append_changes_to_history(view);
37463745 }
37473746 Err(e) => return Err(e.context("Couldn't get system clipboard contents")),
@@ -3813,7 +3812,7 @@ fn indent(cx: &mut Context) {
38133812 Some((pos, pos, Some(indent.clone())))
38143813 }),
38153814 );
3816− apply_transaction(&transaction, doc, view);
3815+ doc.apply(&transaction, view.id);
38173816}
38183817
38193818fn unindent(cx: &mut Context) {
@@ -3852,7 +3851,7 @@ fn unindent(cx: &mut Context) {
38523851
38533852 let transaction = Transaction::change(doc.text(), changes.into_iter());
38543853
3855− apply_transaction(&transaction, doc, view);
3854+ doc.apply(&transaction, view.id);
38563855}
38573856
38583857fn format_selections(cx: &mut Context) {
@@ -3907,7 +3906,7 @@ fn format_selections(cx: &mut Context) {
39073906 language_server.offset_encoding(),
39083907 );
39093908
3910− apply_transaction(&transaction, doc, view);
3909+ doc.apply(&transaction, view.id);
39113910}
39123911
39133912fn join_selections_impl(cx: &mut Context, select_space: bool) {
@@ -3966,7 +3965,7 @@ fn join_selections_impl(cx: &mut Context, select_space: bool) {
39663965 Transaction::change(doc.text(), changes.into_iter())
39673966 };
39683967
3969− apply_transaction(&transaction, doc, view);
3968+ doc.apply(&transaction, view.id);
39703969}
39713970
39723971fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
@@ -4109,7 +4108,7 @@ fn toggle_comments(cx: &mut Context) {
41094108 .map(|tc| tc.as_ref());
41104109 let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token);
41114110
4112− apply_transaction(&transaction, doc, view);
4111+ doc.apply(&transaction, view.id);
41134112 exit_select_mode(cx);
41144113}
41154114
@@ -4165,7 +4164,7 @@ fn rotate_selection_contents(cx: &mut Context, direction: Direction) {
41654164 .map(|(range, fragment)| (range.from(), range.to(), Some(fragment))),
41664165 );
41674166
4168− apply_transaction(&transaction, doc, view);
4167+ doc.apply(&transaction, view.id);
41694168}
41704169
41714170fn rotate_selection_contents_forward(cx: &mut Context) {
@@ -4698,7 +4697,7 @@ fn surround_add(cx: &mut Context) {
46984697
46994698 let transaction = Transaction::change(doc.text(), changes.into_iter())
47004699 .with_selection(Selection::new(ranges, selection.primary_index()));
4701− apply_transaction(&transaction, doc, view);
4700+ doc.apply(&transaction, view.id);
47024701 exit_select_mode(cx);
47034702 })
47044703}
@@ -4738,7 +4737,7 @@ fn surround_replace(cx: &mut Context) {
47384737 (pos, pos + 1, Some(t))
47394738 }),
47404739 );
4741− apply_transaction(&transaction, doc, view);
4740+ doc.apply(&transaction, view.id);
47424741 exit_select_mode(cx);
47434742 });
47444743 })
@@ -4766,7 +4765,7 @@ fn surround_delete(cx: &mut Context) {
47664765
47674766 let transaction =
47684767 Transaction::change(doc.text(), change_pos.into_iter().map(|p| (p, p + 1, None)));
4769− apply_transaction(&transaction, doc, view);
4768+ doc.apply(&transaction, view.id);
47704769 exit_select_mode(cx);
47714770 })
47724771}
@@ -4981,7 +4980,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
49814980 if behavior != &ShellBehavior::Ignore {
49824981 let transaction = Transaction::change(doc.text(), changes.into_iter())
49834982 .with_selection(Selection::new(ranges, selection.primary_index()));
4984− apply_transaction(&transaction, doc, view);
4983+ doc.apply(&transaction, view.id);
49854984 doc.append_changes_to_history(view);
49864985 }
49874986
@@ -5044,7 +5043,7 @@ fn add_newline_impl(cx: &mut Context, open: Open) {
50445043 });
50455044
50465045 let transaction = Transaction::change(text, changes);
5047− apply_transaction(&transaction, doc, view);
5046+ doc.apply(&transaction, view.id);
50485047}
50495048
50505049enum IncrementDirection {
@@ -5111,7 +5110,7 @@ fn increment_impl(cx: &mut Context, increment_direction: IncrementDirection) {
51115110 let new_selection = Selection::new(new_selection_ranges, selection.primary_index());
51125111 let transaction = Transaction::change(doc.text(), changes.into_iter());
51135112 let transaction = transaction.with_selection(new_selection);
5114− apply_transaction(&transaction, doc, view);
5113+ doc.apply(&transaction, view.id);
51155114 }
51165115}
51175116
helix-term/src/commands/lsp.rs+2−2
@@ -13,7 +13,7 @@ use tui::{
1313use super::{align_view, push_jump, Align, Context, Editor, Open};
1414
1515use helix_core::{path, Selection};
16−use helix_view::{apply_transaction, document::Mode, editor::Action, theme::Style};
16+use helix_view::{document::Mode, editor::Action, theme::Style};
1717
1818use crate::{
1919 compositor::{self, Compositor},
@@ -800,7 +800,7 @@ pub fn apply_workspace_edit(
800800 offset_encoding,
801801 );
802802 let view = view_mut!(editor, view_id);
803− apply_transaction(&transaction, doc, view);
803+ doc.apply(&transaction, view.id);
804804 doc.append_changes_to_history(view);
805805 };
806806
helix-term/src/commands/typed.rs+5−8
@@ -4,10 +4,7 @@ use crate::job::Job;
44
55use super::*;
66
7−use helix_view::{
8− apply_transaction,
9− editor::{Action, CloseError, ConfigEvent},
10−};
7+use helix_view::editor::{Action, CloseError, ConfigEvent};
118use ui::completers::{self, Completer};
129
1310#[derive(Clone)]
@@ -480,7 +477,7 @@ fn set_line_ending(
480477 }
481478 }),
482479 );
483− apply_transaction(&transaction, doc, view);
480+ doc.apply(&transaction, view.id);
484481 doc.append_changes_to_history(view);
485482
486483 Ok(())
@@ -925,7 +922,7 @@ fn replace_selections_with_clipboard_impl(
925922 (range.from(), range.to(), Some(contents.as_str().into()))
926923 });
927924
928− apply_transaction(&transaction, doc, view);
925+ doc.apply(&transaction, view.id);
929926 doc.append_changes_to_history(view);
930927 Ok(())
931928 }
@@ -1596,7 +1593,7 @@ fn sort_impl(
15961593 .map(|(s, fragment)| (s.from(), s.to(), Some(fragment))),
15971594 );
15981595
1599− apply_transaction(&transaction, doc, view);
1596+ doc.apply(&transaction, view.id);
16001597 doc.append_changes_to_history(view);
16011598
16021599 Ok(())
@@ -1640,7 +1637,7 @@ fn reflow(
16401637 (range.from(), range.to(), Some(reflowed_text))
16411638 });
16421639
1643− apply_transaction(&transaction, doc, view);
1640+ doc.apply(&transaction, view.id);
16441641 doc.append_changes_to_history(view);
16451642 view.ensure_cursor_in_view(doc, scrolloff);
16461643
helix-term/src/ui/completion.rs+4−4
@@ -1,5 +1,5 @@
11use crate::compositor::{Component, Context, Event, EventResult};
2−use helix_view::{apply_transaction, editor::CompleteAction, ViewId};
2+use helix_view::{editor::CompleteAction, ViewId};
33use tui::buffer::Buffer as Surface;
44
55use std::borrow::Cow;
@@ -183,7 +183,7 @@ impl Completion {
183183
184184 // initialize a savepoint
185185 doc.savepoint();
186− apply_transaction(&transaction, doc, view);
186+ doc.apply(&transaction, view.id);
187187
188188 editor.last_completion = Some(CompleteAction {
189189 trigger_offset,
@@ -203,7 +203,7 @@ impl Completion {
203203 trigger_offset,
204204 );
205205
206− apply_transaction(&transaction, doc, view);
206+ doc.apply(&transaction, view.id);
207207
208208 editor.last_completion = Some(CompleteAction {
209209 trigger_offset,
@@ -233,7 +233,7 @@ impl Completion {
233233 additional_edits.clone(),
234234 offset_encoding, // TODO: should probably transcode in Client
235235 );
236− apply_transaction(&transaction, doc, view);
236+ doc.apply(&transaction, view.id);
237237 }
238238 }
239239 }
helix-term/src/ui/editor.rs+1−2
@@ -17,7 +17,6 @@ use helix_core::{
1717 visual_coords_at_pos, LineEnding, Position, Range, Selection, Transaction,
1818};
1919use helix_view::{
20− apply_transaction,
2120 document::{Mode, SCRATCH_BUFFER_NAME},
2221 editor::{CompleteAction, CursorShapeConfig},
2322 graphics::{Color, CursorKind, Modifier, Rect, Style},
@@ -1048,7 +1047,7 @@ impl EditorView {
10481047 (shift_position(start), shift_position(end), t)
10491048 }),
10501049 );
1051− apply_transaction(&tx, doc, view);
1050+ doc.apply(&tx, view.id);
10521051 }
10531052 InsertEvent::TriggerCompletion => {
10541053 let (_, doc) = current!(cxt.editor);
helix-term/tests/test/helpers.rs+2−2
@@ -130,7 +130,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
130130 })
131131 .with_selection(test_case.in_selection.clone());
132132
133− helix_view::apply_transaction(&transaction, doc, view);
133+ doc.apply(&transaction, view.id);
134134
135135 test_key_sequence(
136136 &mut app,
@@ -315,7 +315,7 @@ impl AppBuilder {
315315 .with_selection(selection);
316316
317317 // replace the initial text with the input text
318− helix_view::apply_transaction(&trans, doc, view);
318+ doc.apply(&trans, view.id);
319319 }
320320
321321 Ok(app)
helix-view/src/document.rs+3−6
@@ -27,7 +27,7 @@ use helix_core::{
2727};
2828
2929use crate::editor::RedrawHandle;
30−use crate::{apply_transaction, DocumentId, Editor, View, ViewId};
30+use crate::{DocumentId, Editor, View, ViewId};
3131
3232/// 8kB of buffer space for encoding and decoding `Rope`s.
3333const BUF_SIZE: usize = 8192;
@@ -650,7 +650,7 @@ impl Document {
650650 // This is not considered a modification of the contents of the file regardless
651651 // of the encoding.
652652 let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
653− apply_transaction(&transaction, self, view);
653+ self.apply(&transaction, view.id);
654654 self.append_changes_to_history(view);
655655 self.reset_modified();
656656
@@ -852,9 +852,6 @@ impl Document {
852852 }
853853
854854 /// Apply a [`Transaction`] to the [`Document`] to change its text.
855− /// Instead of calling this function directly, use [crate::apply_transaction]
856− /// to ensure that the transaction is applied to the appropriate [`View`] as
857− /// well.
858855 pub fn apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
859856 // store the state just before any changes are made. This allows us to undo to the
860857 // state just before a transaction was applied.
@@ -911,7 +908,7 @@ impl Document {
911908
912909 pub fn restore(&mut self, view: &mut View) {
913910 if let Some(revert) = self.savepoint.take() {
914− apply_transaction(&revert, self, view);
911+ self.apply(&revert, view.id);
915912 }
916913 }
917914
helix-view/src/editor.rs+1−1
@@ -1502,6 +1502,6 @@ fn try_restore_indent(doc: &mut Document, view: &mut View) {
15021502 let line_start_pos = text.line_to_char(range.cursor_line(text));
15031503 (line_start_pos, pos, None)
15041504 });
1505− crate::apply_transaction(&transaction, doc, view);
1505+ doc.apply(&transaction, view.id);
15061506 }
15071507}
helix-view/src/lib.rs−11
@@ -66,17 +66,6 @@ pub fn align_view(doc: &Document, view: &mut View, align: Align) {
6666 view.offset.row = line.saturating_sub(relative);
6767}
6868
69−/// Applies a [`helix_core::Transaction`] to the given [`Document`]
70−/// and [`View`].
71−pub fn apply_transaction(
72− transaction: &helix_core::Transaction,
73− doc: &mut Document,
74− view: &View,
75−) -> bool {
76− // TODO remove this helper function. Just call Document::apply everywhere directly.
77− doc.apply(transaction, view.id)
78−}
79−
8069pub use document::Document;
8170pub use editor::Editor;
8271pub use theme::Theme;
helix-view/src/view.rs−2
@@ -380,8 +380,6 @@ impl View {
380380 // }
381381
382382 /// Applies a [`Transaction`] to the view.
383− /// Instead of calling this function directly, use [crate::apply_transaction]
384− /// which applies a transaction to the [`Document`] and view together.
385383 pub fn apply(&mut self, transaction: &Transaction, doc: &mut Document) {
386384 self.jumps.apply(transaction, doc);
387385 self.doc_revisions