Skip to main content

ytil_noxi/
jumplist.rs

1//! Neovim jumplist utilities for accessing jump history.
2
3use nvim_oxi::Array;
4use nvim_oxi::Object;
5use nvim_oxi::conversion::FromObject;
6use nvim_oxi::lua::Poppable;
7use nvim_oxi::lua::ffi::State;
8use serde::Deserialize;
9
10/// Represents a single entry in Neovim's jumplist.
11#[derive(Clone, Debug, Deserialize)]
12pub struct JumpEntry {
13    pub bufnr: i32,
14    pub col: i32,
15    pub coladd: i32,
16    pub lnum: i32,
17}
18
19impl FromObject for JumpList {
20    fn from_object(obj: Object) -> Result<Self, nvim_oxi::conversion::Error> {
21        Self::deserialize(nvim_oxi::serde::Deserializer::new(obj)).map_err(Into::into)
22    }
23}
24
25impl Poppable for JumpList {
26    unsafe fn pop(lstate: *mut State) -> Result<Self, nvim_oxi::lua::Error> {
27        // SAFETY: The caller (nvim-oxi framework) guarantees that:
28        // 1. `lstate` is a valid pointer to an initialized Lua state
29        // 2. The Lua stack has at least one value to pop
30        unsafe {
31            let obj = Object::pop(lstate)?;
32            Self::from_object(obj).map_err(nvim_oxi::lua::Error::pop_error_from_err::<Self, _>)
33        }
34    }
35}
36
37/// Internal representation of Neovim's jumplist structure.
38#[derive(Debug, Deserialize)]
39#[allow(dead_code)]
40struct JumpList(Vec<JumpEntry>, usize);
41
42/// Retrieves the current jumplist from Neovim.
43pub fn get() -> Option<Vec<JumpEntry>> {
44    Some(
45        nvim_oxi::api::call_function::<_, JumpList>("getjumplist", Array::new())
46            .inspect_err(|err| crate::notify::error(format!("error getting jumplist | error={err:?}")))
47            .ok()?
48            .0,
49    )
50}