mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-23 07:41:03 +00:00
feat: going to next or previous occurrence is working with J and K
This commit is contained in:
parent
a29e5c9296
commit
520ff0adaf
6 changed files with 43 additions and 47 deletions
|
|
@ -29,7 +29,9 @@ impl Preview {
|
|||
impl UserData for Preview {
|
||||
fn add_fields<F: UserDataFields<Self>>(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()
|
||||
|
|
|
|||
|
|
@ -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 => {}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ impl From<ActionCow> 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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -9,9 +9,10 @@ use yazi_shared::pool::Symbol;
|
|||
#[derive(Clone, Debug)]
|
||||
pub struct PeekJob {
|
||||
pub previewer: Arc<yazi_config::plugin::Previewer>,
|
||||
pub file: yazi_fs::File,
|
||||
pub mime: Symbol<str>,
|
||||
pub skip: usize,
|
||||
pub file: yazi_fs::File,
|
||||
pub mime: Symbol<str>,
|
||||
pub skip: usize,
|
||||
pub search_idx: Option<usize>,
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue