Skip to content

Apply transactions to jumplist selections #4186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 36 additions & 38 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use helix_core::{
SmallVec, Tendril, Transaction,
};
use helix_view::{
apply_transaction,
clipboard::ClipboardType,
document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
editor::{Action, Motion},
Expand Down Expand Up @@ -859,7 +860,7 @@ fn align_selections(cx: &mut Context) {
changes.sort_unstable_by_key(|(from, _, _)| *from);

let transaction = Transaction::change(doc.text(), changes.into_iter());
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
}

fn goto_window(cx: &mut Context, align: Align) {
Expand Down Expand Up @@ -1289,7 +1290,7 @@ fn replace(cx: &mut Context) {
}
});

doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
}
})
}
Expand All @@ -1306,7 +1307,7 @@ where
(range.from(), range.to(), Some(text))
});

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

fn switch_case(cx: &mut Context) {
Expand Down Expand Up @@ -2112,7 +2113,7 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
(range.from(), range.to(), None)
});
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);

match op {
Operation::Delete => {
Expand All @@ -2126,14 +2127,11 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
}

#[inline]
fn delete_selection_insert_mode(doc: &mut Document, view: &View, selection: &Selection) {
let view_id = view.id;

// then delete
fn delete_selection_insert_mode(doc: &mut Document, view: &mut View, selection: &Selection) {
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
(range.from(), range.to(), None)
});
doc.apply(&transaction, view_id);
apply_transaction(&transaction, doc, view);
}

fn delete_selection(cx: &mut Context) {
Expand Down Expand Up @@ -2229,7 +2227,7 @@ fn append_mode(cx: &mut Context) {
doc.text(),
[(end, end, Some(doc.line_ending.as_str().into()))].into_iter(),
);
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
}

let selection = doc.selection(view.id).clone().transform(|range| {
Expand Down Expand Up @@ -2529,7 +2527,7 @@ async fn make_format_callback(
let doc = doc_mut!(editor, &doc_id);
let view = view_mut!(editor);
if doc.version() == doc_version {
doc.apply(&format, view.id);
apply_transaction(&format, doc, view);
doc.append_changes_to_history(view.id);
doc.detect_indent_and_line_ending();
view.ensure_cursor_in_view(doc, scrolloff);
Expand Down Expand Up @@ -2616,7 +2614,7 @@ fn open(cx: &mut Context, open: Open) {

transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));

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

// o inserts a new line after each line with a selection
Expand All @@ -2637,7 +2635,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 +2652,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 +2671,18 @@ 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);
apply_transaction(&transaction, doc, view);
}
}

Expand Down Expand Up @@ -2998,7 +2996,7 @@ pub mod insert {

let (view, doc) = current!(cx.editor);
if let Some(t) = transaction {
doc.apply(&t, view.id);
apply_transaction(&t, doc, view);
}

// TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
Expand All @@ -3020,7 +3018,7 @@ pub mod insert {
&doc.selection(view.id).clone().cursors(doc.text().slice(..)),
indent,
);
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
}

pub fn insert_newline(cx: &mut Context) {
Expand Down Expand Up @@ -3107,7 +3105,7 @@ pub mod insert {
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));

let (view, doc) = current!(cx.editor);
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
}

pub fn delete_char_backward(cx: &mut Context) {
Expand Down Expand Up @@ -3201,7 +3199,7 @@ pub mod insert {
}
});
let (view, doc) = current!(cx.editor);
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);

lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
}
Expand All @@ -3219,7 +3217,7 @@ pub mod insert {
None,
)
});
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);

lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
}
Expand Down Expand Up @@ -3413,7 +3411,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 @@ -3456,7 +3454,7 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &View, action: Paste,
};
(pos, pos, values.next())
});
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
}

pub(crate) fn paste_bracketed_value(cx: &mut Context, contents: String) {
Expand Down Expand Up @@ -3548,7 +3546,7 @@ fn replace_with_yanked(cx: &mut Context) {
}
});

doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
}
}
}
Expand All @@ -3571,7 +3569,7 @@ fn replace_selections_with_clipboard_impl(
)
});

doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
doc.append_changes_to_history(view.id);
Ok(())
}
Expand Down Expand Up @@ -3641,7 +3639,7 @@ fn indent(cx: &mut Context) {
Some((pos, pos, Some(indent.clone())))
}),
);
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
}

fn unindent(cx: &mut Context) {
Expand Down Expand Up @@ -3680,7 +3678,7 @@ fn unindent(cx: &mut Context) {

let transaction = Transaction::change(doc.text(), changes.into_iter());

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

fn format_selections(cx: &mut Context) {
Expand Down Expand Up @@ -3727,7 +3725,7 @@ fn format_selections(cx: &mut Context) {
// language_server.offset_encoding(),
// );

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

Expand Down Expand Up @@ -3782,7 +3780,7 @@ fn join_selections_inner(cx: &mut Context, select_space: bool) {
Transaction::change(doc.text(), changes.into_iter())
};

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

fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
Expand Down Expand Up @@ -3935,7 +3933,7 @@ fn toggle_comments(cx: &mut Context) {
.map(|tc| tc.as_ref());
let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token);

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

Expand Down Expand Up @@ -3991,7 +3989,7 @@ fn rotate_selection_contents(cx: &mut Context, direction: Direction) {
.map(|(range, fragment)| (range.from(), range.to(), Some(fragment))),
);

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

fn rotate_selection_contents_forward(cx: &mut Context) {
Expand Down Expand Up @@ -4487,7 +4485,7 @@ fn surround_add(cx: &mut Context) {
}

let transaction = Transaction::change(doc.text(), changes.into_iter());
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
})
}

Expand Down Expand Up @@ -4526,7 +4524,7 @@ fn surround_replace(cx: &mut Context) {
(pos, pos + 1, Some(t))
}),
);
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
});
})
}
Expand All @@ -4553,7 +4551,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);
apply_transaction(&transaction, doc, view);
})
}

Expand Down Expand Up @@ -4728,7 +4726,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);
apply_transaction(&transaction, doc, view);
doc.append_changes_to_history(view.id);
}

Expand Down Expand Up @@ -4791,7 +4789,7 @@ fn add_newline_impl(cx: &mut Context, open: Open) {
});

let transaction = Transaction::change(text, changes);
doc.apply(&transaction, view.id);
apply_transaction(&transaction, doc, view);
}

/// Increment object under cursor by count.
Expand Down Expand Up @@ -4884,7 +4882,7 @@ fn increment_impl(cx: &mut Context, amount: i64) {
let transaction = Transaction::change(doc.text(), changes);
let transaction = transaction.with_selection(selection.clone());

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

Expand Down
8 changes: 3 additions & 5 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tui::text::{Span, Spans};
use super::{align_view, push_jump, Align, Context, Editor, Open};

use helix_core::{path, Selection};
use helix_view::{editor::Action, theme::Style};
use helix_view::{apply_transaction, editor::Action, theme::Style};

use crate::{
compositor::{self, Compositor},
Expand Down 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 @@ -619,7 +617,7 @@ pub fn apply_workspace_edit(
text_edits,
offset_encoding,
);
doc.apply(&transaction, view_id);
apply_transaction(&transaction, doc, view_mut!(editor, view_id));
doc.append_changes_to_history(view_id);
};

Expand Down
Loading