Skip to content

Commit

Permalink
Apply transactions to Views
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
the-mikedavis authored and archseer committed Oct 11, 2022
1 parent d418f07 commit 0aedef0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 20 deletions.
43 changes: 36 additions & 7 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1290,6 +1291,7 @@ fn replace(cx: &mut Context) {
});

doc.apply(&transaction, view.id);
view.apply(&transaction, doc);
}
})
}
Expand All @@ -1307,6 +1309,7 @@ where
});

doc.apply(&transaction, view.id);
view.apply(&transaction, doc);
}

fn switch_case(cx: &mut Context) {
Expand Down Expand Up @@ -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 => {
Expand All @@ -2126,14 +2130,15 @@ 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
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
(range.from(), range.to(), None)
});
doc.apply(&transaction, view_id);
view.apply(&transaction, doc);
}

fn delete_selection(cx: &mut Context) {
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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;

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -3021,6 +3031,7 @@ pub mod insert {
indent,
);
doc.apply(&transaction, view.id);
view.apply(&transaction, doc);
}

pub fn insert_newline(cx: &mut Context) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -3220,6 +3233,7 @@ pub mod insert {
)
});
doc.apply(&transaction, view.id);
view.apply(&transaction, doc);

lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -3549,6 +3564,7 @@ fn replace_with_yanked(cx: &mut Context) {
});

doc.apply(&transaction, view.id);
view.apply(&transaction, doc);
}
}
}
Expand All @@ -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(())
}
Expand Down Expand Up @@ -3642,6 +3659,7 @@ fn indent(cx: &mut Context) {
}),
);
doc.apply(&transaction, view.id);
view.apply(&transaction, doc);
}

fn unindent(cx: &mut Context) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -3728,6 +3747,7 @@ fn format_selections(cx: &mut Context) {
// );

// doc.apply(&transaction, view.id);
// view.apply(&transaction, doc);
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
})
}

Expand Down Expand Up @@ -4527,6 +4551,7 @@ fn surround_replace(cx: &mut Context) {
}),
);
doc.apply(&transaction, view.id);
view.apply(&transaction, doc);
});
})
}
Expand Down Expand Up @@ -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);
})
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}

Expand Down
5 changes: 2 additions & 3 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,7 @@ pub fn apply_workspace_edit(
}
};

let doc = editor
.document_mut(doc_id)
.expect("Document for document_changes not found");
let doc = doc_mut!(editor, &doc_id);

// Need to determine a view for apply/append_changes_to_history
let selections = doc.selections();
Expand All @@ -620,6 +618,7 @@ pub fn apply_workspace_edit(
offset_encoding,
);
doc.apply(&transaction, view_id);
view_mut!(editor, view_id).apply(&transaction, doc);
doc.append_changes_to_history(view_id);
};

Expand Down
6 changes: 5 additions & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ fn set_line_ending(
}),
);
doc.apply(&transaction, view.id);
view.apply(&transaction, doc);
doc.append_changes_to_history(view.id);

Ok(())
Expand Down Expand Up @@ -884,6 +885,7 @@ fn replace_selections_with_clipboard_impl(
});

doc.apply(&transaction, view.id);
view.apply(&transaction, doc);
doc.append_changes_to_history(view.id);
Ok(())
}
Expand Down Expand Up @@ -1004,7 +1006,7 @@ fn reload(

let scrolloff = cx.editor.config().scrolloff;
let (view, doc) = current!(cx.editor);
doc.reload(view.id).map(|_| {
doc.reload(view).map(|_| {
view.ensure_cursor_in_view(doc, scrolloff);
})
}
Expand Down Expand Up @@ -1399,6 +1401,7 @@ fn sort_impl(
);

doc.apply(&transaction, view.id);
view.apply(&transaction, doc);
doc.append_changes_to_history(view.id);

Ok(())
Expand Down Expand Up @@ -1443,6 +1446,7 @@ fn reflow(
});

doc.apply(&transaction, view.id);
view.apply(&transaction, doc);
doc.append_changes_to_history(view.id);
view.ensure_cursor_in_view(doc, scrolloff);

Expand Down
7 changes: 5 additions & 2 deletions helix-term/src/ui/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ impl Completion {
let (view, doc) = current!(editor);

// if more text was entered, remove it
doc.restore(view.id);
doc.restore(view);

match event {
PromptEvent::Abort => {
doc.restore(view.id);
doc.restore(view);
editor.last_completion = None;
}
PromptEvent::Update => {
Expand All @@ -165,6 +165,7 @@ impl Completion {
// initialize a savepoint
doc.savepoint();
doc.apply(&transaction, view.id);
view.apply(&transaction, doc);

editor.last_completion = Some(CompleteAction {
trigger_offset,
Expand All @@ -184,6 +185,7 @@ impl Completion {
);

doc.apply(&transaction, view.id);
view.apply(&transaction, doc);

editor.last_completion = Some(CompleteAction {
trigger_offset,
Expand Down Expand Up @@ -214,6 +216,7 @@ impl Completion {
offset_encoding, // TODO: should probably transcode in Client
);
doc.apply(&transaction, view.id);
view.apply(&transaction, doc);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ impl EditorView {
InsertEvent::CompletionApply(compl) => {
let (view, doc) = current!(cxt.editor);

doc.restore(view.id);
doc.restore(view);

let text = doc.text().slice(..);
let cursor = doc.selection(view.id).primary().cursor(text);
Expand All @@ -1003,6 +1003,7 @@ impl EditorView {
}),
);
doc.apply(&tx, view.id);
view.apply(&tx, doc);
}
InsertEvent::TriggerCompletion => {
let (_, doc) = current!(cxt.editor);
Expand Down
Loading

0 comments on commit 0aedef0

Please sign in to comment.