|
| 1 | +use std::sync::Arc; |
| 2 | + |
1 | 3 | use all_asserts::*;
|
2 | 4 | use nvim_oxi::api::{self, opts::*, types::*, Buffer, Window};
|
| 5 | +use nvim_oxi::mlua::{Error as LuaError, IntoLuaMulti, Lua, Table}; |
| 6 | +use nvim_oxi::{Dictionary, Object}; |
3 | 7 |
|
4 | 8 | #[nvim_oxi::test]
|
5 | 9 | fn chan_send_fail() {
|
@@ -176,6 +180,46 @@ fn list_wins() {
|
176 | 180 | );
|
177 | 181 | }
|
178 | 182 |
|
| 183 | +#[nvim_oxi::test] |
| 184 | +fn notify() { |
| 185 | + let opts = Dictionary::new(); |
| 186 | + let ret = api::notify("", LogLevel::Error, &opts).unwrap(); |
| 187 | + assert_eq!(ret, Object::nil()); |
| 188 | +} |
| 189 | + |
| 190 | +// Fails on 0.9.5 on macOS and Windows. Not sure why. |
| 191 | +#[nvim_oxi::test] |
| 192 | +#[cfg_attr(not(any(target_os = "linux", feature = "neovim-0-10")), ignore)] |
| 193 | +fn notify_custom() { |
| 194 | + let message = "Notifier was called!"; |
| 195 | + |
| 196 | + // Set up a custom notification provider. |
| 197 | + set_notification_provider(move |lua, _msg, _level, _opts| { |
| 198 | + lua.create_string(message) |
| 199 | + }); |
| 200 | + |
| 201 | + let opts = Dictionary::new(); |
| 202 | + let ret = api::notify("", LogLevel::Error, &opts).unwrap(); |
| 203 | + assert_eq!(ret, message.into()); |
| 204 | +} |
| 205 | + |
| 206 | +// Fails on 0.9.5 on macOS and Windows. Not sure why. |
| 207 | +#[nvim_oxi::test] |
| 208 | +#[cfg_attr(not(any(target_os = "linux", feature = "neovim-0-10")), ignore)] |
| 209 | +fn notify_custom_err() { |
| 210 | + #[derive(Debug, thiserror::Error)] |
| 211 | + #[error("")] |
| 212 | + struct CustomError; |
| 213 | + |
| 214 | + // Set up a custom notification provider. |
| 215 | + set_notification_provider(move |_lua, _msg, _level, _opts| { |
| 216 | + Err::<(), _>(LuaError::ExternalError(Arc::new(CustomError))) |
| 217 | + }); |
| 218 | + |
| 219 | + let opts = Dictionary::new(); |
| 220 | + let _err = api::notify("", LogLevel::Error, &opts).unwrap_err(); |
| 221 | +} |
| 222 | + |
179 | 223 | #[nvim_oxi::test]
|
180 | 224 | fn set_get_del_current_line() {
|
181 | 225 | let res = api::set_current_line("foo");
|
@@ -273,3 +317,18 @@ fn hex_to_dec(hex_color: &str) -> u32 {
|
273 | 317 | || ('a'..='f').contains(&c.to_ascii_lowercase())));
|
274 | 318 | u32::from_str_radix(&hex_color[1..], 16).unwrap()
|
275 | 319 | }
|
| 320 | + |
| 321 | +fn set_notification_provider<P, R>(mut provider: P) |
| 322 | +where |
| 323 | + P: FnMut(&Lua, String, u32, Table) -> Result<R, LuaError> + 'static, |
| 324 | + R: IntoLuaMulti, |
| 325 | +{ |
| 326 | + let lua = nvim_oxi::mlua::lua(); |
| 327 | + let vim = lua.globals().get::<Table>("vim").unwrap(); |
| 328 | + let notify = lua |
| 329 | + .create_function_mut(move |lua, (msg, level, opts)| { |
| 330 | + provider(lua, msg, level, opts) |
| 331 | + }) |
| 332 | + .unwrap(); |
| 333 | + vim.set("notify", notify).unwrap(); |
| 334 | +} |
0 commit comments