Skip to content

Commit

Permalink
remove default_item from new
Browse files Browse the repository at this point in the history
  • Loading branch information
mangas committed Nov 27, 2022
1 parent 984951b commit 90bc020
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 39 deletions.
27 changes: 11 additions & 16 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2500,22 +2500,17 @@ pub fn command_palette(cx: &mut Context) {
}
}));

let picker = Picker::new(
commands,
keymap,
move |cx, command, _action| {
let mut ctx = Context {
register: None,
count: std::num::NonZeroUsize::new(1),
editor: cx.editor,
callback: None,
on_next_key_callback: None,
jobs: cx.jobs,
};
command.execute(&mut ctx);
},
None,
);
let picker = Picker::new(commands, keymap, move |cx, command, _action| {
let mut ctx = Context {
register: None,
count: std::num::NonZeroUsize::new(1),
editor: cx.editor,
callback: None,
on_next_key_callback: None,
jobs: cx.jobs,
};
command.execute(&mut ctx);
});
compositor.push(Box::new(overlayed(picker)));
},
));
Expand Down
1 change: 0 additions & 1 deletion helix-term/src/commands/dap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ pub fn dap_launch(cx: &mut Context) {
});
cx.jobs.callback(callback);
},
None,
))));
}

Expand Down
11 changes: 3 additions & 8 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,14 +1138,9 @@ fn lsp_workspace_command(
let callback = async move {
let call: job::Callback = Callback::EditorCompositor(Box::new(
move |_editor: &mut Editor, compositor: &mut Compositor| {
let picker = ui::Picker::new(
commands,
(),
|cx, command, _action| {
execute_lsp_command(cx.editor, command.clone());
},
None,
);
let picker = ui::Picker::new(commands, (), |cx, command, _action| {
execute_lsp_command(cx.editor, command.clone());
});
compositor.push(Box::new(overlayed(picker)))
},
));
Expand Down
22 changes: 8 additions & 14 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ impl<T: Item> FilePicker<T> {
initial_cursor: Option<T>,
) -> Self {
let truncate_start = true;
let mut picker = Picker::new(options, editor_data, callback_fn, initial_cursor);
let mut picker =
Picker::new(options, editor_data, callback_fn).with_default_item(initial_cursor);
picker.truncate_start = truncate_start;

Self {
Expand Down Expand Up @@ -392,7 +393,6 @@ impl<T: Item> Picker<T> {
options: Vec<T>,
editor_data: T::Data,
callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
default_item: Option<T>,
) -> Self {
let prompt = Prompt::new(
"".into(),
Expand All @@ -413,7 +413,7 @@ impl<T: Item> Picker<T> {
show_preview: true,
callback_fn: Box::new(callback_fn),
completion_height: 0,
default_item,
default_item: None,
};
picker.cursor = picker.find_default_item_index().unwrap_or(0);

Expand All @@ -433,6 +433,11 @@ impl<T: Item> Picker<T> {
picker
}

pub fn with_default_item(mut self, default_item: Option<T>) -> Self {
self.default_item = default_item;
self
}

pub fn score(&mut self) {
let now = Instant::now();

Expand Down Expand Up @@ -555,17 +560,6 @@ impl<T: Item> Picker<T> {
self.cursor = self.matches.len().saturating_sub(1);
}

pub fn set_cursor(&mut self, cursor: usize) {
let len = self.matches.len();

if len == 0 {
// No results, can't move.
return;
}

self.cursor = cursor % len;
}

pub fn selection(&self) -> Option<&T> {
self.matches
.get(self.cursor)
Expand Down

0 comments on commit 90bc020

Please sign in to comment.