feat: pass position to preview

This commit is contained in:
Jed 2026-06-14 22:55:02 +02:00
parent 520ff0adaf
commit 21ed8e0167
4 changed files with 25 additions and 14 deletions

View file

@ -1,19 +1,19 @@
use mlua::{
ExternalError, FromLua, IntoLua, Lua, Result as LuaResult, Table, UserData, UserDataMethods,
Value,
};
use mlua::{ExternalError, FromLua, IntoLua, Lua, Result as LuaResult, Value};
const EXPECTED: &str = "expected a table containing a line and a length";
#[derive(Clone, Debug)]
pub struct HighlightPosition {
pub line: usize,
pub col: usize,
pub length: usize,
}
impl HighlightPosition {
pub fn compose(lua: &Lua) -> LuaResult<Value> {
let new = lua.create_function(|_, (line, length): (usize, usize)| Ok(Self { line, length }))?;
let new = lua.create_function(|_, (line, col, length): (usize, usize, usize)| {
Ok(Self { line, col, length })
})?;
let tbl = lua.create_table_from([("new", new)])?;
tbl.into_lua(lua)
@ -23,7 +23,11 @@ impl HighlightPosition {
impl FromLua for HighlightPosition {
fn from_lua(value: Value, _: &Lua) -> mlua::Result<Self> {
match value {
Value::Table(tbl) => Ok(Self { line: tbl.raw_get("line")?, length: tbl.raw_get("length")? }),
Value::Table(tbl) => Ok(Self {
line: tbl.raw_get("line")?,
col: tbl.raw_get("col")?,
length: tbl.raw_get("length")?,
}),
_ => Err(EXPECTED.into_lua_err()),
}
}
@ -33,6 +37,7 @@ impl IntoLua for HighlightPosition {
fn into_lua(self, lua: &Lua) -> LuaResult<Value> {
let tbl = lua.create_table()?;
tbl.set("line", self.line)?;
tbl.set("col", self.col)?;
tbl.set("length", self.length)?;
Ok(Value::Table(tbl))
}

View file

@ -54,6 +54,8 @@ impl Highlighter {
let path = path.into();
let (theme, syntaxes) = CACHE.get_or_init(Self::load);
// tracing::debug!("{:?}", theme);
Ok(Self {
reader: BufReader::new(std::fs::File::open(&path)?),
path,
@ -82,6 +84,7 @@ impl Highlighter {
let mut lines = Vec::with_capacity(self.size.height as usize);
let mut inspected = 0u16;
while self.reader.read_until(b'\n', &mut buf).is_ok_and(|n| n > 0) {
// tracing::debug!("{:?}", buf);
if Self::is_binary(&buf, &mut inspected) {
Err(anyhow!("Binary file"))?;
}

View file

@ -19,8 +19,7 @@ pub struct RgOpt {
pub fn rg(opt: RgOpt) -> Result<UnboundedReceiver<File>> {
let mut child = Command::new("rg")
.args(["--color=never", "--no-heading", "--column", "--smart-case"])
// .args(["--color=never", "--files-with-matches", "--smart-case"])
.args(["--color=never", "--no-heading", "--column", "--smart-case"])
.arg(if opt.hidden { "--hidden" } else { "--no-hidden" })
.args(opt.args)
.arg(opt.subject)

View file

@ -18,18 +18,22 @@ impl Utils {
pub(super) fn preview_code(lua: &Lua) -> mlua::Result<Function> {
lua.create_async_function(|lua, t: Table| async move {
let area: Area = t.raw_get("area")?;
// let position: Option<HighlightPosition> = t.raw_get("position").ok();
let position: Option<HighlightPosition> = t.raw_get("position").ok();
tracing::debug!("{:?}", position);
let mut lock = PreviewLock::try_from(t)?;
let path = lock.url.as_url().unified_path();
// let skip = position.map(|p| p.line).unwrap_or(lock.skip);
let size = area.size();
let search_subject = lock.url.as_url().scheme().domain();
// tracing::debug!("skip size: {size}");
tracing::debug!("search subject{:?}", search_subject);
let inner = match Highlighter::oneshot(path, lock.skip, size).await {
Ok(text) => text,
let inner = match Highlighter::oneshot(path, lock.skip, area.size()).await {
Ok(text) => {
// tracing::debug!("{:?}", text);
text
}
Err(e @ PeekError::Exceeded(max)) => return (e, max).into_lua_multi(&lua),
Err(e) => {
return e.into_lua_multi(&lua);