Skip to content

Commit

Permalink
Fix nvim_oxi::api::notify() (#208)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
noib3 authored Dec 26, 2024
1 parent 61cc490 commit a72ad38
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 55 deletions.
16 changes: 8 additions & 8 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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"]
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
2 changes: 2 additions & 0 deletions crates/api/src/ffi/vim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 0 additions & 2 deletions crates/api/src/opts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::*;
Expand Down
28 changes: 0 additions & 28 deletions crates/api/src/opts/notify.rs

This file was deleted.

11 changes: 6 additions & 5 deletions crates/api/src/vim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down
5 changes: 2 additions & 3 deletions crates/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)]
Expand Down
6 changes: 3 additions & 3 deletions examples/mlua.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
10 changes: 6 additions & 4 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
59 changes: 59 additions & 0 deletions tests/src/api/global.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
}

0 comments on commit a72ad38

Please sign in to comment.