diff --git a/yazi-binding/src/runtime.rs b/yazi-binding/src/runtime.rs index 7e4f4d4c..b1493bca 100644 --- a/yazi-binding/src/runtime.rs +++ b/yazi-binding/src/runtime.rs @@ -41,6 +41,11 @@ impl Runtime { self.frames.back().map(|f| f.id.as_str()).context("No current runtime frame") } + pub fn current_module(&self) -> Result<&str> { + let s = self.current()?; + Ok(s.split('.').next().unwrap_or(s)) + } + pub fn current_owned(&self) -> Result { self.current().map(ToOwned::to_owned) } pub fn get_block(&self, id: &str, calls: usize) -> Option { diff --git a/yazi-core/src/app/plugin.rs b/yazi-core/src/app/plugin.rs index 505db625..900186f3 100644 --- a/yazi-core/src/app/plugin.rs +++ b/yazi-core/src/app/plugin.rs @@ -55,9 +55,9 @@ impl PluginOpt { fn normalize_id(s: SStr) -> SStr { match s { - Cow::Borrowed(s) => s.trim_end_matches(".main").into(), + Cow::Borrowed(s) => s.strip_suffix(".main").unwrap_or(s).into(), Cow::Owned(mut s) => { - s.truncate(s.trim_end_matches(".main").len()); + s.truncate(s.strip_suffix(".main").unwrap_or(&s).len()); s.into() } } diff --git a/yazi-runner/src/loader/loader.rs b/yazi-runner/src/loader/loader.rs index 9a07bbf1..d5c99e18 100644 --- a/yazi-runner/src/loader/loader.rs +++ b/yazi-runner/src/loader/loader.rs @@ -83,7 +83,7 @@ impl Loader { where F: FnOnce(&Chunk) -> T, { - let (id, plugin, entry) = Self::normalize_id(id)?; + let (id, plugin, entry) = Self::explode_id_parts(id)?; if let Some(c) = self.cache.read().get(id) { return Self::compatible_or_error(id, c).map(|_| f(c)); } @@ -103,7 +103,7 @@ impl Loader { } pub async fn load(&self, lua: &Lua, id: &str) -> mlua::Result { - let (id, ..) = Self::normalize_id(id)?; + let (id, ..) = Self::explode_id_parts(id)?; let loaded: Table = lua.globals().raw_get::
("package")?.raw_get("loaded")?; if let Ok(t) = loaded.raw_get(id) { @@ -118,7 +118,7 @@ impl Loader { } async fn load_new(&self, lua: &Lua, id: &str) -> mlua::Result
{ - let (id, ..) = Self::normalize_id(id)?; + let (id, ..) = Self::explode_id_parts(id)?; let mut mode = ChunkMode::Text; let f = match self.cache.read().get(id) { @@ -141,7 +141,7 @@ impl Loader { } pub fn load_chunk(&self, lua: &Lua, id: &str, chunk: &Chunk) -> mlua::Result
{ - let (id, ..) = Self::normalize_id(id)?; + let (id, ..) = Self::explode_id_parts(id)?; let loaded: Table = lua.globals().raw_get::
("package")?.raw_get("loaded")?; if let Ok(t) = loaded.raw_get(id) { @@ -156,7 +156,7 @@ impl Loader { } pub fn try_load(&self, lua: &Lua, id: &str) -> mlua::Result
{ - let (id, ..) = Self::normalize_id(id)?; + let (id, ..) = Self::explode_id_parts(id)?; lua.globals().raw_get::
("package")?.raw_get::
("loaded")?.raw_get(id) } @@ -172,8 +172,8 @@ impl Loader { ); } - pub fn normalize_id(id: &str) -> anyhow::Result<(&str, &str, &str)> { - let id = id.trim_end_matches(".main"); + fn explode_id_parts(id: &str) -> anyhow::Result<(&str, &str, &str)> { + let id = id.strip_suffix(".main").unwrap_or(id); let (plugin, entry) = if let Some((a, b)) = id.split_once(".") { (a, b) } else { (id, "main") }; ensure!(plugin.as_bytes().kebab_cased(), "Plugin name `{plugin}` must be in kebab-case"); diff --git a/yazi-runner/src/loader/require.rs b/yazi-runner/src/loader/require.rs index 601c66de..507588fc 100644 --- a/yazi-runner/src/loader/require.rs +++ b/yazi-runner/src/loader/require.rs @@ -89,10 +89,10 @@ impl Require { } fn absolute_id<'a>(lua: &Lua, id: &'a str) -> mlua::Result> { - let Some(stripped) = id.strip_prefix('.') else { return Ok(id.into()) }; - - let rt = runtime!(lua)?; - let cur = rt.current()?; - Ok(format!("{}.{stripped}", cur.split('.').next().unwrap_or(cur)).into()) + Ok(match id.strip_prefix('.') { + None => id.strip_suffix(".main").unwrap_or(id).into(), + Some("main") => runtime!(lua)?.current_module()?.to_owned().into(), + Some(rel) => format!("{}.{rel}", runtime!(lua)?.current_module()?).into(), + }) } }