From 0aedef03334dfc0cb2a723cce23abc5b4ca55a22 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Mon, 10 Oct 2022 14:56:26 -0500 Subject: Apply transactions to Views This change adds View::apply calls for all Document::apply call-sites, ensuring that changes to a document do not leave invalid entries in the View's jumplist. --- helix-term/src/commands.rs | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'helix-term/src/commands.rs') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 2db5bfcf..e2bd9402 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -860,6 +860,7 @@ fn align_selections(cx: &mut Context) { let transaction = Transaction::change(doc.text(), changes.into_iter()); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } fn goto_window(cx: &mut Context, align: Align) { @@ -1290,6 +1291,7 @@ fn replace(cx: &mut Context) { }); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } }) } @@ -1307,6 +1309,7 @@ where }); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } fn switch_case(cx: &mut Context) { @@ -2113,6 +2116,7 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) { (range.from(), range.to(), None) }); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); match op { Operation::Delete => { @@ -2126,7 +2130,7 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) { } #[inline] -fn delete_selection_insert_mode(doc: &mut Document, view: &View, selection: &Selection) { +fn delete_selection_insert_mode(doc: &mut Document, view: &mut View, selection: &Selection) { let view_id = view.id; // then delete @@ -2134,6 +2138,7 @@ fn delete_selection_insert_mode(doc: &mut Document, view: &View, selection: &Sel (range.from(), range.to(), None) }); doc.apply(&transaction, view_id); + view.apply(&transaction, doc); } fn delete_selection(cx: &mut Context) { @@ -2230,6 +2235,7 @@ fn append_mode(cx: &mut Context) { [(end, end, Some(doc.line_ending.as_str().into()))].into_iter(), ); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } let selection = doc.selection(view.id).clone().transform(|range| { @@ -2530,6 +2536,7 @@ async fn make_format_callback( let view = view_mut!(editor); if doc.version() == doc_version { doc.apply(&format, view.id); + view.apply(&format, doc); doc.append_changes_to_history(view.id); doc.detect_indent_and_line_ending(); view.ensure_cursor_in_view(doc, scrolloff); @@ -2617,6 +2624,7 @@ fn open(cx: &mut Context, open: Open) { transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index())); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } // o inserts a new line after each line with a selection @@ -2637,7 +2645,7 @@ fn normal_mode(cx: &mut Context) { cx.editor.mode = Mode::Normal; let (view, doc) = current!(cx.editor); - try_restore_indent(doc, view.id); + try_restore_indent(doc, view); // if leaving append mode, move cursor back by 1 if doc.restore_cursor { @@ -2654,7 +2662,7 @@ fn normal_mode(cx: &mut Context) { } } -fn try_restore_indent(doc: &mut Document, view_id: ViewId) { +fn try_restore_indent(doc: &mut Document, view: &mut View) { use helix_core::chars::char_is_whitespace; use helix_core::Operation; @@ -2673,18 +2681,19 @@ fn try_restore_indent(doc: &mut Document, view_id: ViewId) { let doc_changes = doc.changes().changes(); let text = doc.text().slice(..); - let range = doc.selection(view_id).primary(); + let range = doc.selection(view.id).primary(); let pos = range.cursor(text); let line_end_pos = line_end_char_index(&text, range.cursor_line(text)); if inserted_a_new_blank_line(doc_changes, pos, line_end_pos) { // Removes tailing whitespaces. let transaction = - Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| { + Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { let line_start_pos = text.line_to_char(range.cursor_line(text)); (line_start_pos, pos, None) }); - doc.apply(&transaction, view_id); + doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } } @@ -2999,6 +3008,7 @@ pub mod insert { let (view, doc) = current!(cx.editor); if let Some(t) = transaction { doc.apply(&t, view.id); + view.apply(&t, doc); } // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc) @@ -3021,6 +3031,7 @@ pub mod insert { indent, ); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } pub fn insert_newline(cx: &mut Context) { @@ -3108,6 +3119,7 @@ pub mod insert { let (view, doc) = current!(cx.editor); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } pub fn delete_char_backward(cx: &mut Context) { @@ -3202,6 +3214,7 @@ pub mod insert { }); let (view, doc) = current!(cx.editor); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic); } @@ -3220,6 +3233,7 @@ pub mod insert { ) }); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic); } @@ -3413,7 +3427,7 @@ enum Paste { Cursor, } -fn paste_impl(values: &[String], doc: &mut Document, view: &View, action: Paste, count: usize) { +fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Paste, count: usize) { let repeat = std::iter::repeat( values .last() @@ -3457,6 +3471,7 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &View, action: Paste, (pos, pos, values.next()) }); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } pub(crate) fn paste_bracketed_value(cx: &mut Context, contents: String) { @@ -3549,6 +3564,7 @@ fn replace_with_yanked(cx: &mut Context) { }); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } } } @@ -3572,6 +3588,7 @@ fn replace_selections_with_clipboard_impl( }); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); doc.append_changes_to_history(view.id); Ok(()) } @@ -3642,6 +3659,7 @@ fn indent(cx: &mut Context) { }), ); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } fn unindent(cx: &mut Context) { @@ -3681,6 +3699,7 @@ fn unindent(cx: &mut Context) { let transaction = Transaction::change(doc.text(), changes.into_iter()); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } fn format_selections(cx: &mut Context) { @@ -3728,6 +3747,7 @@ fn format_selections(cx: &mut Context) { // ); // doc.apply(&transaction, view.id); + // view.apply(&transaction, doc); } } @@ -3783,6 +3803,7 @@ fn join_selections_inner(cx: &mut Context, select_space: bool) { }; doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) { @@ -3936,6 +3957,7 @@ fn toggle_comments(cx: &mut Context) { let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); exit_select_mode(cx); } @@ -3992,6 +4014,7 @@ fn rotate_selection_contents(cx: &mut Context, direction: Direction) { ); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } fn rotate_selection_contents_forward(cx: &mut Context) { @@ -4488,6 +4511,7 @@ fn surround_add(cx: &mut Context) { let transaction = Transaction::change(doc.text(), changes.into_iter()); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); }) } @@ -4527,6 +4551,7 @@ fn surround_replace(cx: &mut Context) { }), ); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); }); }) } @@ -4554,6 +4579,7 @@ fn surround_delete(cx: &mut Context) { let transaction = Transaction::change(doc.text(), change_pos.into_iter().map(|p| (p, p + 1, None))); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); }) } @@ -4729,6 +4755,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) { if behavior != &ShellBehavior::Ignore { let transaction = Transaction::change(doc.text(), changes.into_iter()); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); doc.append_changes_to_history(view.id); } @@ -4792,6 +4819,7 @@ fn add_newline_impl(cx: &mut Context, open: Open) { let transaction = Transaction::change(text, changes); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } /// Increment object under cursor by count. @@ -4885,6 +4913,7 @@ fn increment_impl(cx: &mut Context, amount: i64) { let transaction = transaction.with_selection(selection.clone()); doc.apply(&transaction, view.id); + view.apply(&transaction, doc); } } -- cgit v1.2.3-70-g09d2