Skip to content

Commit

Permalink
api: update BufAttachOpts for nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Dec 6, 2023
1 parent 6da2c83 commit e86db08
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
1 change: 1 addition & 0 deletions crates/oxi-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ neovim-nightly = []

[dependencies]
oxi-luajit = { workspace = true }
oxi-macros = { workspace = true }
oxi-types = { workspace = true }

serde = { version = "1.0", features = ["derive"] }
Expand Down
7 changes: 7 additions & 0 deletions crates/oxi-api/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,23 @@ impl Buffer {
opts: &BufAttachOpts,
) -> Result<()> {
let mut err = nvim::Error::new();

#[cfg(not(feature = "neovim-nightly"))]
let opts = Dictionary::from(opts);

let has_attached = unsafe {
nvim_buf_attach(
LUA_INTERNAL_CALL,
self.0,
send_buffer,
#[cfg(not(feature = "neovim-nightly"))]
opts.non_owning(),
#[cfg(feature = "neovim-nightly")]
opts,
&mut err,
)
};

choose!(
err,
match has_attached {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxi-api/src/ffi/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ extern "C" {
channel_id: u64,
buf: BufHandle,
send_buffer: bool,
opts: NonOwning<Dictionary>,
#[cfg(not(feature = "neovim-nightly"))] opts: NonOwning<Dictionary>,
#[cfg(feature = "neovim-nightly")] opts: *const BufAttachOpts,
err: *mut Error,
) -> bool;

Expand Down
97 changes: 82 additions & 15 deletions crates/oxi-api/src/opts/buf_attach.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxi_types::{Dictionary, Object};
use oxi_types as types;

use crate::Buffer;
use crate::ToFunction;
Expand Down Expand Up @@ -84,17 +84,19 @@ pub type OnReloadArgs = (String, Buffer);
pub type ShouldDetach = bool;

/// Options passed to [`Buffer::attach`](crate::Buffer::attach).
#[cfg(not(feature = "neovim-nightly"))]
#[derive(Clone, Debug, Default)]
pub struct BufAttachOpts {
on_bytes: Object,
on_changedtick: Object,
on_detach: Object,
on_lines: Object,
on_reload: Object,
preview: Object,
utf_sizes: Object,
on_bytes: types::Object,
on_changedtick: types::Object,
on_detach: types::Object,
on_lines: types::Object,
on_reload: types::Object,
preview: types::Object,
utf_sizes: types::Object,
}

#[cfg(not(feature = "neovim-nightly"))]
impl BufAttachOpts {
#[inline(always)]
/// Creates a new [`BufAttachOptsBuilder`].
Expand All @@ -103,9 +105,11 @@ impl BufAttachOpts {
}
}

#[cfg(not(feature = "neovim-nightly"))]
#[derive(Clone, Default)]
pub struct BufAttachOptsBuilder(BufAttachOpts);

#[cfg(not(feature = "neovim-nightly"))]
impl BufAttachOptsBuilder {
/// Callback invoked on change. It receives more granular information about
/// the change compared to [`on_lines`](BufAttachOptsBuilder::on_lines).
Expand All @@ -114,7 +118,7 @@ impl BufAttachOptsBuilder {
where
F: ToFunction<OnBytesArgs, ShouldDetach>,
{
self.0.on_bytes = Object::from_luaref(on_bytes.into_luaref());
self.0.on_bytes = types::Object::from_luaref(on_bytes.into_luaref());
self
}

Expand All @@ -125,7 +129,7 @@ impl BufAttachOptsBuilder {
F: ToFunction<OnChangedtickArgs, ShouldDetach>,
{
self.0.on_changedtick =
Object::from_luaref(on_changedtick.into_luaref());
types::Object::from_luaref(on_changedtick.into_luaref());
self
}

Expand All @@ -135,7 +139,7 @@ impl BufAttachOptsBuilder {
where
F: ToFunction<OnDetachArgs, ShouldDetach>,
{
self.0.on_detach = Object::from_luaref(on_detach.into_luaref());
self.0.on_detach = types::Object::from_luaref(on_detach.into_luaref());
self
}

Expand All @@ -145,7 +149,7 @@ impl BufAttachOptsBuilder {
where
F: ToFunction<OnLinesArgs, ShouldDetach>,
{
self.0.on_lines = Object::from_luaref(fun.into_luaref());
self.0.on_lines = types::Object::from_luaref(fun.into_luaref());
self
}

Expand All @@ -156,7 +160,7 @@ impl BufAttachOptsBuilder {
where
F: ToFunction<OnReloadArgs, ShouldDetach>,
{
self.0.on_reload = Object::from_luaref(on_reload.into_luaref());
self.0.on_reload = types::Object::from_luaref(on_reload.into_luaref());
self
}

Expand Down Expand Up @@ -184,10 +188,10 @@ impl BufAttachOptsBuilder {
}
}

impl From<&BufAttachOpts> for Dictionary {
#[cfg(not(feature = "neovim-nightly"))]
impl From<&BufAttachOpts> for types::Dictionary {
#[inline]
fn from(opts: &BufAttachOpts) -> Self {
// TODO: don't clone by making non-owning version of Dictionary
Self::from_iter([
("on_bytes", opts.on_bytes.clone()),
("on_changedtick", opts.on_changedtick.clone()),
Expand All @@ -199,3 +203,66 @@ impl From<&BufAttachOpts> for Dictionary {
])
}
}

/// Options passed to [`Buffer::attach`](crate::Buffer::attach).
#[cfg(feature = "neovim-nightly")]
#[derive(Clone, Debug, Default, oxi_macros::OptsBuilder)]
#[repr(C)]
pub struct BufAttachOpts {
#[builder(mask)]
mask: u64,

/// Callback invoked on change. It receives more granular information about
/// the change compared to [`on_lines`](BufAttachOptsBuilder::on_lines).
#[builder(
generics = "F: ToFunction<OnBytesArgs, ShouldDetach>",
argtype = "F",
inline = "{0}.into_luaref()"
)]
on_bytes: types::LuaRef,

/// Callback invoked on changedtick increment without text change.
#[builder(
generics = "F: ToFunction<OnChangedtickArgs, ShouldDetach>",
argtype = "F",
inline = "{0}.into_luaref()"
)]
on_changedtick: types::LuaRef,

/// Callback invoked on detach.
#[builder(
generics = "F: ToFunction<OnDetachArgs, ShouldDetach>",
argtype = "F",
inline = "{0}.into_luaref()"
)]
on_detach: types::LuaRef,

/// Callback invoked on change.
#[builder(
generics = "F: ToFunction<OnLinesArgs, ShouldDetach>",
argtype = "F",
inline = "{0}.into_luaref()"
)]
on_lines: types::LuaRef,

/// Callback invoked on reload. The entire buffer content should be
/// considered changed.
#[builder(
generics = "F: ToFunction<OnReloadArgs, ShouldDetach>",
argtype = "F",
inline = "{0}.into_luaref()"
)]
on_reload: types::LuaRef,

/// Whether to also attach to command preview (i.e.
/// [`inccommand`](https://neovim.io/doc/user/options.html#'inccommand'))
/// events.
#[builder(argtype = "bool")]
preview: types::Boolean,

/// Whether to include the UTF-32 and UTF-16 sizes of the replaced region
/// as the last arguments of the
/// [`on_lines`](BufAttachOptsBuilder::on_lines) callback.
#[builder(argtype = "bool")]
utf_sizes: types::Boolean,
}

0 comments on commit e86db08

Please sign in to comment.