codemp/src/ffi/lua/buffer.rs

73 lines
2.2 KiB
Rust
Raw Normal View History

2024-10-06 10:18:58 +02:00
use crate::buffer::controller::Delta;
2024-10-03 04:10:52 +02:00
use crate::prelude::*;
2024-10-01 00:42:57 +02:00
use mlua::prelude::*;
use mlua_codemp_patch as mlua;
2024-09-17 23:00:30 +02:00
use super::ext::a_sync::a_sync;
use super::ext::from_lua_serde;
impl LuaUserData for CodempBufferController {
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
2024-10-01 00:42:57 +02:00
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| {
Ok(format!("{:?}", this))
});
2024-09-17 23:00:30 +02:00
2024-10-03 03:17:30 +02:00
methods.add_method("send", |_, this, (change,): (CodempTextChange,)| {
this.send(change)?;
Ok(())
});
2024-09-17 23:00:30 +02:00
2024-10-01 00:42:57 +02:00
methods.add_method(
"try_recv",
|_, this, ()| a_sync! { this => this.try_recv().await? },
);
2024-09-17 23:00:30 +02:00
methods.add_method("recv", |_, this, ()| a_sync! { this => this.recv().await? });
methods.add_method("poll", |_, this, ()| a_sync! { this => this.poll().await? });
2024-10-01 00:42:57 +02:00
methods.add_method(
"content",
|_, this, ()| a_sync! { this => this.content().await? },
);
2024-09-17 23:00:30 +02:00
2024-10-01 00:42:57 +02:00
methods.add_method("clear_callback", |_, this, ()| {
this.clear_callback();
Ok(())
});
methods.add_method("callback", |_, this, (cb,): (LuaFunction,)| {
this.callback(move |controller: CodempBufferController| {
super::ext::callback().invoke(cb.clone(), controller)
});
2024-09-17 23:00:30 +02:00
Ok(())
});
}
}
from_lua_serde! { CodempTextChange }
impl LuaUserData for CodempTextChange {
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("content", |_, this| Ok(this.content.clone()));
2024-10-01 00:42:57 +02:00
fields.add_field_method_get("start", |_, this| Ok(this.start));
fields.add_field_method_get("end", |_, this| Ok(this.end));
fields.add_field_method_get("hash", |_, this| Ok(this.hash));
2024-09-17 23:00:30 +02:00
// add a 'finish' accessor too because in Lua 'end' is reserved
2024-10-01 00:42:57 +02:00
fields.add_field_method_get("finish", |_, this| Ok(this.end));
2024-09-17 23:00:30 +02:00
}
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
2024-10-01 00:42:57 +02:00
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| {
Ok(format!("{:?}", this))
});
methods.add_method("apply", |_, this, (txt,): (String,)| Ok(this.apply(&txt)));
2024-09-17 23:00:30 +02:00
}
}
2024-10-03 03:41:28 +02:00
2024-10-06 10:18:58 +02:00
impl LuaUserData for Delta {
2024-10-03 03:41:28 +02:00
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("change", |_, this| Ok(this.change.clone()));
}
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
2024-10-06 10:18:58 +02:00
methods.add_method_mut("ack", |_, this, ()| Ok(this.ack()));
2024-10-03 03:41:28 +02:00
}
}