Skip to content

Commit

Permalink
Merge pull request #196 from noib3/fix-buffer-call-always-returns-luaref
Browse files Browse the repository at this point in the history
Fix `Buffer::call()` always returning `LuaRef`s
  • Loading branch information
noib3 authored Dec 5, 2024
2 parents 23f78f8 + 665d409 commit 5b603ed
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 34 deletions.
32 changes: 31 additions & 1 deletion crates/api/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,37 @@ impl Buffer {
{
let fun = Function::from_fn_once(fun);
let mut err = nvim::Error::new();
let obj = unsafe { nvim_buf_call(self.0, fun.lua_ref(), &mut err) };

let obj = if cfg!(not(feature = "neovim-0-10")) {
// Only on 0.9.
unsafe { nvim_buf_call(self.0, fun.lua_ref(), &mut err) }
} else {
// On 0.10 and Nightly.
let ref_or_nil =
unsafe { nvim_buf_call(self.0, fun.lua_ref(), &mut err) };

let lua_ref = match ref_or_nil.kind() {
types::ObjectKind::LuaRef => unsafe {
ref_or_nil.as_luaref_unchecked()
},
types::ObjectKind::Nil => {
return Ret::from_object(Object::nil()).map_err(Into::into)
},
other => panic!("Unexpected object kind: {:?}", other),
};

unsafe {
lua::with_state(|lstate| {
lua::ffi::lua_rawgeti(
lstate,
lua::ffi::LUA_REGISTRYINDEX,
lua_ref,
);
Object::pop(lstate)
})
}
.map_err(Error::custom)?
};

choose!(err, {
fun.remove_from_lua_registry();
Expand Down
32 changes: 31 additions & 1 deletion crates/api/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,37 @@ impl Window {
{
let fun = Function::from_fn_once(fun);
let mut err = nvim::Error::new();
let obj = unsafe { nvim_win_call(self.0, fun.lua_ref(), &mut err) };

let obj = if cfg!(not(feature = "neovim-0-10")) {
// Only on 0.9.
unsafe { nvim_win_call(self.0, fun.lua_ref(), &mut err) }
} else {
// On 0.10 and Nightly.
let ref_or_nil =
unsafe { nvim_win_call(self.0, fun.lua_ref(), &mut err) };

let lua_ref = match ref_or_nil.kind() {
types::ObjectKind::LuaRef => unsafe {
ref_or_nil.as_luaref_unchecked()
},
types::ObjectKind::Nil => {
return Ret::from_object(Object::nil()).map_err(Into::into)
},
other => panic!("Unexpected object kind: {:?}", other),
};

unsafe {
lua::with_state(|lstate| {
lua::ffi::lua_rawgeti(
lstate,
lua::ffi::LUA_REGISTRYINDEX,
lua_ref,
);
Object::pop(lstate)
})
}
.map_err(crate::Error::custom)?
};

choose!(err, {
fun.remove_from_lua_registry();
Expand Down
46 changes: 26 additions & 20 deletions tests/src/api/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use std::ops;
use std::rc::Rc;

use all_asserts::*;
use nvim_oxi as nvim;
use nvim_oxi::api::{self, opts::*, types::*, Buffer};

#[nvim::test]
#[nvim_oxi::test]
fn buf_attach() {
let buf = Buffer::current();

Expand All @@ -25,7 +24,7 @@ fn buf_attach() {
assert!(bytes_written.is_ok(), "{bytes_written:?}");
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_attach_on_bytes() -> Result<(), api::Error> {
let mut buffer = api::create_buf(true, false)?;

Expand All @@ -44,7 +43,7 @@ fn buf_attach_on_bytes() -> Result<(), api::Error> {

buffer.attach(false, &opts)?;

nvim::api::Window::current().set_buf(&buffer)?;
api::Window::current().set_buf(&buffer)?;

buffer.set_text(0..0, 0, 0, [" "])?;
buffer.set_text(0..0, 0, 0, [" "])?;
Expand All @@ -55,14 +54,21 @@ fn buf_attach_on_bytes() -> Result<(), api::Error> {
Ok(())
}

#[nvim::test]
fn buf_call() {
#[nvim_oxi::test]
fn buf_call_nil() {
let buf = Buffer::current();
let res = buf.call(|_| ());
assert_eq!(Ok(()), res);
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_call_int() {
let buf = Buffer::current();
let res = buf.call(|_| 42);
assert_eq!(Ok(42), res);
}

#[nvim_oxi::test]
fn buf_create_del_user_command() {
let mut buf = Buffer::current();

Expand All @@ -86,13 +92,13 @@ fn buf_create_del_user_command() {
assert_eq!(Ok(()), buf.del_user_command("Bar"));
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_get_changedtick() {
let buf = Buffer::current();
assert!(buf.get_changedtick().is_ok());
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_get_lines_range_bounds() {
let mut buf = api::create_buf(true, false).unwrap();

Expand Down Expand Up @@ -124,20 +130,20 @@ fn buf_get_lines_range_bounds() {
}
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_loaded_n_valid() {
let buf = Buffer::current();
assert!(buf.is_loaded());
assert!(buf.is_valid());
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_new_delete() {
let buf = api::create_buf(true, false).unwrap();
assert_eq!(Ok(()), buf.delete(&Default::default()));
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_set_get_del_keymap() {
let mut buf = Buffer::current();

Expand All @@ -157,7 +163,7 @@ fn buf_set_get_del_keymap() {
assert_eq!(Ok(()), res);
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_set_get_del_nvo_keymap() {
let mut buf = Buffer::current();

Expand All @@ -179,7 +185,7 @@ fn buf_set_get_del_nvo_keymap() {
assert_eq!(Ok(()), res);
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_set_get_del_lines() {
let mut buf = Buffer::current();

Expand All @@ -197,7 +203,7 @@ fn buf_set_get_del_lines() {
assert_eq!(Ok(1), buf.line_count());
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_set_get_del_mark() {
let mut buf = Buffer::current();
let opts = SetMarkOpts::default();
Expand All @@ -211,7 +217,7 @@ fn buf_set_get_del_mark() {
assert_eq!(Ok(()), res);
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_set_get_del_text() {
let mut buf = Buffer::current();

Expand Down Expand Up @@ -243,15 +249,15 @@ fn buf_set_get_del_text() {
assert_eq!(Ok(1), buf.line_count());
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_set_get_del_var() {
let mut buf = Buffer::current();
buf.set_var("foo", 42).unwrap();
assert_eq!(Ok(42), buf.get_var("foo"));
assert_eq!(Ok(()), buf.del_var("foo"));
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_set_get_name() {
let mut buf = api::create_buf(true, false).unwrap();

Expand All @@ -267,7 +273,7 @@ fn buf_set_get_name() {
assert_eq!(Ok(()), buf.set_name(""));
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_set_get_option() {
let mut buf = Buffer::current();

Expand All @@ -278,7 +284,7 @@ fn buf_set_get_option() {
assert!(!buf.get_option::<bool>("modified").unwrap());
}

#[nvim::test]
#[nvim_oxi::test]
fn buf_terminal_name() {
api::command("term").unwrap();

Expand Down
30 changes: 18 additions & 12 deletions tests/src/api/window.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use nvim_oxi as oxi;
use nvim_oxi::api::{self, types::*, Buffer, TabPage, Window};

#[oxi::test]
fn win_call() {
#[nvim_oxi::test]
fn win_call_nil() {
let win = Window::current();
let res = win.call(|_| ());
assert_eq!(Ok(()), res);
}

#[oxi::test]
#[nvim_oxi::test]
fn win_call_int() {
let win = Window::current();
let res = win.call(|_| 42);
assert_eq!(Ok(42), res);
}

#[nvim_oxi::test]
fn close_hide() {
let config = WindowConfig::builder()
.relative(WindowRelativeTo::Editor)
Expand All @@ -27,17 +33,17 @@ fn close_hide() {
assert_eq!(Ok(()), win.hide());
}

#[oxi::test]
#[nvim_oxi::test]
fn win_get_number() {
assert_eq!(Ok(1), Window::current().get_number());
}

#[oxi::test]
#[nvim_oxi::test]
fn get_position() {
assert_eq!(Ok((0, 0)), Window::current().get_position());
}

#[oxi::test]
#[nvim_oxi::test]
fn get_set_buf() {
let mut win = Window::current();

Expand All @@ -54,7 +60,7 @@ fn get_set_buf() {
assert_eq!(Ok(()), res);
}

#[oxi::test]
#[nvim_oxi::test]
fn get_set_height_width() {
let config = WindowConfig::builder()
.relative(WindowRelativeTo::Editor)
Expand All @@ -78,12 +84,12 @@ fn get_set_height_width() {
assert_eq!(10, win.get_width().unwrap());
}

#[oxi::test]
#[nvim_oxi::test]
fn get_tabpage() {
assert_eq!(Ok(TabPage::current()), Window::current().get_tabpage())
}

#[oxi::test]
#[nvim_oxi::test]
fn set_get_cursor() {
let mut buf = Buffer::current();
buf.set_lines(.., true, ["foo"]).unwrap();
Expand All @@ -101,7 +107,7 @@ fn set_get_cursor() {
assert_eq!(Ok((1, 0)), win.get_cursor());
}

#[oxi::test]
#[nvim_oxi::test]
fn win_set_get_option() {
let mut win = Window::current();

Expand All @@ -112,7 +118,7 @@ fn win_set_get_option() {
assert!(!win.get_option::<bool>("spell").unwrap());
}

#[oxi::test]
#[nvim_oxi::test]
fn win_set_get_del_var() {
let mut win = Window::current();
win.set_var("foo", 42).unwrap();
Expand Down

0 comments on commit 5b603ed

Please sign in to comment.