Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
54 changes: 36 additions & 18 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use helix_core::{
};
use helix_view::{
document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
editor::Action,
editor::{Action, CloseError},
expansion,
info::Info,
input::KeyEvent,
Expand Down Expand Up @@ -3157,8 +3157,6 @@ fn file_explorer_in_current_directory(cx: &mut Context) {
}

fn buffer_picker(cx: &mut Context) {
let current = view!(cx.editor).doc;

struct BufferMeta {
id: DocumentId,
path: Option<PathBuf>,
Expand All @@ -3167,23 +3165,30 @@ fn buffer_picker(cx: &mut Context) {
focused_at: std::time::Instant,
}

let new_meta = |doc: &Document| BufferMeta {
id: doc.id(),
path: doc.path().cloned(),
is_modified: doc.is_modified(),
is_current: doc.id() == current,
focused_at: doc.focused_at,
};
let get_items = |editor: &Editor| -> Vec<BufferMeta> {
let current = view!(editor).doc;

let mut items = cx
.editor
.documents
.values()
.map(new_meta)
.collect::<Vec<BufferMeta>>();
let new_meta = |doc: &Document| BufferMeta {
id: doc.id(),
path: doc.path().cloned(),
is_modified: doc.is_modified(),
is_current: doc.id() == current,
focused_at: doc.focused_at,
};

// mru
items.sort_unstable_by_key(|item| std::cmp::Reverse(item.focused_at));
let mut items = editor
.documents
.values()
.map(new_meta)
.collect::<Vec<BufferMeta>>();

// mru
items.sort_unstable_by_key(|item| std::cmp::Reverse(item.focused_at));

items
};

let items = get_items(cx.editor);

let columns = [
PickerColumn::new("id", |meta: &BufferMeta, _| meta.id.to_string().into()),
Expand Down Expand Up @@ -3234,6 +3239,19 @@ fn buffer_picker(cx: &mut Context) {
(cursor_line, cursor_line)
});
Some((meta.id.into(), lines))
})
.with_delete_item(move |editor, meta| {
if let Err(err) = editor.close_document(meta.id, false) {
editor.set_error(match err {
CloseError::BufferModified(s) => format!("Could not close modified buffer: {}", s),
CloseError::SaveError(s) => format!("Could not close buffer: {}", s),
CloseError::DoesNotExist => "Buffer does not exist".to_owned(),
});
} else {
editor.clear_status();
}

get_items(editor)
});
cx.push_layer(Box::new(overlaid(picker)));
}
Expand Down
23 changes: 23 additions & 0 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ pub struct Picker<T: 'static + Send + Sync, D: 'static> {
/// An event handler for syntax highlighting the currently previewed file.
preview_highlight_handler: Sender<Arc<Path>>,
dynamic_query_handler: Option<Sender<DynamicQueryChange>>,
/// Called to delete an item from the picker's list.
delete_item_fn: Option<PickerDeleteItem<T>>,
}

impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
Expand Down Expand Up @@ -394,6 +396,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
file_fn: None,
preview_highlight_handler: PreviewHighlightHandler::<T, D>::default().spawn(),
dynamic_query_handler: None,
delete_item_fn: None,
}
}

Expand Down Expand Up @@ -434,6 +437,11 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
self
}

pub fn with_delete_item(mut self, f: impl Fn(&mut Editor, &T) -> Vec<T> + 'static) -> Self {
self.delete_item_fn = Some(Box::new(f));
self
}

pub fn with_dynamic_query(
mut self,
callback: DynQueryCallback<T, D>,
Expand Down Expand Up @@ -1143,6 +1151,20 @@ impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I,
ctrl!('t') => {
self.toggle_preview();
}
alt!('d') | key!(Delete) => {
if let Some(option) = self.selection() {
if let Some(delete_fn) = &self.delete_item_fn {
let new_items = delete_fn(ctx.editor, option);
let new_len = new_items.len() as u32;
self.matcher.restart(false);
let injector = self.matcher.injector();
for item in new_items {
inject_nucleo_item(&injector, &self.columns, item, &self.editor_data);
}
self.cursor = self.cursor.min(new_len.saturating_sub(1));
}
}
}
_ => {
self.prompt_handle_event(event, ctx);
}
Expand Down Expand Up @@ -1187,3 +1209,4 @@ impl<T: 'static + Send + Sync, D> Drop for Picker<T, D> {
}

type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>;
type PickerDeleteItem<T> = Box<dyn Fn(&mut Editor, &T) -> Vec<T>>;