1
0
Fork 0
mirror of https://github.com/helix-editor/helix synced 2024-06-07 09:46:06 +02:00

prompt: make the callback a FnOnce.

This commit is contained in:
Blaž Hrastnik 2020-12-15 19:07:25 +09:00
parent 2bfdcede32
commit 1a843b6c06
2 changed files with 16 additions and 17 deletions

View File

@ -262,24 +262,23 @@ pub fn split_selection(cx: &mut Context) {
// # update state
// }
cx.callback = Some(Box::new(|compositor: &mut Compositor| {
let prompt = Prompt::new(
"split:".to_string(),
|input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
|editor: &mut Editor, input: &str| {
match Regex::new(input) {
Ok(regex) => {
let view = editor.view_mut().unwrap();
let text = &view.doc.text().slice(..);
let selection =
selection::split_on_matches(text, view.doc.selection(), &regex);
view.doc.set_selection(selection);
}
Err(_) => (), // TODO: mark command line as error
let prompt = Prompt::new(
"split:".to_string(),
|input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
|editor: &mut Editor, input: &str| {
match Regex::new(input) {
Ok(regex) => {
let view = editor.view_mut().unwrap();
let text = &view.doc.text().slice(..);
let selection = selection::split_on_matches(text, view.doc.selection(), &regex);
view.doc.set_selection(selection);
}
},
);
Err(_) => (), // TODO: mark command line as error
}
},
);
cx.callback = Some(Box::new(move |compositor: &mut Compositor| {
compositor.push(Box::new(prompt));
}));
}

View File

@ -19,7 +19,7 @@
use tui::buffer::Buffer as Surface;
use tui::layout::Rect;
pub type Callback = Box<dyn Fn(&mut Compositor)>;
pub type Callback = Box<dyn FnOnce(&mut Compositor)>;
// --> EventResult should have a callback that takes a context with methods like .popup(),
// .prompt() etc. That way we can abstract it from the renderer.