feat: allow overriding and rewriting the sync methods of builtin plugins (#1695)

This commit is contained in:
三咲雅 · Misaki Masa 2024-09-28 00:41:34 +08:00 committed by GitHub
parent 70fbe41cc2
commit 3e4973dbbf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 14 deletions

View file

@ -43,7 +43,7 @@ body:
label: Validations
description: Before submitting the issue, please make sure you have completed the following
options:
- label: I have searched the existing issues
required: true
- label: The latest nightly build of Yazi doesn't already have this feature
required: true
- label: I have searched the existing issues/discussions
required: true
- label: The latest nightly build of Yazi doesn't already have this feature
required: true

View file

@ -42,17 +42,25 @@ impl Require {
fn create_mt<'a>(lua: &'a Lua, id: &str, mod_: Table<'a>, sync: bool) -> mlua::Result<Table<'a>> {
let id: Arc<str> = Arc::from(id);
let mt = lua.create_table_from([(
"__index",
lua.create_function(move |lua, (ts, key): (Table, mlua::String)| {
match ts.raw_get::<_, Table>("__mod")?.raw_get::<_, Value>(&key)? {
Value::Function(_) => {
Self::create_wrapper(lua, id.clone(), key.to_str()?, sync)?.into_lua(lua)
let mt = lua.create_table_from([
(
"__index",
lua.create_function(move |lua, (ts, key): (Table, mlua::String)| {
match ts.raw_get::<_, Table>("__mod")?.raw_get::<_, Value>(&key)? {
Value::Function(_) => {
Self::create_wrapper(lua, id.clone(), key.to_str()?, sync)?.into_lua(lua)
}
v => Ok(v),
}
v => Ok(v),
}
})?,
)])?;
})?,
),
(
"__newindex",
lua.create_function(move |_, (ts, key, value): (Table, mlua::String, Value)| {
ts.raw_get::<_, Table>("__mod")?.raw_set(key, value)
})?,
),
])?;
let ts = lua.create_table_from([("__mod", mod_)])?;
ts.set_metatable(Some(mt));