From 3e4973dbbfa7d34a95b6005822db84db51ebddb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Sat, 28 Sep 2024 00:41:34 +0800 Subject: [PATCH] feat: allow overriding and rewriting the sync methods of builtin plugins (#1695) --- .github/ISSUE_TEMPLATE/feature.yml | 8 ++++---- yazi-plugin/src/loader/require.rs | 28 ++++++++++++++++++---------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/feature.yml b/.github/ISSUE_TEMPLATE/feature.yml index b4ef8f186..1ac3a36e5 100644 --- a/.github/ISSUE_TEMPLATE/feature.yml +++ b/.github/ISSUE_TEMPLATE/feature.yml @@ -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 diff --git a/yazi-plugin/src/loader/require.rs b/yazi-plugin/src/loader/require.rs index c5869a783..5f29f3bfa 100644 --- a/yazi-plugin/src/loader/require.rs +++ b/yazi-plugin/src/loader/require.rs @@ -42,17 +42,25 @@ impl Require { fn create_mt<'a>(lua: &'a Lua, id: &str, mod_: Table<'a>, sync: bool) -> mlua::Result> { let id: Arc = 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));