mirror of
https://github.com/noib3/nvim-oxi.git
synced 2025-07-07 21:35:18 +00:00
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:
parent
61cc490370
commit
a72ad38339
12 changed files with 96 additions and 55 deletions
|
@ -1,11 +1,11 @@
|
||||||
[target.x86_64-apple-darwin]
|
[target.x86_64-apple-darwin]
|
||||||
rustflags = [
|
rustflags = ["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
|
||||||
"-C", "link-arg=-undefined",
|
|
||||||
"-C", "link-arg=dynamic_lookup",
|
|
||||||
]
|
|
||||||
|
|
||||||
[target.aarch64-apple-darwin]
|
[target.aarch64-apple-darwin]
|
||||||
rustflags = [
|
rustflags = ["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
|
||||||
"-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
.github/workflows/ci.yml
vendored
7
.github/workflows/ci.yml
vendored
|
@ -34,11 +34,16 @@ jobs:
|
||||||
with:
|
with:
|
||||||
neovim: true
|
neovim: true
|
||||||
version: ${{ matrix.neovim }}
|
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`
|
- name: Install latest stable `rustc`
|
||||||
uses: dtolnay/rust-toolchain@stable
|
uses: dtolnay/rust-toolchain@stable
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: cargo test --workspace ${{ matrix.features }}
|
run: cargo test --workspace ${{ matrix.features }}
|
||||||
working-directory: .
|
|
||||||
|
|
||||||
clippy:
|
clippy:
|
||||||
name: clippy
|
name: clippy
|
||||||
|
|
|
@ -29,6 +29,10 @@
|
||||||
- the argument of `SetHighlightOptsBuilder::link()` from `&str` to any type
|
- the argument of `SetHighlightOptsBuilder::link()` from `&str` to any type
|
||||||
implementing `HlGroup`;
|
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
|
### Removed
|
||||||
|
|
||||||
- the `SetHighlightOptsBuilder::global_link()` method. Use
|
- the `SetHighlightOptsBuilder::global_link()` method. Use
|
||||||
|
|
|
@ -64,7 +64,6 @@ cargo_metadata = { version = "0.19", optional = true }
|
||||||
mlua = { version = "0.10", features = ["luajit"], optional = true }
|
mlua = { version = "0.10", features = ["luajit"], optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
mlua = { version = "0.10", features = ["luajit", "module"] }
|
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
tokio = { version = "1.0", features = ["full"] }
|
tokio = { version = "1.0", features = ["full"] }
|
||||||
|
|
||||||
|
|
|
@ -252,6 +252,8 @@ extern "C" {
|
||||||
msg: NonOwning<String>,
|
msg: NonOwning<String>,
|
||||||
log_level: Integer,
|
log_level: Integer,
|
||||||
opts: NonOwning<Dictionary>,
|
opts: NonOwning<Dictionary>,
|
||||||
|
#[cfg(feature = "neovim-0-10")] // On 0.10 and Nightly.
|
||||||
|
arena: *mut Arena,
|
||||||
err: *mut Error,
|
err: *mut Error,
|
||||||
) -> Object;
|
) -> Object;
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,6 @@ mod get_mark;
|
||||||
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
|
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
|
||||||
mod get_namespace;
|
mod get_namespace;
|
||||||
mod get_text;
|
mod get_text;
|
||||||
mod notify;
|
|
||||||
mod open_term;
|
mod open_term;
|
||||||
mod option;
|
mod option;
|
||||||
mod parse_cmd;
|
mod parse_cmd;
|
||||||
|
@ -61,7 +60,6 @@ pub use get_mark::*;
|
||||||
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
|
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
|
||||||
pub use get_namespace::*;
|
pub use get_namespace::*;
|
||||||
pub use get_text::*;
|
pub use get_text::*;
|
||||||
pub use notify::*;
|
|
||||||
pub use open_term::*;
|
pub use open_term::*;
|
||||||
pub use option::*;
|
pub use option::*;
|
||||||
pub use parse_cmd::*;
|
pub use parse_cmd::*;
|
||||||
|
|
|
@ -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()
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -715,20 +715,21 @@ pub fn load_context(ctx: EditorContext) {
|
||||||
pub fn notify(
|
pub fn notify(
|
||||||
msg: &str,
|
msg: &str,
|
||||||
log_level: LogLevel,
|
log_level: LogLevel,
|
||||||
opts: &NotifyOpts,
|
opts: &Dictionary,
|
||||||
) -> Result<()> {
|
) -> Result<Object> {
|
||||||
let msg = nvim::String::from(msg);
|
let msg = nvim::String::from(msg);
|
||||||
let opts = Dictionary::from(opts);
|
|
||||||
let mut err = nvim::Error::new();
|
let mut err = nvim::Error::new();
|
||||||
let _ = unsafe {
|
let obj = unsafe {
|
||||||
nvim_notify(
|
nvim_notify(
|
||||||
msg.non_owning(),
|
msg.non_owning(),
|
||||||
log_level as Integer,
|
log_level as Integer,
|
||||||
opts.non_owning(),
|
opts.non_owning(),
|
||||||
|
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
|
||||||
|
types::arena(),
|
||||||
&mut err,
|
&mut err,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
choose!(err, ())
|
choose!(err, Ok(obj))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Binding to [`nvim_open_term()`][1].
|
/// Binding to [`nvim_open_term()`][1].
|
||||||
|
|
|
@ -2,12 +2,10 @@ use std::error::Error as StdError;
|
||||||
use std::ffi::{c_char, CStr, CString};
|
use std::ffi::{c_char, CStr, CString};
|
||||||
use std::fmt;
|
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
|
// 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.
|
/// Binding to the error type used by Neovim.
|
||||||
#[derive(Clone, ThisError, Eq, PartialEq, Hash)]
|
#[derive(Clone, Eq, PartialEq, Hash)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
r#type: ErrorType,
|
r#type: ErrorType,
|
||||||
|
@ -16,6 +14,7 @@ pub struct Error {
|
||||||
|
|
||||||
unsafe impl Send for Error {}
|
unsafe impl Send for Error {}
|
||||||
unsafe impl Sync 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
|
// https://github.com/neovim/neovim/blob/v0.9.0/src/nvim/api/private/defs.h#L27-L31
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use mlua::prelude::LuaFunction;
|
use mlua::prelude::LuaFunction;
|
||||||
use nvim_oxi::{mlua::lua, print, Result};
|
use nvim_oxi::{mlua, print, Result};
|
||||||
|
|
||||||
#[nvim_oxi::plugin]
|
#[nvim_oxi::plugin]
|
||||||
fn mlua() -> Result<()> {
|
fn mlua() -> Result<()> {
|
||||||
print!("Hello from nvim-oxi..");
|
print!("Hello from nvim-oxi..");
|
||||||
let lua = lua();
|
let lua = mlua::lua();
|
||||||
let print: LuaFunction = lua.globals().get("print")?;
|
let print: LuaFunction = lua.globals().get("print")?;
|
||||||
print.call("..and goodbye from mlua!")?;
|
print.call::<()>("..and goodbye from mlua!")?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,13 +13,15 @@ neovim-0-9 = ["nvim-oxi/neovim-0-9"]
|
||||||
neovim-0-10 = ["neovim-0-9", "nvim-oxi/neovim-0-10"]
|
neovim-0-10 = ["neovim-0-9", "nvim-oxi/neovim-0-10"]
|
||||||
neovim-nightly = ["neovim-0-10", "nvim-oxi/neovim-nightly"]
|
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"
|
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]
|
[target.'cfg(any(target_os = "windows", target_env = "msvc"))'.dependencies]
|
||||||
all_asserts = "2.3"
|
nvim-oxi = { path = "..", features = ["mlua", "test", "__vendored_luajit"] }
|
||||||
nvim-oxi = { path = "..", features = ["test"] }
|
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
nvim-oxi = { path = "..", features = ["test"] }
|
nvim-oxi = { path = "..", features = ["test"] }
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use all_asserts::*;
|
use all_asserts::*;
|
||||||
use nvim_oxi::api::{self, opts::*, types::*, Buffer, Window};
|
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]
|
#[nvim_oxi::test]
|
||||||
fn chan_send_fail() {
|
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]
|
#[nvim_oxi::test]
|
||||||
fn set_get_del_current_line() {
|
fn set_get_del_current_line() {
|
||||||
let res = api::set_current_line("foo");
|
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())));
|
|| ('a'..='f').contains(&c.to_ascii_lowercase())));
|
||||||
u32::from_str_radix(&hex_color[1..], 16).unwrap()
|
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();
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue