diff --git a/yazi-actor/src/lives/preview.rs b/yazi-actor/src/lives/preview.rs index 754e768a..5390f48d 100644 --- a/yazi-actor/src/lives/preview.rs +++ b/yazi-actor/src/lives/preview.rs @@ -29,7 +29,9 @@ impl Preview { impl UserData for Preview { fn add_fields>(fields: &mut F) { fields.add_field_method_get("skip", |_, me| Ok(me.skip)); + // TODO: Make sure we need this ? fields.add_field_method_get("search_idx", |_, me| Ok(me.search_idx)); + cached_field!(fields, folder, |_, me| { me.tab .hovered_folder() diff --git a/yazi-actor/src/mgr/peek.rs b/yazi-actor/src/mgr/peek.rs index 739ac081..4399ed55 100644 --- a/yazi-actor/src/mgr/peek.rs +++ b/yazi-actor/src/mgr/peek.rs @@ -1,7 +1,7 @@ use anyhow::Result; use yazi_macro::succ; use yazi_parser::mgr::PeekForm; -use yazi_shared::{data::Data, url::UrlLike}; +use yazi_shared::data::Data; use crate::{Actor, Ctx}; @@ -46,10 +46,8 @@ impl Actor for Peek { } } - tracing::debug!("peeeek boy before set stuff"); match form.search_idx { Some(index) => { - tracing::debug!("search index setting {}", index); preview.search_idx = Some(index); } None => {} diff --git a/yazi-core/src/tab/preview.rs b/yazi-core/src/tab/preview.rs index f8cd9d3d..d1f9f4fe 100644 --- a/yazi-core/src/tab/preview.rs +++ b/yazi-core/src/tab/preview.rs @@ -43,10 +43,9 @@ impl Preview { self.abort(); - // let search_first_occurrence = self.get_first_search_occurrence(&file.url); - let job = PeekJob { previewer, file, mime, skip: self.skip }; + let job = PeekJob { previewer, file, mime, skip: self.skip, search_idx: self.search_idx }; - tracing::debug!("preview {:?}", self); + tracing::debug!("preview before to peek {:?}", self.search_idx); self.handle = Some(tokio::spawn(async move { let mut rx = RUNNER.peek(&job).await; diff --git a/yazi-parser/src/mgr/peek.rs b/yazi-parser/src/mgr/peek.rs index 69a296b4..bccdaf20 100644 --- a/yazi-parser/src/mgr/peek.rs +++ b/yazi-parser/src/mgr/peek.rs @@ -17,7 +17,7 @@ impl From for PeekForm { force: a.bool("force"), only_if: a.take("only-if").ok(), upper_bound: a.bool("upper-bound"), - search_idx: a.take("search_idx").ok(), + search_idx: a.take("search-idx").ok(), } } } diff --git a/yazi-plugin/preset/plugins/code.lua b/yazi-plugin/preset/plugins/code.lua index 0f239a80..6ad206ee 100644 --- a/yazi-plugin/preset/plugins/code.lua +++ b/yazi-plugin/preset/plugins/code.lua @@ -18,35 +18,36 @@ local function get_search_occurrences(url) return lines end --- local function get_next_occurrence(occurrences, current_line, direction) --- local step = direction == "up" and -1 or 1 --- local start = direction == "up" and #occurrences or 1 --- local stop = direction == "up" and 1 or #occurrences --- --- for i = start, stop, step do --- local line = occurrences[i] --- if go_up and line >= current_line then --- return line --- end --- if direction == "down" and line <= current_line then --- return line --- end --- --- -- TODO: Handle return to first element or last one --- end --- end +local function get_next_occurrence_idx(search_idx, direction, occurrences_len) + local index + + if direction == "up" then + index = (search_idx or 1) - 1 + if index < 1 then + index = occurrences_len + end + else + index = (search_idx or 1) + 1 + if index > occurrences_len then + index = 1 + end + end + + return index +end function M:peek(job) local search_occurrences = get_search_occurrences(job.file.url) - local search_idx = cx.active.preview.search_idx - ya.dbg(" search index", search_idx) + local search_idx = job.search_idx + + ya.dbg("code.lua: search_occurrences:", search_occurrences) + local occurrence if search_occurrences and search_idx then occurrence = search_occurrences[search_idx] end - ya.dgb("Peek Occurrence:", occurrence) - + ya.dbg("code.lua: Occurrence:", occurrence) -- Todo: Pass the occurrence for highlighting local err, bound = ya.preview_code(job) if bound then @@ -60,7 +61,7 @@ function M:seek(job) local direction = job.units > 0 and "down" or "up" local search_idx = cx.active.preview.search_idx - ya.dbg(" search index", search_idx) + ya.dbg("search index before to set it in seek", search_idx) local h = cx.active.current.hovered if not h or h.url ~= job.file.url then @@ -69,19 +70,13 @@ function M:seek(job) local search_occurrences = get_search_occurrences(job.file.url) if search_occurrences then - search_idx = (search_idx or 0) + 1 - -- local current_line = cx.active.preview.skip + 1 - -- ya.dbg("Current line:", current_line) - -- local next_occurrence = get_next_occurrence(search_occurrences, current_line, direction) - -- for _, occurrence in ipairs(search_occurrences) do - -- local search_line = occurrence[1] - -- -- local col = occurrence[2] - -- - -- if search_line >= current_line then - -- ya.emit("peek", { math.max(0, search_line - 1), only_if = job.file.url }) - -- return - -- end - -- end + local next_occurrence_idx = get_next_occurrence_idx(search_idx, direction, #search_occurrences) + local occurrence = search_occurrences[next_occurrence_idx] + local line = occurrence[1] + -- local col = occurrence[2] + + ya.emit("peek", { math.max(0, line - 1), only_if = job.file.url, search_idx = next_occurrence_idx }) + return end -- ya.emit("peek", { math.max(0, line - 1), only_if = job.file.url }) @@ -92,7 +87,7 @@ function M:seek(job) ya.emit("peek", { math.max(0, cx.active.preview.skip + step), only_if = job.file.url, - search_idx, + search_idx = search_idx, }) end diff --git a/yazi-runner/src/previewer/job.rs b/yazi-runner/src/previewer/job.rs index 09a2a1ab..f2a492a2 100644 --- a/yazi-runner/src/previewer/job.rs +++ b/yazi-runner/src/previewer/job.rs @@ -9,9 +9,10 @@ use yazi_shared::pool::Symbol; #[derive(Clone, Debug)] pub struct PeekJob { pub previewer: Arc, - pub file: yazi_fs::File, - pub mime: Symbol, - pub skip: usize, + pub file: yazi_fs::File, + pub mime: Symbol, + pub skip: usize, + pub search_idx: Option, } impl IntoLua for PeekJob { @@ -23,6 +24,7 @@ impl IntoLua for PeekJob { ("file", File::new(self.file).into_lua(lua)?), ("mime", self.mime.into_lua(lua)?), ("skip", self.skip.into_lua(lua)?), + ("search_idx", self.search_idx.into_lua(lua)?), ])? .into_lua(lua) } @@ -31,7 +33,7 @@ impl IntoLua for PeekJob { // --- Seek #[derive(Clone, Debug)] pub struct SeekJob { - pub file: yazi_fs::File, + pub file: yazi_fs::File, pub units: i16, }