Giter Club home page Giter Club logo

Comments (4)

patata3000 avatar patata3000 commented on September 22, 2024 1

Ok so after searching a couple hours, I understood that I was supposed to use mlua. Here is a small example :

// You must add `mlua` as a dependency in your Cargo.toml file.
use mlua::prelude::{LuaFunction, LuaTable};

use nvim_oxi::{self as oxi, Dictionary, Function};

// I don't understand yet what is this `_none` parameter.
// I added it and it's working but I guess I'll need to understand why at some point.
fn mlua_test(_none: ()) -> oxi::Result<()> {
    // You must also add feature `mlua` for nvim_oxi to your `Cargo.toml` as well.
    let lua = oxi::mlua::lua();

    // `lua.globals()` returns a reference to the global table of the Lua state.
    // `LuaFunction` is the type of what you `get` from the global table.
    // In this case it's a Lua function.
    let require = lua.globals().get::<_, LuaFunction>("require")?;

    // You can call this fuction like this to get the `telescope.builtin` table.
    let telescope_builtin: LuaTable = require.call("telescope.builtin")?;

    // I think that you get the point now.
    let find_files = telescope_builtin.get::<_, LuaFunction>("find_files")?;
    find_files.call(())?;

    Ok(())
}

#[oxi::module]
pub fn my_module() -> oxi::Result<Dictionary> {
    let mlua_test = Function::from_fn(mlua_test);
    Ok(Dictionary::from_iter([("mlua_test", mlua_test)]))
}

After installing it, you can do:

nvim -c "lua require'my_module'.mlua_test()"

I guess that you can go there for more in depth mlua usage -> https://github.com/mlua-rs/mlua

from nvim-oxi.

noib3 avatar noib3 commented on September 22, 2024 1

// I don't understand yet what is this _none parameter.

You need to add it because Function::from_fn() accepts a closure (or a function in this case) whose argument implements Poppable. If your function doesn't take any arguments you can just use (), just like you did.

from nvim-oxi.

patata3000 avatar patata3000 commented on September 22, 2024

Hello ! I've got the same question. 😄

Moreover I think that it would be a good example.

from nvim-oxi.

noib3 avatar noib3 commented on September 22, 2024

I'll close this as completed but feel free to re-open it if you still have questions.

from nvim-oxi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.