From 7cbdff4df863f26f01b062ef083fd49aca83091a Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Fri, 26 Apr 2024 15:15:15 -0400 Subject: [PATCH] Remove unnecessary Vec collections for Registers::write callsites These callsites either used `vec![..]` instead of a static slice `[..]` or, worse, collected into a Vec unnecessarily. The refactor in the parent commit to `IntoIterator` for `Registers::write` makes these Vecs unnecessary as the values are appended to each register via `VecDeque::extend`. We could take this change further by returning the number of values we yanked in `Registers::write`. However this would only remove the need to collect from one callsite, so this further refactor is deferred until it would more useful in the future. --- helix-term/src/commands.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index a8f6543f8..be694bff4 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2644,7 +2644,7 @@ fn delete_selection_impl(cx: &mut Context, op: Operation, yank: YankAction) { if cx.register != Some('_') && matches!(yank, YankAction::Yank) { // yank the selection let text = doc.text().slice(..); - let values: Vec = selection.fragments(text).map(Cow::into_owned).collect(); + let values = selection.fragments(text).map(Cow::into_owned); 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()); @@ -4137,7 +4137,7 @@ fn yank_joined_impl(editor: &mut Editor, separator: &str, register: char) { acc }); - match editor.registers.write(register, vec![joined]) { + match editor.registers.write(register, [joined]) { Ok(_) => editor.set_status(format!( "joined and yanked {selections} selection{} to register {register}", if selections == 1 { "" } else { "s" } @@ -4170,7 +4170,7 @@ fn yank_primary_selection_impl(editor: &mut Editor, register: char) { let selection = doc.selection(view.id).primary().fragment(text).to_string(); - match editor.registers.write(register, vec![selection]) { + match editor.registers.write(register, [selection]) { Ok(_) => editor.set_status(format!("yanked primary selection to register {register}",)), Err(err) => editor.set_error(err.to_string()), } @@ -5932,18 +5932,15 @@ fn record_macro(cx: &mut Context) { if let Some((reg, mut keys)) = cx.editor.macro_recording.take() { // Remove the keypress which ends the recording keys.pop(); - let s = keys - .into_iter() - .map(|key| { - let s = key.to_string(); - if s.chars().count() == 1 { - s - } else { - format!("<{}>", s) - } - }) - .collect::(); - match cx.editor.registers.write(reg, vec![s]) { + let s = keys.into_iter().map(|key| { + let s = key.to_string(); + if s.chars().count() == 1 { + s + } else { + format!("<{}>", s) + } + }); + match cx.editor.registers.write(reg, s) { Ok(_) => cx .editor .set_status(format!("Recorded to register [{}]", reg)),