diff --git a/src/event.rs b/src/event.rs new file mode 100644 index 0000000..711dbc9 --- /dev/null +++ b/src/event.rs @@ -0,0 +1,18 @@ +use std::path::PathBuf; + +use serde::{Deserialize, Serialize}; + +/// The kind of of event +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum PluginEventKind { + /// An editor is closed + FileEditorClosed, +} + +/// An event that can be handled. +/// Note that not all events are sent by default, and that you must subscribe to them! +#[derive(Debug, Serialize, Deserialize)] +pub enum PluginEvent { + /// A file-backed editor was closed + FileEditorClosed { path: PathBuf }, +} diff --git a/src/lib.rs b/src/lib.rs index bf9c1e0..cc70f98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,13 @@ +use event::PluginEventKind; use serde::{de::DeserializeOwned, Serialize}; use serde_json::{json, Value}; +pub mod event; + pub trait LapcePlugin { fn initialize(&mut self, configuration: serde_json::Value) {} + + fn update(&mut self, event: serde_json::Value) {} } #[macro_export] @@ -22,6 +27,15 @@ macro_rules! register_plugin { .initialize($crate::object_from_stdin().unwrap()); }); } + + #[no_mangle] + fn update() { + STATE.with(|state| { + state + .borrow_mut() + .update($crate::object_from_stdin().unwrap()); + }); + } }; } @@ -43,6 +57,16 @@ pub fn send_notification(method: &str, params: &Value) { unsafe { host_handle_notification() }; } +/// Subscribe to specific events to receive them in the [`LapcePlugin::update`] method +pub fn subscribe(events: &[PluginEventKind]) { + send_notification( + "subscribe", + &json!({ + "events": events, + }), + ) +} + pub fn start_lsp(exec_path: &str, language_id: &str, options: Option) { send_notification( "start_lsp_server",