From 8555248b012ddfc4578c5d82f9c7b31495b03ca0 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Tue, 23 Apr 2024 14:34:43 -0400 Subject: [PATCH] Accept 'IntoIterator' for Picker::new options `Picker::new` loops through the input options to inject each of them, so there's no need to collect into an intermediary Vec. This removes some unnecessary collections. Also, pickers that start with no initial options can now pass an empty slice instead of an empty Vec. Co-authored-by: Luis Useche --- helix-term/src/commands.rs | 37 ++++++++++++++++------------------ helix-term/src/commands/lsp.rs | 2 +- helix-term/src/ui/mod.rs | 26 +++++++++--------------- helix-term/src/ui/picker.rs | 2 +- 4 files changed, 29 insertions(+), 38 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 7e59bbdd6..847695946 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2404,7 +2404,7 @@ fn global_search(cx: &mut Context) { let picker = Picker::new( columns, 1, // contents - vec![], + [], config, move |cx, FileResult { path, line_num, .. }, action| { let doc = match cx.editor.open(path, action) { @@ -2991,16 +2991,12 @@ fn jumplist_picker(cx: &mut Context) { let picker = Picker::new( columns, 1, // path - cx.editor - .tree - .views() - .flat_map(|(view, _)| { - view.jumps - .iter() - .rev() - .map(|(doc_id, selection)| new_meta(view, *doc_id, selection.clone())) - }) - .collect(), + cx.editor.tree.views().flat_map(|(view, _)| { + view.jumps + .iter() + .rev() + .map(|(doc_id, selection)| new_meta(view, *doc_id, selection.clone())) + }), (), |cx, meta, action| { cx.editor.switch(meta.id, action); @@ -3077,7 +3073,7 @@ fn changed_file_picker(cx: &mut Context) { let picker = Picker::new( columns, 1, // path - Vec::new(), + [], FileChangeData { cwd: cwd.clone(), style_untracked: added, @@ -3124,14 +3120,15 @@ pub fn command_palette(cx: &mut Context) { [&cx.editor.mode] .reverse_map(); - let mut commands: Vec = MappableCommand::STATIC_COMMAND_LIST.into(); - commands.extend(typed::TYPABLE_COMMAND_LIST.iter().map(|cmd| { - MappableCommand::Typable { - name: cmd.name.to_owned(), - doc: cmd.doc.to_owned(), - args: Vec::new(), - } - })); + let commands = MappableCommand::STATIC_COMMAND_LIST.iter().cloned().chain( + typed::TYPABLE_COMMAND_LIST + .iter() + .map(|cmd| MappableCommand::Typable { + name: cmd.name.to_owned(), + args: Vec::new(), + doc: cmd.doc.to_owned(), + }), + ); let columns = vec![ ui::PickerColumn::new("name", |item, _| match item { diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 059fb814d..601c58ebe 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -501,7 +501,7 @@ pub fn workspace_symbol_picker(cx: &mut Context) { let picker = Picker::new( columns, 1, // name column - vec![], + [], (), move |cx, item, action| { jump_to_location( diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 14d92b575..e1ecf0a66 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -228,22 +228,16 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi .into() }, )]; - let picker = Picker::new( - columns, - 0, - Vec::new(), - root, - move |cx, path: &PathBuf, action| { - if let Err(e) = cx.editor.open(path, action) { - let err = if let Some(err) = e.source() { - format!("{}", err) - } else { - format!("unable to open \"{}\"", path.display()) - }; - cx.editor.set_error(err); - } - }, - ) + let picker = Picker::new(columns, 0, [], root, move |cx, path: &PathBuf, action| { + if let Err(e) = cx.editor.open(path, action) { + let err = if let Some(err) = e.source() { + format!("{}", err) + } else { + format!("unable to open \"{}\"", path.display()) + }; + cx.editor.set_error(err); + } + }) .with_preview(|_editor, path| Some((path.as_path().into(), None))); let injector = picker.injector(); let timeout = std::time::Instant::now() + std::time::Duration::from_millis(30); diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 4f5095300..ebc06cc02 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -298,7 +298,7 @@ impl Picker { pub fn new( columns: Vec>, primary_column: usize, - options: Vec, + options: impl IntoIterator, editor_data: D, callback_fn: impl Fn(&mut Context, &T, Action) + 'static, ) -> Self {