From baa753176a59d8a0fa793c7d3c2a112a0994c955 Mon Sep 17 00:00:00 2001 From: mattwparas Date: Mon, 18 Sep 2023 22:00:03 -0700 Subject: [PATCH] add more functions for document and editor api --- helix-term/src/commands/engine/scheme.rs | 52 ++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/helix-term/src/commands/engine/scheme.rs b/helix-term/src/commands/engine/scheme.rs index 47d06f620..f88000009 100644 --- a/helix-term/src/commands/engine/scheme.rs +++ b/helix-term/src/commands/engine/scheme.rs @@ -336,9 +336,7 @@ impl super::PluginSystem for SteelScriptingEngine { }) } - fn available_commands<'a>( - &self, - ) -> Vec> { + fn available_commands<'a>(&self) -> Vec> { EXPORTED_IDENTIFIERS .identifiers .read() @@ -1027,7 +1025,40 @@ fn configure_engine() -> std::rc::Rc| todo!()); + engine.register_fn( + "Picker::new", + |values: steel::List| -> WrappedDynComponent { + let picker = ui::Picker::new( + Vec::new(), + PathBuf::from(""), + move |cx, path: &PathBuf, action| { + if let Err(e) = cx.editor.open(path, action) { + let err = if let Some(err) = e.source() { + format!("{}", err) + } else { + format!("unable to open \"{}\"", path.display()) + }; + cx.editor.set_error(err); + } + }, + ) + .with_preview(|_editor, path| Some((path.clone().into(), None))); + + let injector = picker.injector(); + + for file in values { + if injector.push(PathBuf::from(file)).is_err() { + break; + } + } + + WrappedDynComponent { + inner: Some(Box::new(ui::overlay::overlaid(picker))), + } + }, + ); + + // engine.register_fn("Picker::new", |values: Vec| todo!()); // engine.register_fn( // "Picker::new", @@ -1135,6 +1166,9 @@ fn configure_engine() -> std::rc::Rc("DocumentId?"); @@ -1445,6 +1479,16 @@ fn document_path(doc: &Document) -> Option { doc.path().and_then(|x| x.to_str()).map(|x| x.to_string()) } +// Get the time the document was focused +fn document_focused_at(doc: &Document) -> std::time::Instant { + doc.focused_at +} + +// Get all the editor documents +fn editor_all_documents(editor: &mut Editor) -> Vec { + editor.documents.keys().copied().collect() +} + fn switch(editor: &mut Editor, doc_id: DocumentId) { editor.switch(doc_id, Action::VerticalSplit) }