mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: support calling methods in builtin plugins with arbitrary types of arguments (self can now be omitted) (#1666)
This commit is contained in:
parent
e858d11ce0
commit
ac196f211e
3 changed files with 35 additions and 27 deletions
|
|
@ -13,7 +13,7 @@ function M:peek()
|
||||||
:spawn()
|
:spawn()
|
||||||
|
|
||||||
if not child then
|
if not child then
|
||||||
return self:fallback_to_builtin()
|
return require("code").peek(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
local limit = self.area.h
|
local limit = self.area.h
|
||||||
|
|
@ -21,7 +21,7 @@ function M:peek()
|
||||||
repeat
|
repeat
|
||||||
local next, event = child:read_line()
|
local next, event = child:read_line()
|
||||||
if event == 1 then
|
if event == 1 then
|
||||||
return self:fallback_to_builtin()
|
return require("code").peek(self)
|
||||||
elseif event ~= 0 then
|
elseif event ~= 0 then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
@ -52,15 +52,4 @@ function M:seek(units)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M:fallback_to_builtin()
|
|
||||||
local err, bound = ya.preview_code(self)
|
|
||||||
if bound then
|
|
||||||
ya.manager_emit("peek", { bound, only_if = self.file.url, upper_bound = true })
|
|
||||||
elseif err and not err:find("cancelled", 1, true) then
|
|
||||||
ya.preview_widgets(self, {
|
|
||||||
ui.Paragraph(self.area, { ui.Line(err):reverse() }),
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -41,13 +41,11 @@ impl Require {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_mt<'a>(lua: &'a Lua, id: &str, mod_: Table<'a>, sync: bool) -> mlua::Result<Table<'a>> {
|
fn create_mt<'a>(lua: &'a Lua, id: &str, mod_: Table<'a>, sync: bool) -> mlua::Result<Table<'a>> {
|
||||||
let ts = lua.create_table_from([("_mod", mod_.into_lua(lua)?)])?;
|
|
||||||
|
|
||||||
let id: Arc<str> = Arc::from(id);
|
let id: Arc<str> = Arc::from(id);
|
||||||
let mt = lua.create_table_from([(
|
let mt = lua.create_table_from([(
|
||||||
"__index",
|
"__index",
|
||||||
lua.create_function(move |lua, (ts, key): (Table, mlua::String)| {
|
lua.create_function(move |lua, (ts, key): (Table, mlua::String)| {
|
||||||
match ts.raw_get::<_, Table>("_mod")?.raw_get::<_, Value>(&key)? {
|
match ts.raw_get::<_, Table>("__mod")?.raw_get::<_, Value>(&key)? {
|
||||||
Value::Function(_) => {
|
Value::Function(_) => {
|
||||||
Self::create_wrapper(lua, id.clone(), key.to_str()?, sync)?.into_lua(lua)
|
Self::create_wrapper(lua, id.clone(), key.to_str()?, sync)?.into_lua(lua)
|
||||||
}
|
}
|
||||||
|
|
@ -56,6 +54,7 @@ impl Require {
|
||||||
})?,
|
})?,
|
||||||
)])?;
|
)])?;
|
||||||
|
|
||||||
|
let ts = lua.create_table_from([("__mod", mod_)])?;
|
||||||
ts.set_metatable(Some(mt));
|
ts.set_metatable(Some(mt));
|
||||||
Ok(ts)
|
Ok(ts)
|
||||||
}
|
}
|
||||||
|
|
@ -69,24 +68,45 @@ impl Require {
|
||||||
let f: Arc<str> = Arc::from(f);
|
let f: Arc<str> = Arc::from(f);
|
||||||
|
|
||||||
if sync {
|
if sync {
|
||||||
lua.create_function(move |lua, (ts, args): (Table, MultiValue)| {
|
lua.create_function(move |lua, args: MultiValue| {
|
||||||
let mod_: Table = ts.raw_get::<_, Table>("_mod")?;
|
let (mod_, args) = Self::split_mod_and_args(lua, &id, args)?;
|
||||||
lua.named_registry_value::<RtRef>("rt")?.push(&id);
|
lua.named_registry_value::<RtRef>("rt")?.push(&id);
|
||||||
let result = mod_.call_method::<_, MultiValue>(&f, args);
|
let result = mod_.call_function::<_, MultiValue>(&f, args);
|
||||||
lua.named_registry_value::<RtRef>("rt")?.pop();
|
lua.named_registry_value::<RtRef>("rt")?.pop();
|
||||||
result
|
result
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
lua.create_async_function(move |lua, (ts, args): (Table, MultiValue)| {
|
lua.create_async_function(move |lua, args: MultiValue| {
|
||||||
let (id, f) = (id.clone(), f.clone());
|
let (id, f) = (id.clone(), f.clone());
|
||||||
async move {
|
async move {
|
||||||
let mod_: Table = ts.raw_get::<_, Table>("_mod")?;
|
let (mod_, args) = Self::split_mod_and_args(lua, &id, args)?;
|
||||||
lua.named_registry_value::<RtRef>("rt")?.push(&id);
|
lua.named_registry_value::<RtRef>("rt")?.push(&id);
|
||||||
let result = mod_.call_async_method::<_, MultiValue>(&f, args).await;
|
let result = mod_.call_async_function::<_, MultiValue>(&f, args).await;
|
||||||
lua.named_registry_value::<RtRef>("rt")?.pop();
|
lua.named_registry_value::<RtRef>("rt")?.pop();
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn split_mod_and_args<'a>(
|
||||||
|
lua: &'a Lua,
|
||||||
|
id: &str,
|
||||||
|
mut args: MultiValue<'a>,
|
||||||
|
) -> mlua::Result<(Table<'a>, MultiValue<'a>)> {
|
||||||
|
let Some(front) = args.pop_front() else {
|
||||||
|
return Ok((LOADER.load(lua, id)?, args));
|
||||||
|
};
|
||||||
|
let Value::Table(tbl) = front else {
|
||||||
|
args.push_front(front);
|
||||||
|
return Ok((LOADER.load(lua, id)?, args));
|
||||||
|
};
|
||||||
|
Ok(if let Ok(mod_) = tbl.raw_get::<_, Table>("__mod") {
|
||||||
|
args.push_front(Value::Table(mod_.clone()));
|
||||||
|
(mod_, args)
|
||||||
|
} else {
|
||||||
|
args.push_front(Value::Table(tbl));
|
||||||
|
(LOADER.load(lua, id)?, args)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use mlua::{ExternalError, ExternalResult, Function, IntoLua, Lua, MultiValue, Table, Value};
|
use mlua::{ExternalError, ExternalResult, Function, Lua, MultiValue, Table, Value};
|
||||||
use tokio::sync::oneshot;
|
use tokio::sync::oneshot;
|
||||||
use yazi_dds::Sendable;
|
use yazi_dds::Sendable;
|
||||||
use yazi_shared::{emit, event::{Cmd, Data}, Layer};
|
use yazi_shared::{emit, event::{Cmd, Data}, Layer};
|
||||||
|
|
@ -17,10 +17,9 @@ impl Utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
let cur = rt.current().unwrap().to_owned();
|
let cur = rt.current().unwrap().to_owned();
|
||||||
lua.create_function(move |lua, args: MultiValue| {
|
lua.create_function(move |lua, mut args: MultiValue| {
|
||||||
f.call::<_, MultiValue>(MultiValue::from_iter(
|
args.push_front(Value::Table(LOADER.load(lua, &cur)?));
|
||||||
[LOADER.load(lua, &cur)?.into_lua(lua)?].into_iter().chain(args),
|
f.call::<_, MultiValue>(args)
|
||||||
))
|
|
||||||
})
|
})
|
||||||
})?,
|
})?,
|
||||||
)?;
|
)?;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue