nvim_utils/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! # Utilities for building Neovim plugins in Rust<br>
3//! The intention of `nvim-utils` is to allow Rust plugin authors to interact with Neovim in Rust just as easily as in Lua,<br>
4//! by removing as much of the required boilerplate as possible.
5//!
6//! It provides utilities for:
7//! - Declaratively building lua modules using `mlua`
8//! - Interacting with Neovim's lua api
9//! - Logging using `vim.notify`
10//! - Accessing common lua builtin functions like `require` and `print`
11//! - And more to come!
12//!
13//! #### Features
14//! - `builder` enables the [`builder`] module, containing [`ModuleBuilder`](struct@builder) (enabled by default)
15//! - `vim` enables the [`vim`] module (enabled by default)
16//! - `async` enables async functions in [`builder::ModuleBuilder`], and the `async` feature in mlua (disabled by default)
17//! - `send` enables the `send` feature for [`mlua`], which enables `Send` for lua types (disabled by default)
18//! - `unstable` includes unstable / untested API features (disabled by default)
19
20/// Includes [`mlua::prelude`], [`vim`], [`vim::ext::log`], and [`builder::ModuleBuilder`] if the corresponding features are enabled
21pub mod prelude {
22    #[cfg(feature = "vim")]
23    #[cfg_attr(docsrs, doc(cfg(feature = "vim")))]
24    pub use crate::vim;
25
26    #[cfg(feature = "vim")]
27    #[cfg_attr(docsrs, doc(cfg(feature = "vim")))]
28    pub use crate::vim::ext::log;
29
30    #[cfg(feature = "builder")]
31    #[cfg_attr(docsrs, doc(cfg(feature = "builder")))]
32    pub use crate::builder::ModuleBuilder;
33
34    pub use mlua::serde::{Deserializer, LuaSerdeExt, Serializer};
35
36    pub use mlua::lua_module;
37    pub use mlua::prelude::*;
38}
39
40#[allow(unused_imports)]
41#[macro_use]
42extern crate nvim_utils_macros;
43pub use nvim_utils_macros::module;
44
45#[cfg(feature = "builder")]
46#[cfg_attr(docsrs, doc(cfg(feature = "builder")))]
47pub mod builder;
48#[cfg(feature = "vim")]
49#[cfg_attr(docsrs, doc(cfg(feature = "vim")))]
50pub mod vim;
51
52use prelude::*;
53
54// TODO: figure out how to cache the result of gets for commonly used functions like require so they don't need to be repeatedly fetched
55
56/// Gets the global `require` function and calls it with the given name as an argument
57pub fn require<'a, T: FromLua<'a> + Clone>(lua: &'a Lua, name: &'a str) -> LuaResult<T> {
58    lua.globals()
59        .get::<_, LuaFunction<'a>>("require")?
60        .call(name)
61}
62
63/// Gets the global `print` function and calls it with the given message as an argument
64pub fn print(lua: &Lua, msg: String) -> LuaResult<()> {
65    lua.globals()
66        .get::<_, LuaFunction>("print")?
67        .call::<_, ()>(msg)
68}