nvim_oxi_luajit/
macros.rs

1use core::ffi::c_char;
2
3use crate::ffi::*;
4
5// Taken from https://github.com/khvzak/mlua/blob/master/src/macros.rs#L11
6#[macro_export]
7macro_rules! cstr {
8    ($s:expr) => {
9        concat!($s, "\0") as *const str as *const [::std::ffi::c_char]
10            as *const ::std::ffi::c_char
11    };
12}
13
14pub use cstr;
15
16macro_rules! count {
17    () => {0i32};
18    ($x:tt $($xs:tt)*) => {1i32 + count!($($xs)*)};
19}
20
21pub(crate) use count;
22
23/// Same as [`std::dbg!`](dbg) but writes to the Neovim message area instead of
24/// stdout.
25///
26/// [dbg]: https://doc.rust-lang.org/std/macro.dbg.html
27#[macro_export]
28macro_rules! dbg {
29    () => {
30        $crate::print!("[{}:{}]", ::core::file!(), ::core::line!())
31    };
32    ($val:expr $(,)?) => {
33        match $val {
34            tmp => {
35                $crate::print!("[{}:{}] {} = {:#?}",
36                    ::core::file!(), ::core::line!(), ::core::stringify!($val), &tmp);
37                tmp
38            }
39        }
40    };
41    ($($val:expr),+ $(,)?) => {
42        ($($crate::dbg!($val)),+,)
43    };
44}
45
46/// Same as [`std::print!`](print) but writes to the Neovim message area
47/// instead of stdout.
48///
49/// # Examples
50///
51/// ```ignore
52/// use nvim_oxi as nvim;
53///
54/// nvim::print!("Goodbye {}..", "Earth");
55/// nvim::print!("Hello {planet}!", planet = "Mars");
56/// ```
57///
58/// [print]: https://doc.rust-lang.org/std/macro.print.html
59#[macro_export]
60macro_rules! print {
61    ($($arg:tt)*) => {{
62        $crate::__print(::std::fmt::format(format_args!($($arg)*)));
63    }}
64}
65
66/// Prints a message to the Neovim message area.
67#[doc(hidden)]
68pub fn __print(text: impl Into<String>) {
69    unsafe {
70        crate::with_state(move |lstate| {
71            let text = text.into();
72            lua_getglobal(lstate, cstr!("print"));
73            lua_pushlstring(
74                lstate,
75                text.as_ptr() as *const c_char,
76                text.len(),
77            );
78            lua_call(lstate, 1, 0);
79        })
80    };
81}