Fix nvim_oxi::api::notify() (#208)

* Remove `mlua` from the `dev-dependencies`

* Test `api::notify()`

* Fix `api::notify()`'s API

* Test `api::notify()` w/ custom provider

* Add an `Arena` argument to `nvim_notify` on 0.10 and Nightly

* Directly implement `Error` for `types::Error`

* Test that errors returned by `vim.notify` are propagated

* Fix using `mlua` in CI

* Don't test `notify_custom{_err}` on `v0.9.5` on macOS and Windows
This commit is contained in:
Riccardo Mazzarini 2024-12-27 03:28:46 +08:00 committed by GitHub
parent 61cc490370
commit a72ad38339
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 96 additions and 55 deletions

View file

@ -1,11 +1,11 @@
[target.x86_64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]
rustflags = ["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
[target.aarch64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]
rustflags = ["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "link-args=/FORCE:MULTIPLE"]
[target.aarch64-pc-windows-msvc]
rustflags = ["-C", "link-args=/FORCE:MULTIPLE"]

View file

@ -34,11 +34,16 @@ jobs:
with:
neovim: true
version: ${{ matrix.neovim }}
- name: Install libluajit (Linux)
if: runner.os == 'Linux'
run: sudo apt-get update && sudo apt-get install -y libluajit-5.1-dev
- name: Install libluajit (macOS)
if: runner.os == 'macOS'
run: brew install luajit
- name: Install latest stable `rustc`
uses: dtolnay/rust-toolchain@stable
- name: Run tests
run: cargo test --workspace ${{ matrix.features }}
working-directory: .
clippy:
name: clippy

View file

@ -29,6 +29,10 @@
- the argument of `SetHighlightOptsBuilder::link()` from `&str` to any type
implementing `HlGroup`;
- `nvim_oxi::api::notify()` now takes a `&Dictionary` instead of `&NotifyOpts`
at its third parameter, and returns `Result<Object>` instead of `Result<()>`
([#208](https://github.com/noib3/nvim-oxi/pull/208));
### Removed
- the `SetHighlightOptsBuilder::global_link()` method. Use

View file

@ -64,7 +64,6 @@ cargo_metadata = { version = "0.19", optional = true }
mlua = { version = "0.10", features = ["luajit"], optional = true }
[dev-dependencies]
mlua = { version = "0.10", features = ["luajit", "module"] }
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.0", features = ["full"] }

View file

@ -252,6 +252,8 @@ extern "C" {
msg: NonOwning<String>,
log_level: Integer,
opts: NonOwning<Dictionary>,
#[cfg(feature = "neovim-0-10")] // On 0.10 and Nightly.
arena: *mut Arena,
err: *mut Error,
) -> Object;

View file

@ -25,7 +25,6 @@ mod get_mark;
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
mod get_namespace;
mod get_text;
mod notify;
mod open_term;
mod option;
mod parse_cmd;
@ -61,7 +60,6 @@ pub use get_mark::*;
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
pub use get_namespace::*;
pub use get_text::*;
pub use notify::*;
pub use open_term::*;
pub use option::*;
pub use parse_cmd::*;

View file

@ -1,28 +0,0 @@
use types::Dictionary;
/// Options passed to [`notify()`](crate::notify). Currently unused.
#[derive(Clone, Debug, Default)]
pub struct NotifyOpts {}
impl NotifyOpts {
#[inline(always)]
pub fn builder() -> NotifyOptsBuilder {
NotifyOptsBuilder::default()
}
}
#[derive(Clone, Default)]
pub struct NotifyOptsBuilder(NotifyOpts);
impl NotifyOptsBuilder {
#[inline]
pub fn build(&mut self) -> NotifyOpts {
std::mem::take(&mut self.0)
}
}
impl From<&NotifyOpts> for Dictionary {
fn from(_: &NotifyOpts) -> Self {
Dictionary::new()
}
}

View file

@ -715,20 +715,21 @@ pub fn load_context(ctx: EditorContext) {
pub fn notify(
msg: &str,
log_level: LogLevel,
opts: &NotifyOpts,
) -> Result<()> {
opts: &Dictionary,
) -> Result<Object> {
let msg = nvim::String::from(msg);
let opts = Dictionary::from(opts);
let mut err = nvim::Error::new();
let _ = unsafe {
let obj = unsafe {
nvim_notify(
msg.non_owning(),
log_level as Integer,
opts.non_owning(),
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
types::arena(),
&mut err,
)
};
choose!(err, ())
choose!(err, Ok(obj))
}
/// Binding to [`nvim_open_term()`][1].

View file

@ -2,12 +2,10 @@ use std::error::Error as StdError;
use std::ffi::{c_char, CStr, CString};
use std::fmt;
use thiserror::Error as ThisError;
// https://github.com/neovim/neovim/blob/v0.9.0/src/nvim/api/private/defs.h#L64-L67
//
/// Binding to the error type used by Neovim.
#[derive(Clone, ThisError, Eq, PartialEq, Hash)]
#[derive(Clone, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct Error {
r#type: ErrorType,
@ -16,6 +14,7 @@ pub struct Error {
unsafe impl Send for Error {}
unsafe impl Sync for Error {}
impl StdError for Error {}
// https://github.com/neovim/neovim/blob/v0.9.0/src/nvim/api/private/defs.h#L27-L31
#[derive(Copy, Clone, Eq, PartialEq, Hash)]

View file

@ -1,11 +1,11 @@
use mlua::prelude::LuaFunction;
use nvim_oxi::{mlua::lua, print, Result};
use nvim_oxi::{mlua, print, Result};
#[nvim_oxi::plugin]
fn mlua() -> Result<()> {
print!("Hello from nvim-oxi..");
let lua = lua();
let lua = mlua::lua();
let print: LuaFunction = lua.globals().get("print")?;
print.call("..and goodbye from mlua!")?;
print.call::<()>("..and goodbye from mlua!")?;
Ok(())
}

View file

@ -13,13 +13,15 @@ neovim-0-9 = ["nvim-oxi/neovim-0-9"]
neovim-0-10 = ["neovim-0-9", "nvim-oxi/neovim-0-10"]
neovim-nightly = ["neovim-0-10", "nvim-oxi/neovim-nightly"]
[target.'cfg(not(any(target_os = "windows", target_env = "msvc")))'.dependencies]
[dependencies]
all_asserts = "2.3"
nvim-oxi = { path = "..", features = ["libuv", "test"] }
thiserror = { workspace = true }
[target.'cfg(not(any(target_os = "windows", target_env = "msvc")))'.dependencies]
nvim-oxi = { path = "..", features = ["libuv", "mlua", "test"] }
[target.'cfg(any(target_os = "windows", target_env = "msvc"))'.dependencies]
all_asserts = "2.3"
nvim-oxi = { path = "..", features = ["test"] }
nvim-oxi = { path = "..", features = ["mlua", "test", "__vendored_luajit"] }
[build-dependencies]
nvim-oxi = { path = "..", features = ["test"] }

View file

@ -1,5 +1,9 @@
use std::sync::Arc;
use all_asserts::*;
use nvim_oxi::api::{self, opts::*, types::*, Buffer, Window};
use nvim_oxi::mlua::{Error as LuaError, IntoLuaMulti, Lua, Table};
use nvim_oxi::{Dictionary, Object};
#[nvim_oxi::test]
fn chan_send_fail() {
@ -176,6 +180,46 @@ fn list_wins() {
);
}
#[nvim_oxi::test]
fn notify() {
let opts = Dictionary::new();
let ret = api::notify("", LogLevel::Error, &opts).unwrap();
assert_eq!(ret, Object::nil());
}
// Fails on 0.9.5 on macOS and Windows. Not sure why.
#[nvim_oxi::test]
#[cfg_attr(not(any(target_os = "linux", feature = "neovim-0-10")), ignore)]
fn notify_custom() {
let message = "Notifier was called!";
// Set up a custom notification provider.
set_notification_provider(move |lua, _msg, _level, _opts| {
lua.create_string(message)
});
let opts = Dictionary::new();
let ret = api::notify("", LogLevel::Error, &opts).unwrap();
assert_eq!(ret, message.into());
}
// Fails on 0.9.5 on macOS and Windows. Not sure why.
#[nvim_oxi::test]
#[cfg_attr(not(any(target_os = "linux", feature = "neovim-0-10")), ignore)]
fn notify_custom_err() {
#[derive(Debug, thiserror::Error)]
#[error("")]
struct CustomError;
// Set up a custom notification provider.
set_notification_provider(move |_lua, _msg, _level, _opts| {
Err::<(), _>(LuaError::ExternalError(Arc::new(CustomError)))
});
let opts = Dictionary::new();
let _err = api::notify("", LogLevel::Error, &opts).unwrap_err();
}
#[nvim_oxi::test]
fn set_get_del_current_line() {
let res = api::set_current_line("foo");
@ -273,3 +317,18 @@ fn hex_to_dec(hex_color: &str) -> u32 {
|| ('a'..='f').contains(&c.to_ascii_lowercase())));
u32::from_str_radix(&hex_color[1..], 16).unwrap()
}
fn set_notification_provider<P, R>(mut provider: P)
where
P: FnMut(&Lua, String, u32, Table) -> Result<R, LuaError> + 'static,
R: IntoLuaMulti,
{
let lua = nvim_oxi::mlua::lua();
let vim = lua.globals().get::<Table>("vim").unwrap();
let notify = lua
.create_function_mut(move |lua, (msg, level, opts)| {
provider(lua, msg, level, opts)
})
.unwrap();
vim.set("notify", notify).unwrap();
}