Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feat/close buffer from picker #4522

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
06c5cd1
working implementation of closing a buffer from file picker
EricHenry Oct 29, 2022
00eaa22
reset cursor position on deletion
EricHenry Oct 29, 2022
d2f5c5c
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry Oct 29, 2022
e21ed37
remove unused variants
EricHenry Oct 29, 2022
462cb4b
fix clippy warning
EricHenry Oct 30, 2022
48dfb7b
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry Nov 3, 2022
04b4a60
make key event callback optional on picker
EricHenry Nov 3, 2022
7bd7ae7
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry Jan 8, 2023
ebe4548
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry Feb 5, 2023
7e3223e
fix settign new options
EricHenry Feb 5, 2023
357a997
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry Feb 26, 2023
304dfd8
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry Mar 4, 2023
f17e813
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry Mar 5, 2023
add2d01
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry May 27, 2023
0d77aa7
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry May 30, 2023
0bc3558
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry Jun 4, 2023
e5a50ad
implementation without callback
EricHenry Jun 4, 2023
e2b1ca1
simplify key event callback
EricHenry Jun 5, 2023
439b79e
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry Jun 5, 2023
44f9e8f
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry Jun 6, 2023
edf3cd9
Merge branch 'master' of github.com:helix-editor/helix into feat/clos…
EricHenry Jun 23, 2023
88a1b59
update to implement on new picker
EricHenry Jun 23, 2023
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
37 changes: 34 additions & 3 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ use movement::Movement;
use crate::{
args,
compositor::{self, Component, Compositor},
filter_picker_entry,
ctrl, filter_picker_entry,
job::Callback,
keymap::ReverseKeymap,
ui::{
self, editor::InsertEvent, lsp::SignatureHelp, overlay::overlaid, CompletionItem, Picker,
Popup, Prompt, PromptEvent,
PickerAction, Popup, Prompt, PromptEvent,
},
};

Expand Down Expand Up @@ -2559,7 +2559,7 @@ fn buffer_picker(cx: &mut Context) {
}
}

let new_meta = |doc: &Document| BufferMeta {
let new_meta = move |doc: &Document| BufferMeta {
id: doc.id(),
path: doc.path().cloned(),
is_modified: doc.is_modified(),
Expand Down Expand Up @@ -2588,6 +2588,37 @@ fn buffer_picker(cx: &mut Context) {
.primary()
.cursor_line(doc.text().slice(..));
Some((meta.id.into(), Some((line, line))))
})
.on_key_event(move |cx, meta, key_event| match key_event {
ctrl!('x') => {
if cx.editor.close_document(meta.id, false).is_err() {
cx.editor.set_error("Cannot close buffer");
None
} else {
let updated_options = cx
.editor
.documents
.iter()
.map(|(_, doc)| new_meta(doc))
.collect();
Some(PickerAction::UpdateOptions(updated_options))
}
}
ctrl!('X') => {
if cx.editor.close_document(meta.id, true).is_err() {
cx.editor.set_error("Cannot force close buffer");
None
} else {
let updated_options = cx
.editor
.documents
.iter()
.map(|(_, doc)| new_meta(doc))
.collect();
Some(PickerAction::UpdateOptions(updated_options))
}
}
_ => None,
});
cx.push_layer(Box::new(overlaid(picker)));
}
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub use completion::{Completion, CompletionItem};
pub use editor::EditorView;
pub use markdown::Markdown;
pub use menu::Menu;
pub use picker::{DynamicPicker, FileLocation, Picker};
pub use picker::{DynamicPicker, FileLocation, Picker, PickerAction};
pub use popup::Popup;
pub use prompt::{Prompt, PromptEvent};
pub use spinner::{ProgressSpinners, Spinner};
Expand Down
29 changes: 27 additions & 2 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use helix_core::{
use helix_view::{
editor::Action,
graphics::{CursorKind, Margin, Modifier, Rect},
input::KeyEvent,
theme::Style,
view::ViewPosition,
Document, DocumentId, Editor,
Expand Down Expand Up @@ -74,6 +75,12 @@ impl From<DocumentId> for PathOrId {

type FileCallback<T> = Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>;

pub enum PickerAction<T: Item> {
UpdateOptions(Vec<T>),
}

type KeyEventCallback<T> = Box<dyn Fn(&mut Context, &T, &KeyEvent) -> Option<PickerAction<T>>>;

/// File path and range of lines (used to align and highlight lines)
pub type FileLocation = (PathOrId, Option<(usize, usize)>);

Expand Down Expand Up @@ -132,7 +139,6 @@ pub struct Picker<T: Item> {
show_preview: bool,
/// Constraints for tabular formatting
widths: Vec<Constraint>,

callback_fn: PickerCallback<T>,

pub truncate_start: bool,
Expand All @@ -141,6 +147,7 @@ pub struct Picker<T: Item> {
read_buffer: Vec<u8>,
/// Given an item in the picker, return the file path and line number to display.
file_fn: Option<FileCallback<T>>,
key_event_callback: Option<KeyEventCallback<T>>,
}

impl<T: Item + 'static> Picker<T> {
Expand Down Expand Up @@ -172,6 +179,7 @@ impl<T: Item + 'static> Picker<T> {
preview_cache: HashMap::new(),
read_buffer: Vec::with_capacity(1024),
file_fn: None,
key_event_callback: None,
};

picker.calculate_column_widths();
Expand Down Expand Up @@ -205,6 +213,14 @@ impl<T: Item + 'static> Picker<T> {
self
}

pub fn on_key_event(
mut self,
callback: impl Fn(&mut Context, &T, &KeyEvent) -> Option<PickerAction<T>> + 'static,
) -> Self {
self.key_event_callback = Some(Box::new(callback));
self
}

pub fn set_options(&mut self, new_options: Vec<T>) {
self.options = new_options;
self.cursor = 0;
Expand Down Expand Up @@ -849,7 +865,16 @@ impl<T: Item + 'static> Component for Picker<T> {
self.toggle_preview();
}
_ => {
self.prompt_handle_event(event, ctx);
match self.selection().and_then(|option| {
self.key_event_callback
.as_ref()
.and_then(|cb| cb(ctx, option, &key_event))
}) {
Some(PickerAction::UpdateOptions(options)) => self.set_options(options),
None => {
self.prompt_handle_event(event, ctx);
}
};
}
}

Expand Down