noib3 / nvim-oxi

:link: Rust bindings to all things Neovim
https://crates.io/crates/nvim-oxi
MIT License
902 stars 45 forks source link

Use LuaFunction as `rhs` for `buffer_keymap_set` that should capture local variable #140

Closed patata3000 closed 8 months ago

patata3000 commented 8 months ago

Hey there!

I've got this piece of code that doesn't work:

    let table_obj: LuaTable = build_table_output(buffer.clone(), buffer_content)?;
    let my_function = Function::from_fn({
        move |()| {
            print!("{:?}", table_obj.call_method("get_cell", ())?);
            Ok::<(), oxi::Error>(())
        }
    });
    buffer.set_keymap(Mode::Normal, "p", my_function, &keymap_opts)?;  // FORBIDDEN

I couldn't find a way to pass a lambda LuaFunction to a set_keymap.

What I could do is to define a LuaFunction in some module -> my_module => my_module.my_function and make

    buffer.set_keymap(Mode::Normal, "p", "<cmd>lua require'my_module.my_function()'<cr>", &keymap_opts)?;

There is still a problem though. I want it to either:

If it's possible, I can't find a way to do this. If it's not possible, I may be using a bad pattern but I don't know how to solve this problem.

Do you have an explanation? Or any hint?

noib3 commented 8 months ago

You can leave the rhs empty and pass the closure to the callback method of your keymap_opts builder.

patata3000 commented 8 months ago

I love you.