From 3ff136338b2e569b893601fb1a2808fa719e5de5 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Tue, 1 Oct 2024 18:25:57 +0800 Subject: [PATCH 01/11] lua: make FFI types CamelCase --- crates/api/src/buffer.rs | 4 +- crates/api/src/ffi/buffer.rs | 4 +- crates/api/src/tabpage.rs | 4 +- crates/api/src/types/autocmd_callback_args.rs | 2 +- crates/api/src/types/command_args.rs | 2 +- crates/api/src/window.rs | 4 +- crates/libuv/src/ffi.rs | 2 +- crates/libuv/src/loop.rs | 4 +- crates/luajit/src/error.rs | 2 +- crates/luajit/src/ffi.rs | 86 +++++++++---------- crates/luajit/src/function.rs | 6 +- crates/luajit/src/poppable.rs | 36 ++++---- crates/luajit/src/pushable.rs | 72 +++++----------- crates/luajit/src/state.rs | 8 +- crates/luajit/src/utils.rs | 17 ++-- crates/macros/src/plugin.rs | 2 +- crates/types/src/array.rs | 6 +- crates/types/src/dictionary.rs | 6 +- crates/types/src/function.rs | 6 +- crates/types/src/object.rs | 8 +- crates/types/src/string.rs | 6 +- examples/mechanic.rs | 6 +- src/entrypoint.rs | 7 +- 23 files changed, 125 insertions(+), 175 deletions(-) diff --git a/crates/api/src/buffer.rs b/crates/api/src/buffer.rs index b09fc419..455617ba 100644 --- a/crates/api/src/buffer.rs +++ b/crates/api/src/buffer.rs @@ -71,7 +71,7 @@ impl FromObject for Buffer { impl Poppable for Buffer { unsafe fn pop( - lstate: *mut lua::ffi::lua_State, + lstate: *mut lua::ffi::State, ) -> std::result::Result { BufHandle::pop(lstate).map(Into::into) } @@ -80,7 +80,7 @@ impl Poppable for Buffer { impl Pushable for Buffer { unsafe fn push( self, - lstate: *mut lua::ffi::lua_State, + lstate: *mut lua::ffi::State, ) -> std::result::Result { self.0.push(lstate) } diff --git a/crates/api/src/ffi/buffer.rs b/crates/api/src/ffi/buffer.rs index 27d97171..106b0a66 100644 --- a/crates/api/src/ffi/buffer.rs +++ b/crates/api/src/ffi/buffer.rs @@ -83,7 +83,7 @@ extern "C" { strict_indexing: bool, #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly. arena: *mut Arena, - lstate: *mut luajit::ffi::lua_State, + lstate: *mut luajit::ffi::State, err: *mut Error, ) -> Array; @@ -124,7 +124,7 @@ extern "C" { opts: *const GetTextOpts, #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly. arena: *mut Arena, - lstate: *mut luajit::ffi::lua_State, + lstate: *mut luajit::ffi::State, err: *mut Error, ) -> Array; diff --git a/crates/api/src/tabpage.rs b/crates/api/src/tabpage.rs index 3ab57824..cd675fc6 100644 --- a/crates/api/src/tabpage.rs +++ b/crates/api/src/tabpage.rs @@ -46,7 +46,7 @@ impl From for Object { impl Poppable for TabPage { unsafe fn pop( - lstate: *mut lua::ffi::lua_State, + lstate: *mut lua::ffi::State, ) -> std::result::Result { TabHandle::pop(lstate).map(Into::into) } @@ -55,7 +55,7 @@ impl Poppable for TabPage { impl Pushable for TabPage { unsafe fn push( self, - lstate: *mut lua::ffi::lua_State, + lstate: *mut lua::ffi::State, ) -> std::result::Result { self.0.push(lstate) } diff --git a/crates/api/src/types/autocmd_callback_args.rs b/crates/api/src/types/autocmd_callback_args.rs index 95b9fa19..66798110 100644 --- a/crates/api/src/types/autocmd_callback_args.rs +++ b/crates/api/src/types/autocmd_callback_args.rs @@ -47,7 +47,7 @@ impl FromObject for AutocmdCallbackArgs { impl luajit::Poppable for AutocmdCallbackArgs { unsafe fn pop( - lstate: *mut luajit::ffi::lua_State, + lstate: *mut luajit::ffi::State, ) -> Result { let obj = Object::pop(lstate)?; diff --git a/crates/api/src/types/command_args.rs b/crates/api/src/types/command_args.rs index 7bc663f2..d292e5c3 100644 --- a/crates/api/src/types/command_args.rs +++ b/crates/api/src/types/command_args.rs @@ -58,7 +58,7 @@ impl FromObject for CommandArgs { impl luajit::Poppable for CommandArgs { unsafe fn pop( - lstate: *mut luajit::ffi::lua_State, + lstate: *mut luajit::ffi::State, ) -> Result { let obj = Object::pop(lstate)?; diff --git a/crates/api/src/window.rs b/crates/api/src/window.rs index e33eb5af..d926a656 100644 --- a/crates/api/src/window.rs +++ b/crates/api/src/window.rs @@ -65,7 +65,7 @@ impl FromObject for Window { impl Poppable for Window { unsafe fn pop( - lstate: *mut lua::ffi::lua_State, + lstate: *mut lua::ffi::State, ) -> std::result::Result { WinHandle::pop(lstate).map(Into::into) } @@ -74,7 +74,7 @@ impl Poppable for Window { impl Pushable for Window { unsafe fn push( self, - lstate: *mut lua::ffi::lua_State, + lstate: *mut lua::ffi::State, ) -> std::result::Result { self.0.push(lstate) } diff --git a/crates/libuv/src/ffi.rs b/crates/libuv/src/ffi.rs index bea2ff41..47ee19c0 100644 --- a/crates/libuv/src/ffi.rs +++ b/crates/libuv/src/ffi.rs @@ -42,7 +42,7 @@ impl crate::ProperLayout for uv_timer_t {} extern "C" { // https://github.com/luvit/luv/blob/master/src/luv.c#L751 pub(crate) fn luv_loop( - lua_state: *mut luajit::ffi::lua_State, + lua_state: *mut luajit::ffi::State, ) -> *mut uv_loop_t; pub(crate) fn uv_async_init( diff --git a/crates/libuv/src/loop.rs b/crates/libuv/src/loop.rs index 745a5fbd..201fddf9 100644 --- a/crates/libuv/src/loop.rs +++ b/crates/libuv/src/loop.rs @@ -1,6 +1,6 @@ use core::cell::OnceCell; -use luajit::ffi::lua_State; +use luajit::ffi::State; use crate::ffi; @@ -13,7 +13,7 @@ thread_local! { /// NOTE: this function **must** be called before calling any other function /// exposed by this crate or there will be segfaults. #[doc(hidden)] -pub unsafe fn init(lua_state: *mut lua_State) { +pub unsafe fn init(lua_state: *mut State) { LOOP.with(|uv_loop| uv_loop.set(ffi::luv_loop(lua_state))) .unwrap_unchecked(); } diff --git a/crates/luajit/src/error.rs b/crates/luajit/src/error.rs index f4ea6d83..fd2c30d6 100644 --- a/crates/luajit/src/error.rs +++ b/crates/luajit/src/error.rs @@ -54,7 +54,7 @@ impl Error { } pub unsafe fn pop_wrong_type_at_idx( - lstate: *mut crate::ffi::lua_State, + lstate: *mut crate::ffi::State, idx: std::ffi::c_int, ) -> Self { let expected = std::any::type_name::(); diff --git a/crates/luajit/src/ffi.rs b/crates/luajit/src/ffi.rs index 999a313c..af3c147a 100644 --- a/crates/luajit/src/ffi.rs +++ b/crates/luajit/src/ffi.rs @@ -1,12 +1,10 @@ #![allow(non_snake_case)] -#![allow(non_camel_case_types)] use std::ffi::{c_char, c_double, c_int, c_void}; use std::marker::{PhantomData, PhantomPinned}; #[repr(C)] -#[doc(hidden)] -pub struct lua_State { +pub struct State { _data: [u8; 0], /// This marker ensures the struct is not `Send`, `Sync` and `Unpin` (the @@ -43,13 +41,13 @@ pub const LUA_TUSERDATA: c_int = 7; pub const LUA_TTHREAD: c_int = 8; // https://www.lua.org/manual/5.1/manual.html#lua_CFunction -pub type lua_CFunction = unsafe extern "C" fn(L: *mut lua_State) -> c_int; +pub type CFunction = unsafe extern "C" fn(L: *mut State) -> c_int; // https://www.lua.org/manual/5.1/manual.html#lua_Integer -pub type lua_Integer = isize; +pub type Integer = isize; // https://www.lua.org/manual/5.1/manual.html#lua_Number -pub type lua_Number = c_double; +pub type Number = c_double; #[cfg_attr( all(target_os = "windows", target_env = "msvc"), @@ -57,7 +55,7 @@ pub type lua_Number = c_double; )] extern "C" { // https://www.lua.org/manual/5.1/manual.html#lua_call - pub fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int); + pub fn lua_call(L: *mut State, nargs: c_int, nresults: c_int); /// Binding to [`lua_createtable()`] (-0, +1). /// @@ -70,134 +68,134 @@ extern "C" { /// `lua_newtable`. /// /// [`lua_createtable`]: https://www.lua.org/manual/5.1/manual.html#lua_createtable - pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int); + pub fn lua_createtable(L: *mut State, narr: c_int, nrec: c_int); // https://www.lua.org/manual/5.1/manual.html#lua_error - pub fn lua_error(L: *mut lua_State) -> !; + pub fn lua_error(L: *mut State) -> !; // https://www.lua.org/manual/5.1/manual.html#lua_call - pub fn lua_getfield(L: *mut lua_State, index: c_int, k: *const c_char); + pub fn lua_getfield(L: *mut State, index: c_int, k: *const c_char); // https://www.lua.org/manual/5.1/manual.html#lua_getmetatable - pub fn lua_getmetatable(L: *mut lua_State, index: c_int) -> c_int; + pub fn lua_getmetatable(L: *mut State, index: c_int) -> c_int; // https://www.lua.org/manual/5.1/manual.html#lua_gettop - pub fn lua_gettop(L: *mut lua_State) -> c_int; + pub fn lua_gettop(L: *mut State) -> c_int; // https://www.lua.org/manual/5.1/manual.html#lua_newuserdata - pub fn lua_newuserdata(L: *mut lua_State, size: usize) -> *mut c_void; + pub fn lua_newuserdata(L: *mut State, size: usize) -> *mut c_void; // https://www.lua.org/manual/5.1/manual.html#lua_next - pub fn lua_next(L: *mut lua_State, index: c_int) -> c_int; + pub fn lua_next(L: *mut State, index: c_int) -> c_int; // https://www.lua.org/manual/5.1/manual.html#lua_objlen - pub fn lua_objlen(L: *mut lua_State, index: c_int) -> usize; + pub fn lua_objlen(L: *mut State, index: c_int) -> usize; // https://www.lua.org/manual/5.1/manual.html#lua_pcall pub fn lua_pcall( - L: *mut lua_State, + L: *mut State, nargs: c_int, nresults: c_int, errorfunc: c_int, ) -> c_int; // https://www.lua.org/manual/5.1/manual.html#lua_pushinteger - pub fn lua_pushboolean(L: *mut lua_State, n: lua_Integer); + pub fn lua_pushboolean(L: *mut State, n: Integer); // https://www.lua.org/manual/5.1/manual.html#lua_pushcclosure - pub fn lua_pushcclosure(L: *mut lua_State, r#fn: lua_CFunction, n: c_int); + pub fn lua_pushcclosure(L: *mut State, r#fn: CFunction, n: c_int); // https://www.lua.org/manual/5.1/manual.html#lua_pushinteger - pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer); + pub fn lua_pushinteger(L: *mut State, n: Integer); // https://www.lua.org/manual/5.1/manual.html#lua_pushlightuserdata - pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void); + pub fn lua_pushlightuserdata(L: *mut State, p: *mut c_void); // https://www.lua.org/manual/5.1/manual.html#lua_pushlstring - pub fn lua_pushlstring(L: *mut lua_State, s: *const c_char, len: usize); + pub fn lua_pushlstring(L: *mut State, s: *const c_char, len: usize); // https://www.lua.org/manual/5.1/manual.html#lua_pushnil - pub fn lua_pushnil(L: *mut lua_State); + pub fn lua_pushnil(L: *mut State); // https://www.lua.org/manual/5.1/manual.html#lua_pushnumber - pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number); + pub fn lua_pushnumber(L: *mut State, n: Number); // https://www.lua.org/manual/5.1/manual.html#lua_pushstring - pub fn lua_pushstring(L: *mut lua_State, s: *const c_char); + pub fn lua_pushstring(L: *mut State, s: *const c_char); // https://www.lua.org/manual/5.1/manual.html#lua_pushvalue - pub fn lua_pushvalue(L: *mut lua_State, index: c_int); + pub fn lua_pushvalue(L: *mut State, index: c_int); // https://www.lua.org/manual/5.1/manual.html#lua_rawgeti - pub fn lua_rawgeti(L: *mut lua_State, index: c_int, n: c_int); + pub fn lua_rawgeti(L: *mut State, index: c_int, n: c_int); // https://www.lua.org/manual/5.1/manual.html#lua_rawset - pub fn lua_rawset(L: *mut lua_State, index: c_int); + pub fn lua_rawset(L: *mut State, index: c_int); // https://www.lua.org/manual/5.1/manual.html#lua_rawseti - pub fn lua_rawseti(L: *mut lua_State, index: c_int, n: c_int); + pub fn lua_rawseti(L: *mut State, index: c_int, n: c_int); // https://www.lua.org/manual/5.1/manual.html#lua_settop - pub fn lua_settop(L: *mut lua_State, index: c_int); + pub fn lua_settop(L: *mut State, index: c_int); // https://www.lua.org/manual/5.1/manual.html#lua_toboolean - pub fn lua_toboolean(L: *mut lua_State, index: c_int) -> c_int; + pub fn lua_toboolean(L: *mut State, index: c_int) -> c_int; // https://www.lua.org/manual/5.1/manual.html#lua_tointeger - pub fn lua_tointeger(L: *mut lua_State, index: c_int) -> lua_Integer; + pub fn lua_tointeger(L: *mut State, index: c_int) -> Integer; // https://www.lua.org/manual/5.1/manual.html#lua_tolstring pub fn lua_tolstring( - L: *mut lua_State, + L: *mut State, index: c_int, len: *mut usize, ) -> *const c_char; // https://www.lua.org/manual/5.1/manual.html#lua_tonumber - pub fn lua_tonumber(L: *mut lua_State, index: c_int) -> lua_Number; + pub fn lua_tonumber(L: *mut State, index: c_int) -> Number; // https://www.lua.org/manual/5.1/manual.html#lua_touserdata - pub fn lua_touserdata(L: *mut lua_State, index: c_int) -> *mut c_void; + pub fn lua_touserdata(L: *mut State, index: c_int) -> *mut c_void; // https://www.lua.org/manual/5.1/manual.html#lua_type - pub fn lua_type(L: *mut lua_State, index: c_int) -> c_int; + pub fn lua_type(L: *mut State, index: c_int) -> c_int; // https://www.lua.org/manual/5.1/manual.html#lua_typename - pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char; + pub fn lua_typename(L: *mut State, tp: c_int) -> *const c_char; // Lua auxiliary library. // https://www.lua.org/manual/5.1/manual.html#luaL_error - pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> !; + pub fn luaL_error(L: *mut State, fmt: *const c_char, ...) -> !; // https://www.lua.org/manual/5.1/manual.html#luaL_ref - pub fn luaL_ref(L: *mut lua_State, t: c_int) -> c_int; + pub fn luaL_ref(L: *mut State, t: c_int) -> c_int; // https://www.lua.org/manual/5.1/manual.html#luaL_unref - pub fn luaL_unref(L: *mut lua_State, t: c_int, r#ref: c_int); + pub fn luaL_unref(L: *mut State, t: c_int, r#ref: c_int); } // https://www.lua.org/manual/5.1/manual.html#lua_getglobal -pub unsafe fn lua_getglobal(L: *mut lua_State, name: *const c_char) { +pub unsafe fn lua_getglobal(L: *mut State, name: *const c_char) { lua_getfield(L, LUA_GLOBALSINDEX, name) } // https://www.lua.org/manual/5.1/manual.html#lua_pop -pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) { +pub unsafe fn lua_pop(L: *mut State, n: c_int) { lua_settop(L, -n - 1) } // https://www.lua.org/manual/5.1/manual.html#lua_pushcfunction -pub unsafe fn lua_pushcfunction(L: *mut lua_State, r#fn: lua_CFunction) { +pub unsafe fn lua_pushcfunction(L: *mut State, r#fn: CFunction) { lua_pushcclosure(L, r#fn, 0) } // https://www.lua.org/manual/5.1/manual.html#lua_tostring -pub unsafe fn lua_tostring(L: *mut lua_State, index: c_int) -> *const c_char { +pub unsafe fn lua_tostring(L: *mut State, index: c_int) -> *const c_char { lua_tolstring(L, index, std::ptr::null_mut()) } // https://www.lua.org/manual/5.1/manual.html#luaL_typename -pub unsafe fn luaL_typename(L: *mut lua_State, index: c_int) -> *const c_char { +pub unsafe fn luaL_typename(L: *mut State, index: c_int) -> *const c_char { lua_typename(L, lua_type(L, index)) } diff --git a/crates/luajit/src/function.rs b/crates/luajit/src/function.rs index 8a5becac..408b4616 100644 --- a/crates/luajit/src/function.rs +++ b/crates/luajit/src/function.rs @@ -3,7 +3,7 @@ use std::ffi::{c_int, CStr}; use std::mem; use std::ptr; -use crate::ffi::{self, lua_State}; +use crate::ffi::{self, State}; use crate::{utils, IntoResult, Poppable, Pushable}; /// Stores a function in the Lua registry, returning its ref. @@ -16,9 +16,9 @@ where R::Error: Error + 'static, { type Callback = - Box Result + 'static>; + Box Result + 'static>; - unsafe extern "C" fn c_fun(lstate: *mut lua_State) -> c_int { + unsafe extern "C" fn c_fun(lstate: *mut State) -> c_int { let fun = { let idx = ffi::lua_upvalueindex(1); let upv = ffi::lua_touserdata(lstate, idx) as *mut Callback; diff --git a/crates/luajit/src/poppable.rs b/crates/luajit/src/poppable.rs index 6b72a7f5..cdb895db 100644 --- a/crates/luajit/src/poppable.rs +++ b/crates/luajit/src/poppable.rs @@ -8,12 +8,12 @@ use crate::Error; /// Trait implemented for types that can be popped off the Lua stack. pub trait Poppable: Sized { /// Pops the value at the top of the stack. - unsafe fn pop(lua_state: *mut lua_State) -> Result; + unsafe fn pop(lua_state: *mut State) -> Result; } impl Poppable for () { #[inline(always)] - unsafe fn pop(state: *mut lua_State) -> Result { + unsafe fn pop(state: *mut State) -> Result { if lua_gettop(state) == 0 { Ok(()) } else if lua_type(state, -1) == LUA_TNIL { @@ -26,7 +26,7 @@ impl Poppable for () { } impl Poppable for bool { - unsafe fn pop(state: *mut lua_State) -> Result { + unsafe fn pop(state: *mut State) -> Result { if lua_gettop(state) == 0 { return Err(Error::PopEmptyStack); } @@ -42,8 +42,8 @@ impl Poppable for bool { } } -impl Poppable for lua_Integer { - unsafe fn pop(state: *mut lua_State) -> Result { +impl Poppable for Integer { + unsafe fn pop(state: *mut State) -> Result { if lua_gettop(state) == 0 { return Err(Error::PopEmptyStack); } @@ -60,14 +60,12 @@ impl Poppable for lua_Integer { } /// Implements `Poppable` for a integer types that implement -/// `TryFrom`. +/// `TryFrom`. macro_rules! pop_try_from_integer { ($integer:ty) => { impl Poppable for $integer { - unsafe fn pop( - lstate: *mut lua_State, - ) -> Result { - lua_Integer::pop(lstate)? + unsafe fn pop(lstate: *mut State) -> Result { + Integer::pop(lstate)? .try_into() .map_err(Error::pop_error_from_err::) } @@ -85,8 +83,8 @@ pop_try_from_integer!(i64); pop_try_from_integer!(u64); pop_try_from_integer!(usize); -impl Poppable for lua_Number { - unsafe fn pop(state: *mut lua_State) -> Result { +impl Poppable for Number { + unsafe fn pop(state: *mut State) -> Result { if lua_gettop(state) == 0 { return Err(Error::PopEmptyStack); } @@ -103,13 +101,13 @@ impl Poppable for lua_Number { } impl Poppable for f32 { - unsafe fn pop(state: *mut lua_State) -> Result { - lua_Number::pop(state).map(|n| n as f32) + unsafe fn pop(state: *mut State) -> Result { + Number::pop(state).map(|n| n as f32) } } impl Poppable for String { - unsafe fn pop(state: *mut lua_State) -> Result { + unsafe fn pop(state: *mut State) -> Result { if lua_gettop(state) == 0 { return Err(Error::PopEmptyStack); } @@ -139,7 +137,7 @@ impl Poppable for Option where T: Poppable, { - unsafe fn pop(state: *mut lua_State) -> Result { + unsafe fn pop(state: *mut State) -> Result { if lua_gettop(state) == 0 { return Ok(None); } @@ -158,7 +156,7 @@ impl Poppable for Vec where T: Poppable, { - unsafe fn pop(state: *mut lua_State) -> Result { + unsafe fn pop(state: *mut State) -> Result { if lua_gettop(state) == 0 { return Err(Error::PopEmptyStack); } @@ -192,7 +190,7 @@ where K: Poppable + Eq + Hash, V: Poppable, { - unsafe fn pop(state: *mut lua_State) -> Result { + unsafe fn pop(state: *mut State) -> Result { if lua_gettop(state) == 0 { return Err(Error::PopEmptyStack); } @@ -239,7 +237,7 @@ macro_rules! pop_tuple { $($name: Poppable,)* { #[allow(non_snake_case)] - unsafe fn pop(state: *mut lua_State) -> Result { + unsafe fn pop(state: *mut State) -> Result { crate::utils::grow_stack(state, count!($($name)*)); pop_reverse!(state, $($name)*); Ok(($($name,)*)) diff --git a/crates/luajit/src/pushable.rs b/crates/luajit/src/pushable.rs index 0864450f..afe7bf1b 100644 --- a/crates/luajit/src/pushable.rs +++ b/crates/luajit/src/pushable.rs @@ -1,6 +1,6 @@ use std::ffi::{c_char, c_int}; -use crate::ffi::{self, lua_Integer, lua_Number, lua_State}; +use crate::ffi::{self, Integer, Number, State}; use crate::macros::count; use crate::utils; @@ -8,52 +8,40 @@ use crate::utils; pub trait Pushable { /// Pushes all its values on the Lua stack, returning the number of values /// that it pushed. - unsafe fn push( - self, - lstate: *mut lua_State, - ) -> Result; + unsafe fn push(self, lstate: *mut State) -> Result; } impl Pushable for () { - unsafe fn push( - self, - lstate: *mut lua_State, - ) -> Result { + unsafe fn push(self, lstate: *mut State) -> Result { ffi::lua_pushnil(lstate); Ok(1) } } impl Pushable for bool { - unsafe fn push( - self, - lstate: *mut lua_State, - ) -> Result { + unsafe fn push(self, lstate: *mut State) -> Result { ffi::lua_pushboolean(lstate, self as _); Ok(1) } } -impl Pushable for lua_Integer { - unsafe fn push( - self, - lstate: *mut lua_State, - ) -> Result { +impl Pushable for Integer { + unsafe fn push(self, lstate: *mut State) -> Result { ffi::lua_pushinteger(lstate, self); Ok(1) } } /// Implements `LuaPushable` for an integer type that implements -/// `Into`. +/// `Into`. macro_rules! push_into_integer { ($integer:ty) => { impl Pushable for $integer { unsafe fn push( self, - lstate: *mut lua_State, + lstate: *mut State, ) -> Result { - let n: lua_Integer = self.into(); + let n: Integer = self.into(); n.push(lstate) } } @@ -61,15 +49,15 @@ macro_rules! push_into_integer { } /// Implements `LuaPushable` for an integer type that implements -/// `TryInto`. +/// `TryInto`. macro_rules! push_try_into_integer { ($integer:ty) => { impl Pushable for $integer { unsafe fn push( self, - lstate: *mut lua_State, + lstate: *mut State, ) -> Result { - let n: lua_Integer = self.try_into().map_err( + let n: Integer = self.try_into().map_err( |err: std::num::TryFromIntError| { crate::Error::push_error( std::any::type_name::<$integer>(), @@ -93,30 +81,21 @@ push_try_into_integer!(i64); push_try_into_integer!(u64); push_try_into_integer!(usize); -impl Pushable for lua_Number { - unsafe fn push( - self, - lstate: *mut lua_State, - ) -> Result { +impl Pushable for Number { + unsafe fn push(self, lstate: *mut State) -> Result { ffi::lua_pushnumber(lstate, self); Ok(1) } } impl Pushable for f32 { - unsafe fn push( - self, - lstate: *mut lua_State, - ) -> Result { - (self as lua_Number).push(lstate) + unsafe fn push(self, lstate: *mut State) -> Result { + (self as Number).push(lstate) } } impl Pushable for String { - unsafe fn push( - self, - lstate: *mut lua_State, - ) -> Result { + unsafe fn push(self, lstate: *mut State) -> Result { ffi::lua_pushlstring( lstate, self.as_ptr() as *const c_char, @@ -130,10 +109,7 @@ impl Pushable for Option where T: Pushable, { - unsafe fn push( - self, - lstate: *mut lua_State, - ) -> Result { + unsafe fn push(self, lstate: *mut State) -> Result { match self { Some(t) => t.push(lstate), None => ().push(lstate), @@ -145,10 +121,7 @@ impl Pushable for Vec where T: Pushable, { - unsafe fn push( - self, - lstate: *mut lua_State, - ) -> Result { + unsafe fn push(self, lstate: *mut State) -> Result { ffi::lua_createtable(lstate, self.len() as _, 0); for (i, obj) in self.into_iter().enumerate() { @@ -166,10 +139,7 @@ where E: core::fmt::Display, { #[inline] - unsafe fn push( - self, - lstate: *mut lua_State, - ) -> Result { + unsafe fn push(self, lstate: *mut State) -> Result { match self { Ok(value) => value.push(lstate), Err(err) => utils::push_error(&err, lstate), @@ -187,7 +157,7 @@ macro_rules! push_tuple { #[allow(non_snake_case)] unsafe fn push( self, - lstate: *mut lua_State, + lstate: *mut State, ) -> Result { let ($($name,)*) = self; $($name.push(lstate)?;)* diff --git a/crates/luajit/src/state.rs b/crates/luajit/src/state.rs index 423be51a..78889965 100644 --- a/crates/luajit/src/state.rs +++ b/crates/luajit/src/state.rs @@ -1,16 +1,16 @@ use core::cell::OnceCell; -use crate::ffi::lua_State; +use crate::ffi::State; thread_local! { - static LUA: OnceCell<*mut lua_State> = const { OnceCell::new() }; + static LUA: OnceCell<*mut State> = const { OnceCell::new() }; } /// Initializes the Lua state. /// /// NOTE: this function **must** be called before calling any other function /// exposed by this crate or there will be segfaults. -pub unsafe fn init(lstate: *mut lua_State) { +pub unsafe fn init(lstate: *mut State) { LUA.with(|lua| lua.set(lstate).unwrap_unchecked()); } @@ -20,7 +20,7 @@ pub unsafe fn init(lstate: *mut lua_State) { /// calling [`init`]. pub unsafe fn with_state(fun: F) -> R where - F: FnOnce(*mut lua_State) -> R, + F: FnOnce(*mut State) -> R, { LUA.with(move |lstate| fun(*lstate.get().unwrap())) } diff --git a/crates/luajit/src/utils.rs b/crates/luajit/src/utils.rs index 72689c4f..e036ab3f 100644 --- a/crates/luajit/src/utils.rs +++ b/crates/luajit/src/utils.rs @@ -1,11 +1,11 @@ use std::ffi::{c_int, CStr}; use std::fmt::Display; -use crate::ffi::{self, lua_State}; +use crate::ffi::{self, State}; /// Does nothing if the stack is already taller than `n`, grows the stack /// height to `n` by adding `nil`s if it's not. -pub unsafe fn grow_stack(lstate: *mut lua_State, n: c_int) { +pub unsafe fn grow_stack(lstate: *mut State, n: c_int) { if ffi::lua_gettop(lstate) < n { ffi::lua_settop(lstate, n); } @@ -13,10 +13,7 @@ pub unsafe fn grow_stack(lstate: *mut lua_State, n: c_int) { /// Returns a displayable representation of the Lua value at a given stack /// index. -pub unsafe fn debug_value( - lstate: *mut lua_State, - n: c_int, -) -> Box { +pub unsafe fn debug_value(lstate: *mut State, n: c_int) -> Box { match ffi::lua_type(lstate, n) { ffi::LUA_TNONE | ffi::LUA_TNIL => Box::new("()"), @@ -34,7 +31,7 @@ pub unsafe fn debug_value( /// Assumes that the value at index `index` is a table and returns whether it's /// an array table (as opposed to a dictionary table). -pub unsafe fn is_table_array(lstate: *mut lua_State, index: c_int) -> bool { +pub unsafe fn is_table_array(lstate: *mut State, index: c_int) -> bool { ffi::lua_pushnil(lstate); if ffi::lua_next(lstate, index - 1) == 0 { @@ -52,12 +49,12 @@ pub unsafe fn is_table_array(lstate: *mut lua_State, index: c_int) -> bool { } /// Returns the type of the Lua value at a given stack index. -pub unsafe fn debug_type(lstate: *mut lua_State, n: c_int) -> impl Display { +pub unsafe fn debug_type(lstate: *mut State, n: c_int) -> impl Display { CStr::from_ptr(ffi::luaL_typename(lstate, n)).to_string_lossy() } /// Pretty prints the contents of the Lua stack to the Neovim message area. -pub unsafe fn debug_stack(lstate: *mut lua_State) { +pub unsafe fn debug_stack(lstate: *mut State) { let height = ffi::lua_gettop(lstate); let stack_pp = (1..height + 1) @@ -75,7 +72,7 @@ pub unsafe fn debug_stack(lstate: *mut lua_State) { pub unsafe fn push_error( err: &E, - lstate: *mut lua_State, + lstate: *mut State, ) -> ! { let msg = err.to_string(); ffi::lua_pushlstring(lstate, msg.as_ptr() as *const _, msg.len()); diff --git a/crates/macros/src/plugin.rs b/crates/macros/src/plugin.rs index 1d4d2822..5e039692 100644 --- a/crates/macros/src/plugin.rs +++ b/crates/macros/src/plugin.rs @@ -24,7 +24,7 @@ pub fn plugin(attr: TokenStream, item: TokenStream) -> TokenStream { #[no_mangle] unsafe extern "C" fn #lua_module( - state: *mut #nvim_oxi::lua::ffi::lua_State, + state: *mut #nvim_oxi::lua::ffi::State, ) -> ::core::ffi::c_int { #nvim_oxi::entrypoint::entrypoint(state, #plugin_name) } diff --git a/crates/types/src/array.rs b/crates/types/src/array.rs index b59a331d..53a1f152 100644 --- a/crates/types/src/array.rs +++ b/crates/types/src/array.rs @@ -116,9 +116,7 @@ impl core::iter::FusedIterator for ArrayIterator {} impl lua::Poppable for Array { #[inline] - unsafe fn pop( - lstate: *mut lua::ffi::lua_State, - ) -> Result { + unsafe fn pop(lstate: *mut lua::ffi::State) -> Result { use lua::ffi::*; if lua_gettop(lstate) == 0 { @@ -150,7 +148,7 @@ impl lua::Pushable for Array { #[inline] unsafe fn push( self, - lstate: *mut lua::ffi::lua_State, + lstate: *mut lua::ffi::State, ) -> Result { use lua::ffi::*; diff --git a/crates/types/src/dictionary.rs b/crates/types/src/dictionary.rs index b40d485c..52f9fbee 100644 --- a/crates/types/src/dictionary.rs +++ b/crates/types/src/dictionary.rs @@ -267,9 +267,7 @@ impl core::iter::FusedIterator for DictIterMut<'_> {} impl lua::Poppable for Dictionary { #[inline] - unsafe fn pop( - lstate: *mut lua::ffi::lua_State, - ) -> Result { + unsafe fn pop(lstate: *mut lua::ffi::State) -> Result { use lua::ffi::*; if lua_gettop(lstate) == 0 { @@ -306,7 +304,7 @@ impl lua::Pushable for Dictionary { #[inline] unsafe fn push( self, - lstate: *mut lua::ffi::lua_State, + lstate: *mut lua::ffi::State, ) -> Result { use lua::ffi::*; diff --git a/crates/types/src/function.rs b/crates/types/src/function.rs index e9c3d64b..68395b1b 100644 --- a/crates/types/src/function.rs +++ b/crates/types/src/function.rs @@ -41,9 +41,7 @@ where } impl Poppable for Function { - unsafe fn pop( - state: *mut lua::ffi::lua_State, - ) -> Result { + unsafe fn pop(state: *mut lua::ffi::State) -> Result { if ffi::lua_gettop(state) == 0 { return Err(lua::Error::PopEmptyStack); } @@ -66,7 +64,7 @@ impl Poppable for Function { impl Pushable for Function { unsafe fn push( self, - state: *mut lua::ffi::lua_State, + state: *mut lua::ffi::State, ) -> Result { ffi::lua_rawgeti(state, ffi::LUA_REGISTRYINDEX, self.lua_ref); Ok(1) diff --git a/crates/types/src/object.rs b/crates/types/src/object.rs index ac7f09b6..b77088aa 100644 --- a/crates/types/src/object.rs +++ b/crates/types/src/object.rs @@ -462,7 +462,7 @@ where } impl Pushable for Object { - unsafe fn push(self, lstate: *mut lua_State) -> Result { + unsafe fn push(self, lstate: *mut State) -> Result { match self.kind() { ObjectKind::Nil => ().push(lstate), ObjectKind::Boolean => self.as_boolean_unchecked().push(lstate), @@ -483,7 +483,7 @@ impl Pushable for Object { } impl Poppable for Object { - unsafe fn pop(lstate: *mut lua_State) -> Result { + unsafe fn pop(lstate: *mut State) -> Result { if lua_gettop(lstate) == 0 { return Ok(Self::nil()); } @@ -494,11 +494,11 @@ impl Poppable for Object { LUA_TBOOLEAN => bool::pop(lstate).map(Into::into), LUA_TNUMBER => { - let n = lua_Number::pop(lstate)?; + let n = Number::pop(lstate)?; // This checks that the number is in the range (i32::MIN, // i32::MAX) andd that it has no fractional component. - if n == (n as c_int) as lua_Number { + if n == (n as c_int) as Number { Ok(Object::from(n as c_int)) } else { Ok(Object::from(n)) diff --git a/crates/types/src/string.rs b/crates/types/src/string.rs index 7db6103d..c4c70d09 100644 --- a/crates/types/src/string.rs +++ b/crates/types/src/string.rs @@ -237,7 +237,7 @@ impl lua::Pushable for String { #[inline] unsafe fn push( self, - lstate: *mut lua::ffi::lua_State, + lstate: *mut lua::ffi::State, ) -> Result { lua::ffi::lua_pushlstring(lstate, self.as_ptr(), self.len()); Ok(1) @@ -246,9 +246,7 @@ impl lua::Pushable for String { impl lua::Poppable for String { #[inline] - unsafe fn pop( - lstate: *mut lua::ffi::lua_State, - ) -> Result { + unsafe fn pop(lstate: *mut lua::ffi::State) -> Result { use lua::ffi::*; if lua_gettop(lstate) < 0 { diff --git a/examples/mechanic.rs b/examples/mechanic.rs index 1cb33959..1c718f91 100644 --- a/examples/mechanic.rs +++ b/examples/mechanic.rs @@ -61,9 +61,7 @@ impl ToObject for Car { } impl lua::Poppable for Car { - unsafe fn pop( - lstate: *mut lua::ffi::lua_State, - ) -> Result { + unsafe fn pop(lstate: *mut lua::ffi::State) -> Result { let obj = Object::pop(lstate)?; Self::from_object(obj) .map_err(lua::Error::pop_error_from_err::) @@ -73,7 +71,7 @@ impl lua::Poppable for Car { impl lua::Pushable for Car { unsafe fn push( self, - lstate: *mut lua::ffi::lua_State, + lstate: *mut lua::ffi::State, ) -> Result { self.to_object() .map_err(lua::Error::push_error_from_err::)? diff --git a/src/entrypoint.rs b/src/entrypoint.rs index 63a9833c..fb3b71ee 100644 --- a/src/entrypoint.rs +++ b/src/entrypoint.rs @@ -1,16 +1,13 @@ use core::ffi::c_int; -use luajit::{ffi::lua_State, Pushable}; +use luajit::{ffi::State, Pushable}; /// The entrypoint of the plugin. /// /// Initializes the Lua state, executes the entrypoint function and pushes the /// result on the stack. #[inline(always)] -pub unsafe fn entrypoint( - lua_state: *mut lua_State, - body: fn() -> T, -) -> c_int +pub unsafe fn entrypoint(lua_state: *mut State, body: fn() -> T) -> c_int where T: Pushable, { From 6a97c1589a8ae01294412e43f5c4b85251d7ed04 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Tue, 1 Oct 2024 18:31:15 +0800 Subject: [PATCH 02/11] lua: import from `core` over `std` where possible --- crates/luajit/src/error.rs | 2 +- crates/luajit/src/ffi.rs | 4 ++-- crates/luajit/src/function.rs | 8 ++++---- crates/luajit/src/macros.rs | 2 +- crates/luajit/src/poppable.rs | 2 +- crates/luajit/src/pushable.rs | 2 +- crates/luajit/src/utils.rs | 4 ++-- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/luajit/src/error.rs b/crates/luajit/src/error.rs index fd2c30d6..bc4692ba 100644 --- a/crates/luajit/src/error.rs +++ b/crates/luajit/src/error.rs @@ -1,4 +1,4 @@ -use std::ffi::c_int; +use core::ffi::c_int; use thiserror::Error as ThisError; diff --git a/crates/luajit/src/ffi.rs b/crates/luajit/src/ffi.rs index af3c147a..9f1015d8 100644 --- a/crates/luajit/src/ffi.rs +++ b/crates/luajit/src/ffi.rs @@ -1,7 +1,7 @@ #![allow(non_snake_case)] -use std::ffi::{c_char, c_double, c_int, c_void}; -use std::marker::{PhantomData, PhantomPinned}; +use core::ffi::{c_char, c_double, c_int, c_void}; +use core::marker::{PhantomData, PhantomPinned}; #[repr(C)] pub struct State { diff --git a/crates/luajit/src/function.rs b/crates/luajit/src/function.rs index 408b4616..4f0bf4be 100644 --- a/crates/luajit/src/function.rs +++ b/crates/luajit/src/function.rs @@ -1,7 +1,7 @@ -use std::error::Error; -use std::ffi::{c_int, CStr}; -use std::mem; -use std::ptr; +use core::error::Error; +use core::ffi::{c_int, CStr}; +use core::mem; +use core::ptr; use crate::ffi::{self, State}; use crate::{utils, IntoResult, Poppable, Pushable}; diff --git a/crates/luajit/src/macros.rs b/crates/luajit/src/macros.rs index b6a9e1f1..bb87b3c4 100644 --- a/crates/luajit/src/macros.rs +++ b/crates/luajit/src/macros.rs @@ -1,4 +1,4 @@ -use std::ffi::c_char; +use core::ffi::c_char; use crate::ffi::*; diff --git a/crates/luajit/src/poppable.rs b/crates/luajit/src/poppable.rs index cdb895db..e3f72de1 100644 --- a/crates/luajit/src/poppable.rs +++ b/crates/luajit/src/poppable.rs @@ -1,5 +1,5 @@ +use core::hash::Hash; use std::collections::HashMap; -use std::hash::Hash; use crate::ffi::*; use crate::macros::count; diff --git a/crates/luajit/src/pushable.rs b/crates/luajit/src/pushable.rs index afe7bf1b..71adf9fa 100644 --- a/crates/luajit/src/pushable.rs +++ b/crates/luajit/src/pushable.rs @@ -1,4 +1,4 @@ -use std::ffi::{c_char, c_int}; +use core::ffi::{c_char, c_int}; use crate::ffi::{self, Integer, Number, State}; use crate::macros::count; diff --git a/crates/luajit/src/utils.rs b/crates/luajit/src/utils.rs index e036ab3f..233879e5 100644 --- a/crates/luajit/src/utils.rs +++ b/crates/luajit/src/utils.rs @@ -1,5 +1,5 @@ -use std::ffi::{c_int, CStr}; -use std::fmt::Display; +use core::ffi::{c_int, CStr}; +use core::fmt::Display; use crate::ffi::{self, State}; From b056ecf8d1dcba0b2d6d61711df7e20d11908ac3 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Wed, 2 Oct 2024 11:16:45 +0800 Subject: [PATCH 03/11] chore: use `PanicHookInfo` over `PanicHook` --- src/tests.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 55a797bd..0bf00cbc 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -128,10 +128,10 @@ pub enum TestFailure<'a, E> { Error(E), /// This is used to indicate that the test failed due to a panic. The - /// [`PanicInfo`](std::panic::PanicInfo) contains information about the - /// panic and can be obtained by calling + /// [`PanicHookInfo`](std::panic::PanicHookInfo) contains information about + /// the panic and can be obtained by calling /// [`set_hook`](std::panic::set_hook). - Panic(&'a std::panic::PanicInfo<'a>), + Panic(&'a std::panic::PanicHookInfo<'a>), } fn exit(result: Result<(), Failure>) { @@ -339,8 +339,8 @@ impl FromStr for PanicInfo { } } -impl From<&panic::PanicInfo<'_>> for PanicInfo { - fn from(info: &panic::PanicInfo) -> Self { +impl From<&panic::PanicHookInfo<'_>> for PanicInfo { + fn from(info: &panic::PanicHookInfo) -> Self { let payload = info.payload(); let msg = downcast_display::<&str>(payload) From d56ae4d6a02fb55371c6d9e3451aa731a5fec666 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Wed, 2 Oct 2024 11:18:33 +0800 Subject: [PATCH 04/11] chore: fix clippy --- crates/api/src/trait_utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/api/src/trait_utils.rs b/crates/api/src/trait_utils.rs index a9617f4c..df1b7d8d 100644 --- a/crates/api/src/trait_utils.rs +++ b/crates/api/src/trait_utils.rs @@ -163,7 +163,7 @@ impl HlGroup for &str { let hl_id = unsafe { crate::ffi::helpers::object_to_hl_id( obj, - b"hl_group\0".as_ptr() as *const _, + c"hl_group".as_ptr() as *const _, &mut err, ) }; From 3e7ca96843cff686a48f290b4731e603ab448230 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Thu, 3 Oct 2024 14:52:25 +0800 Subject: [PATCH 05/11] api: refactor `echo()` to accept `Into`s for hl groups --- crates/api/src/vim.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/api/src/vim.rs b/crates/api/src/vim.rs index 638d030f..4674ece1 100644 --- a/crates/api/src/vim.rs +++ b/crates/api/src/vim.rs @@ -116,21 +116,22 @@ pub fn del_var(name: &str) -> Result<()> { /// Echoes a message to the Neovim message area. /// /// [1]: https://neovim.io/doc/user/api.html#nvim_echo() -pub fn echo<'hl, Text, Chunks>( +pub fn echo( chunks: Chunks, history: bool, opts: &EchoOpts, ) -> Result<()> where - Chunks: IntoIterator)>, + Chunks: IntoIterator)>, Text: Into, + HlGroup: Into, { let chunks = chunks .into_iter() .map(|(text, hlgroup)| { Array::from_iter([ Object::from(text.into()), - Object::from(hlgroup.map(|hl| hl.to_owned())), + Object::from(hlgroup.map(Into::into)), ]) }) .collect::(); From b566d7cec817583c8f220e9d3384f2ab2b04caa2 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Thu, 3 Oct 2024 14:58:43 +0800 Subject: [PATCH 06/11] types: add `String::to_str()` --- crates/types/src/string.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/types/src/string.rs b/crates/types/src/string.rs index c4c70d09..3ec6543a 100644 --- a/crates/types/src/string.rs +++ b/crates/types/src/string.rs @@ -2,6 +2,7 @@ use alloc::borrow::Cow; use alloc::string::String as StdString; +use core::str::{self, Utf8Error}; use core::{ffi, slice}; use std::path::{Path, PathBuf}; @@ -106,6 +107,12 @@ impl String { NonOwning::new(Self { ..*self }) } + /// Yields a string slice if the [`String`]'s contents are valid UTF-8. + #[inline] + pub fn to_str(&self) -> Result<&str, Utf8Error> { + str::from_utf8(self.as_bytes()) + } + /// Converts the `String` into Rust's `std::string::String`. If it already /// holds a valid UTF-8 byte sequence no allocation is made. If it doesn't /// the `String` is copied and all invalid sequences are replaced with `�`. From 101f2be5f1f426a00c12a5352caaf4d1b5565014 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Fri, 4 Oct 2024 09:19:04 +0800 Subject: [PATCH 07/11] chore: update CHANGELOG --- CHANGELOG.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32ca9257..a619b77b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,24 @@ ## [Unreleased] +### Added + +- a `String::to_str()` method which returns a `&str` if the string contains + valid UTF-8 data; + +### Changed + +- `nvim_oxi::api::echo` is now generic over the highlight group type instead of + expecting a string slice; + +- renamed the `lua_` types to ``; + ## [0.5.1] - June 23 2024 ### Added - a `handle` method on `Buffer`, `Window`, and `TabPage` which returns the - underlying handle ([#176](https://github.com/noib3/nvim-oxi/pull/176)); + underlying handle ([#176](https://github.com/noib3/nvim-oxi/pull/176)); ### Removed @@ -66,7 +78,7 @@ ## [0.4.0] - Dec 11 2023 [Unreleased]: https://github.com/noib3/nvim-oxi/compare/v0.5.1...HEAD -[0.5.0]: https://github.com/noib3/nvim-oxi/tree/v0.5.1 +[0.5.1]: https://github.com/noib3/nvim-oxi/tree/v0.5.1 [0.5.0]: https://github.com/noib3/nvim-oxi/tree/v0.5.0 [0.4.2]: https://github.com/noib3/nvim-oxi/tree/v0.4.2 [0.4.1]: https://github.com/noib3/nvim-oxi/tree/v0.4.1 From 96d95c1641fae703f3b45326bcb84017d375ca08 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Mon, 7 Oct 2024 18:12:43 +0800 Subject: [PATCH 08/11] api: add a hidden `url` field to `SetHighlightOpts` --- crates/api/src/opts/set_highlight.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/api/src/opts/set_highlight.rs b/crates/api/src/opts/set_highlight.rs index 2000e439..b5fa4c72 100644 --- a/crates/api/src/opts/set_highlight.rs +++ b/crates/api/src/opts/set_highlight.rs @@ -1,6 +1,6 @@ use types::Object; #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly. -use types::{Boolean, Integer}; +use types::{Boolean, Integer, String as NvimString}; #[cfg(feature = "neovim-0-10")] // On 0.10 and nightly. #[derive(Clone, Debug, Default, PartialEq, macros::OptsBuilder)] @@ -98,6 +98,9 @@ pub struct SetHighlightOpts { #[builder(argtype = "bool")] force: Boolean, + + #[builder(skip)] + url: NvimString, } /// Options passed to [`set_hl()`](crate::set_hl). From a614fa52fea3073839c79e1e76921f32e4a31581 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Mon, 7 Oct 2024 18:13:04 +0800 Subject: [PATCH 09/11] api: test that foreground highlights are set correctly --- tests/src/api/global.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/src/api/global.rs b/tests/src/api/global.rs index c2f2f28e..5e6ea104 100644 --- a/tests/src/api/global.rs +++ b/tests/src/api/global.rs @@ -109,6 +109,19 @@ fn get_runtime_file() { assert!(api::get_runtime_file("*", true).unwrap().next().is_some()); } +#[oxi::test] +fn hl_foreground() { + let opts = SetHighlightOpts::builder() + .foreground("#FF0000") + .strikethrough(true) + .bold(true) + .build(); + api::set_hl(0, "Header", &opts).unwrap(); + + let infos = api::get_hl_by_name("Header", true).unwrap(); + assert_eq!(infos.foreground, Some(16711680)); +} + #[oxi::test] fn hl_underline() { let opts = SetHighlightOpts::builder().underline(true).build(); From 20a793471d959d308eb6bf0c55531dde9c25e7a7 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Mon, 7 Oct 2024 18:15:39 +0800 Subject: [PATCH 10/11] chore: fix clippy --- crates/api/src/types/command_range.rs | 2 +- crates/macros/src/derive_opts.rs | 6 +++--- crates/types/src/function.rs | 2 +- crates/types/src/non_owning.rs | 6 +++--- crates/types/src/serde/de.rs | 2 +- crates/types/src/string.rs | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/api/src/types/command_range.rs b/crates/api/src/types/command_range.rs index c52b1960..caaee98a 100644 --- a/crates/api/src/types/command_range.rs +++ b/crates/api/src/types/command_range.rs @@ -35,7 +35,7 @@ impl<'de> de::Deserialize<'de> for CommandRange { { struct CommandRangeVisitor; - impl<'de> de::Visitor<'de> for CommandRangeVisitor { + impl de::Visitor<'_> for CommandRangeVisitor { type Value = CommandRange; fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/crates/macros/src/derive_opts.rs b/crates/macros/src/derive_opts.rs index 142411e2..000c267c 100644 --- a/crates/macros/src/derive_opts.rs +++ b/crates/macros/src/derive_opts.rs @@ -70,7 +70,7 @@ impl<'a> From<&'a DeriveInput> for OptsBuilder<'a> { } } -impl<'a> OptsBuilder<'a> { +impl OptsBuilder<'_> { /// Returns the `impl Clone` block for the builder. #[inline] fn impl_clone(&self) -> TokenStream { @@ -187,7 +187,7 @@ impl<'a> TryFrom<&'a DeriveInput> for OptsFields<'a> { } } -impl<'a> OptsFields<'a> { +impl OptsFields<'_> { #[inline] fn setters(&self) -> impl Iterator + '_ { self.fields.iter().filter_map(|field| field.setter(self.mask_name)) @@ -249,7 +249,7 @@ impl<'a> TryFrom<&'a Field> for OptsField<'a> { } } -impl<'a> OptsField<'a> { +impl OptsField<'_> { /// TODO: docs #[inline] fn setter(&self, mask_name: Option<&Ident>) -> Option { diff --git a/crates/types/src/function.rs b/crates/types/src/function.rs index 68395b1b..88832470 100644 --- a/crates/types/src/function.rs +++ b/crates/types/src/function.rs @@ -176,7 +176,7 @@ mod serde { struct FunctionVisitor(PhantomData, PhantomData); - impl<'de, A, R> Visitor<'de> for FunctionVisitor { + impl Visitor<'_> for FunctionVisitor { type Value = Function; fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/crates/types/src/non_owning.rs b/crates/types/src/non_owning.rs index dd7bb80d..813dacec 100644 --- a/crates/types/src/non_owning.rs +++ b/crates/types/src/non_owning.rs @@ -11,13 +11,13 @@ pub struct NonOwning<'a, T> { lt: PhantomData<&'a ()>, } -impl<'a, T> NonOwning<'a, T> { +impl NonOwning<'_, T> { pub const fn new(value: T) -> Self { Self { inner: ManuallyDrop::new(value), lt: PhantomData } } } -impl<'a, T> core::fmt::Debug for NonOwning<'a, T> +impl core::fmt::Debug for NonOwning<'_, T> where T: core::fmt::Debug, { @@ -27,7 +27,7 @@ where } } -impl<'a, T> Default for NonOwning<'a, T> +impl Default for NonOwning<'_, T> where T: Default, { diff --git a/crates/types/src/serde/de.rs b/crates/types/src/serde/de.rs index 5529818c..9f8d09c5 100644 --- a/crates/types/src/serde/de.rs +++ b/crates/types/src/serde/de.rs @@ -16,7 +16,7 @@ impl Deserializer { } } -impl<'de> IntoDeserializer<'de, DeserializeError> for Object { +impl IntoDeserializer<'_, DeserializeError> for Object { type Deserializer = Deserializer; #[inline] diff --git a/crates/types/src/string.rs b/crates/types/src/string.rs index 3ec6543a..42dd22d1 100644 --- a/crates/types/src/string.rs +++ b/crates/types/src/string.rs @@ -295,7 +295,7 @@ mod serde { { struct StringVisitor; - impl<'de> Visitor<'de> for StringVisitor { + impl Visitor<'_> for StringVisitor { type Value = crate::String; fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { From e01ba4e847ec7e20dc99ad6dbc3a3b022a55532f Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Thu, 24 Oct 2024 17:49:11 +0800 Subject: [PATCH 11/11] api: add a `mouse` field to `Window{Config,Opts}` on nightly Tracks https://github.com/neovim/neovim/commit/9b8907d90508d7b66f025bbd1f5a48a78c5ce035. --- CHANGELOG.md | 3 +++ crates/api/src/types/window_config.rs | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a619b77b..863efc72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ - a `String::to_str()` method which returns a `&str` if the string contains valid UTF-8 data; +- a `WindowConfig::mouse` field and a `WindowOptsBuilder::mouse()` method + on nightly ([#189](https://github.com/noib3/nvim-oxi/pull/189)); + ### Changed - `nvim_oxi::api::echo` is now generic over the highlight group type instead of diff --git a/crates/api/src/types/window_config.rs b/crates/api/src/types/window_config.rs index 90581640..ea77b5f2 100644 --- a/crates/api/src/types/window_config.rs +++ b/crates/api/src/types/window_config.rs @@ -75,6 +75,11 @@ pub struct WindowConfig { )] pub hide: Option, + #[cfg_attr(docsrs, doc(cfg(feature = "neovim-nightly")))] + #[cfg(feature = "neovim-nightly")] // On 0.10 and nightly. + #[serde(default)] + pub mouse: bool, + /// If `true` then no buffer-related autocommand events such as `BufEnter` /// or `BufLeave` are fired when calling [`open_win`](crate::open_win). pub noautocmd: Option, @@ -436,6 +441,9 @@ pub struct WindowOpts { bufpos: Array, external: Boolean, focusable: Boolean, + #[cfg_attr(docsrs, doc(cfg(feature = "neovim-nightly")))] + #[cfg(feature = "neovim-nightly")] + mouse: Boolean, vertical: Boolean, zindex: Integer, border: Object, @@ -501,6 +509,9 @@ impl From<&WindowConfig> for WindowOpts { builder.focusable(focusable); } + #[cfg(feature = "neovim-nightly")] + builder.mouse(config.mouse); + if let Some(vertical) = config.vertical { builder.vertical(vertical); } @@ -567,6 +578,8 @@ impl TryFrom for WindowConfig { footer_pos, height, hide, + #[cfg(feature = "neovim-nightly")] + mouse, noautocmd, relative, row, @@ -639,6 +652,8 @@ impl TryFrom for WindowConfig { ))?, height: deserialize(height)?, hide: deserialize(hide)?, + #[cfg(feature = "neovim-nightly")] + mouse, noautocmd: deserialize(noautocmd)?, relative, row: deserialize(row)?,