Skip to content
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

Use an enum to represent yank decision during deletion of a selection #10148

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Changes from 1 commit
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
40 changes: 25 additions & 15 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2617,24 +2617,34 @@ fn selection_is_linewise(selection: &Selection, text: &Rope) -> bool {
})
}

fn delete_selection_impl(cx: &mut Context, op: Operation, yank: bool) {
enum YankMode {
voiceroy marked this conversation as resolved.
Show resolved Hide resolved
Yank,
NoYank,
}

fn delete_selection_impl(cx: &mut Context, op: Operation, yank: YankMode) {
let (view, doc) = current!(cx.editor);

let selection = doc.selection(view.id);
let only_whole_lines = selection_is_linewise(selection, doc.text());

if cx.register != Some('_') && yank {
voiceroy marked this conversation as resolved.
Show resolved Hide resolved
// first yank the selection
let text = doc.text().slice(..);
let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect();
let reg_name = cx.register.unwrap_or('"');
if let Err(err) = cx.editor.registers.write(reg_name, values) {
cx.editor.set_error(err.to_string());
return;
match yank {
YankMode::Yank => {
if cx.register != Some('_') {
// yank the selection
let text = doc.text().slice(..);
let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect();
let reg_name = cx.register.unwrap_or('"');
if let Err(err) = cx.editor.registers.write(reg_name, values) {
cx.editor.set_error(err.to_string());
return;
}
}
}
};
YankMode::NoYank => {}
}

// then delete
// delete
let transaction =
Transaction::delete_by_selection(doc.text(), selection, |range| (range.from(), range.to()));
doc.apply(&transaction, view.id);
Expand Down Expand Up @@ -2700,19 +2710,19 @@ fn delete_by_selection_insert_mode(
}

fn delete_selection(cx: &mut Context) {
delete_selection_impl(cx, Operation::Delete, true);
delete_selection_impl(cx, Operation::Delete, YankMode::Yank);
}

fn delete_selection_noyank(cx: &mut Context) {
delete_selection_impl(cx, Operation::Delete, false);
delete_selection_impl(cx, Operation::Delete, YankMode::NoYank);
}

fn change_selection(cx: &mut Context) {
delete_selection_impl(cx, Operation::Change, true);
delete_selection_impl(cx, Operation::Change, YankMode::Yank);
}

fn change_selection_noyank(cx: &mut Context) {
delete_selection_impl(cx, Operation::Change, false);
delete_selection_impl(cx, Operation::Change, YankMode::NoYank);
}

fn collapse_selection(cx: &mut Context) {
Expand Down
Loading