Skip to content
This repository was archived by the owner on Jul 13, 2023. It is now read-only.

Commit 6b8b758

Browse files
authored
Macros (#8)
* Module proc-macro with name / path argument
1 parent 1bdb204 commit 6b8b758

6 files changed

Lines changed: 221 additions & 3 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mlua = { version = "0.8.7", features = [
3333
"serialize",
3434
] }
3535
serde = { version = "1.0.152", features = ["derive"] }
36+
nvim-utils-macros = { path = "macros" }
3637

3738
[dev-dependencies]
3839
fs_extra = "1.3.0"

macros/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
/Cargo.lock

macros/Cargo.lock

Lines changed: 136 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

macros/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "nvim-utils-macros"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]
10+
mlua = { version = "0.8.7", features = ["luajit", "vendored"] }
11+
proc-macro2 = "1.0.51"
12+
quote = "1.0.23"
13+
syn = { version = "1.0.109", features = ["full"] }

macros/src/lib.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#[allow(unused_imports)]
2+
use mlua::lua_State;
3+
use proc_macro::TokenStream;
4+
use proc_macro2::Span;
5+
use quote::quote;
6+
use syn::{AttributeArgs, Error, Path, Result};
7+
8+
struct Plugin {
9+
path: Path,
10+
}
11+
12+
impl Plugin {
13+
fn parse(args: AttributeArgs) -> Result<Self> {
14+
let mut path = None;
15+
16+
for arg in args {
17+
use syn::Meta::Path;
18+
use syn::NestedMeta::*;
19+
match arg {
20+
Meta(Path(p)) => path = Some(p),
21+
_ => {
22+
return Err(Error::new_spanned(arg, "expected `name = \"...\"`"));
23+
}
24+
}
25+
}
26+
27+
let path = path.ok_or_else(|| Error::new(Span::call_site(), "expected module path"))?;
28+
29+
Ok(Self { path })
30+
}
31+
}
32+
33+
#[proc_macro_attribute]
34+
pub fn module(attr: TokenStream, item: TokenStream) -> TokenStream {
35+
let attr = syn::parse_macro_input!(attr as AttributeArgs);
36+
let plugin = match Plugin::parse(attr) {
37+
Ok(plugin) => plugin,
38+
Err(err) => return err.to_compile_error().into(),
39+
};
40+
41+
let path = plugin
42+
.path
43+
.segments
44+
.iter()
45+
.map(|s| s.ident.to_string())
46+
.collect::<Vec<_>>()
47+
.join("_");
48+
49+
let func = syn::parse_macro_input!(item as syn::ItemFn);
50+
let name = func.sig.ident.to_string();
51+
52+
let entry = format!("luaopen_{}", path);
53+
let wrapped = quote! {
54+
#func
55+
56+
#[no_mangle]
57+
unsafe extern "C" fn #entry(state: *mut lua_State) -> std::os::raw::c_int {
58+
mlua::Lua::init_from_ptr(state)
59+
.entrypoint1(#name)
60+
.expect("failed to register module")
61+
}
62+
};
63+
64+
wrapped.into()
65+
}

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
//! - `unstable` includes unstable / untested API features (disabled by default)
1919
2020
/// Includes [`mlua::prelude`], [`vim`], [`vim::ext::log`], and [`builder::ModuleBuilder`] if the corresponding features are enabled
21-
/// Also includes [`mlua::serde`] if the `serde` feature is enabled
2221
pub mod prelude {
2322
#[cfg(feature = "vim")]
2423
#[cfg_attr(docsrs, doc(cfg(feature = "vim")))]
@@ -32,14 +31,16 @@ pub mod prelude {
3231
#[cfg_attr(docsrs, doc(cfg(feature = "builder")))]
3332
pub use crate::builder::ModuleBuilder;
3433

35-
#[cfg(feature = "serde")]
36-
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
3734
pub use mlua::serde::{Deserializer, LuaSerdeExt, Serializer};
3835

3936
pub use mlua::lua_module;
4037
pub use mlua::prelude::*;
4138
}
4239

40+
#[allow(unused_imports)]
41+
#[macro_use]
42+
extern crate nvim_utils_macros;
43+
4344
#[cfg(feature = "builder")]
4445
#[cfg_attr(docsrs, doc(cfg(feature = "builder")))]
4546
pub mod builder;

0 commit comments

Comments
 (0)