mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-22 09:26:19 +04:00
Copy struct fields and new() from Picker to FilePicker
This commit is contained in:
parent
40916dff63
commit
104036bd7f
@ -78,6 +78,26 @@ fn from(v: DocumentId) -> Self {
|
||||
pub type FileLocation = (PathOrId, Option<(usize, usize)>);
|
||||
|
||||
pub struct FilePicker<T: Item> {
|
||||
options: Vec<T>,
|
||||
editor_data: T::Data,
|
||||
// filter: String,
|
||||
matcher: Box<Matcher>,
|
||||
matches: Vec<PickerMatch>,
|
||||
|
||||
/// Current height of the completions box
|
||||
completion_height: u16,
|
||||
|
||||
cursor: usize,
|
||||
// pattern: String,
|
||||
prompt: Prompt,
|
||||
previous_pattern: (String, FuzzyQuery),
|
||||
/// Whether to show the preview panel (default true)
|
||||
show_preview: bool,
|
||||
/// Constraints for tabular formatting
|
||||
widths: Vec<Constraint>,
|
||||
|
||||
callback_fn: PickerCallback<T>,
|
||||
|
||||
picker: Picker<T>,
|
||||
pub truncate_start: bool,
|
||||
/// Caches paths to documents
|
||||
@ -131,17 +151,49 @@ pub fn new(
|
||||
callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
|
||||
preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
|
||||
) -> Self {
|
||||
let truncate_start = true;
|
||||
let mut picker = Picker::new(options, editor_data, callback_fn);
|
||||
picker.truncate_start = truncate_start;
|
||||
let prompt = Prompt::new(
|
||||
"".into(),
|
||||
None,
|
||||
ui::completers::none,
|
||||
|_editor: &mut Context, _pattern: &str, _event: PromptEvent| {},
|
||||
);
|
||||
|
||||
Self {
|
||||
picker,
|
||||
truncate_start,
|
||||
let mut picker = Self {
|
||||
options,
|
||||
editor_data,
|
||||
matcher: Box::default(),
|
||||
matches: Vec::new(),
|
||||
cursor: 0,
|
||||
prompt,
|
||||
previous_pattern: (String::new(), FuzzyQuery::default()),
|
||||
truncate_start: true,
|
||||
show_preview: true,
|
||||
callback_fn: Box::new(callback_fn),
|
||||
completion_height: 0,
|
||||
widths: Vec::new(),
|
||||
preview_cache: HashMap::new(),
|
||||
read_buffer: Vec::with_capacity(1024),
|
||||
file_fn: Box::new(preview_fn),
|
||||
}
|
||||
|
||||
picker: unimplemented!(),
|
||||
};
|
||||
|
||||
picker.calculate_column_widths();
|
||||
|
||||
// scoring on empty input
|
||||
// TODO: just reuse score()
|
||||
picker
|
||||
.matches
|
||||
.extend(picker.options.iter().enumerate().map(|(index, option)| {
|
||||
let text = option.filter_text(&picker.editor_data);
|
||||
PickerMatch {
|
||||
index,
|
||||
score: 0,
|
||||
len: text.chars().count(),
|
||||
}
|
||||
}));
|
||||
|
||||
picker
|
||||
}
|
||||
|
||||
pub fn truncate_start(mut self, truncate_start: bool) -> Self {
|
||||
@ -150,6 +202,38 @@ pub fn truncate_start(mut self, truncate_start: bool) -> Self {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_options(&mut self, new_options: Vec<T>) {
|
||||
self.options = new_options;
|
||||
self.cursor = 0;
|
||||
self.force_score();
|
||||
self.calculate_column_widths();
|
||||
}
|
||||
|
||||
/// Calculate the width constraints using the maximum widths of each column
|
||||
/// for the current options.
|
||||
fn calculate_column_widths(&mut self) {
|
||||
let n = self
|
||||
.options
|
||||
.first()
|
||||
.map(|option| option.format(&self.editor_data).cells.len())
|
||||
.unwrap_or_default();
|
||||
let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| {
|
||||
let row = option.format(&self.editor_data);
|
||||
// maintain max for each column
|
||||
for (acc, cell) in acc.iter_mut().zip(row.cells.iter()) {
|
||||
let width = cell.content.width();
|
||||
if width > *acc {
|
||||
*acc = width;
|
||||
}
|
||||
}
|
||||
acc
|
||||
});
|
||||
self.widths = max_lens
|
||||
.into_iter()
|
||||
.map(|len| Constraint::Length(len as u16))
|
||||
.collect();
|
||||
}
|
||||
|
||||
fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
|
||||
self.picker
|
||||
.selection()
|
||||
@ -477,76 +561,11 @@ pub fn new(
|
||||
editor_data: T::Data,
|
||||
callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
|
||||
) -> Self {
|
||||
let prompt = Prompt::new(
|
||||
"".into(),
|
||||
None,
|
||||
ui::completers::none,
|
||||
|_editor: &mut Context, _pattern: &str, _event: PromptEvent| {},
|
||||
);
|
||||
|
||||
let mut picker = Self {
|
||||
options,
|
||||
editor_data,
|
||||
matcher: Box::default(),
|
||||
matches: Vec::new(),
|
||||
cursor: 0,
|
||||
prompt,
|
||||
previous_pattern: (String::new(), FuzzyQuery::default()),
|
||||
truncate_start: true,
|
||||
show_preview: true,
|
||||
callback_fn: Box::new(callback_fn),
|
||||
completion_height: 0,
|
||||
widths: Vec::new(),
|
||||
};
|
||||
|
||||
picker.calculate_column_widths();
|
||||
|
||||
// scoring on empty input
|
||||
// TODO: just reuse score()
|
||||
picker
|
||||
.matches
|
||||
.extend(picker.options.iter().enumerate().map(|(index, option)| {
|
||||
let text = option.filter_text(&picker.editor_data);
|
||||
PickerMatch {
|
||||
index,
|
||||
score: 0,
|
||||
len: text.chars().count(),
|
||||
}
|
||||
}));
|
||||
|
||||
picker
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub fn set_options(&mut self, new_options: Vec<T>) {
|
||||
self.options = new_options;
|
||||
self.cursor = 0;
|
||||
self.force_score();
|
||||
self.calculate_column_widths();
|
||||
}
|
||||
|
||||
/// Calculate the width constraints using the maximum widths of each column
|
||||
/// for the current options.
|
||||
fn calculate_column_widths(&mut self) {
|
||||
let n = self
|
||||
.options
|
||||
.first()
|
||||
.map(|option| option.format(&self.editor_data).cells.len())
|
||||
.unwrap_or_default();
|
||||
let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| {
|
||||
let row = option.format(&self.editor_data);
|
||||
// maintain max for each column
|
||||
for (acc, cell) in acc.iter_mut().zip(row.cells.iter()) {
|
||||
let width = cell.content.width();
|
||||
if width > *acc {
|
||||
*acc = width;
|
||||
}
|
||||
}
|
||||
acc
|
||||
});
|
||||
self.widths = max_lens
|
||||
.into_iter()
|
||||
.map(|len| Constraint::Length(len as u16))
|
||||
.collect();
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub fn score(&mut self) {
|
||||
|
Loading…
Reference in New Issue
Block a user