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
18 changes: 18 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -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 },
}
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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());
});
}
};
}

Expand All @@ -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<Value>) {
send_notification(
"start_lsp_server",
Expand Down