Skip to content

Commit

Permalink
Merge pull request #118 from noib3/opts-macro
Browse files Browse the repository at this point in the history
add `OptsBuilder` macro
  • Loading branch information
noib3 authored Dec 7, 2023
2 parents 83b5ddd + e38285b commit a67571c
Show file tree
Hide file tree
Showing 26 changed files with 1,394 additions and 642 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
41 changes: 35 additions & 6 deletions crates/oxi-api/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use oxi_types::{
conversion::{self, FromObject, ToObject},
Array,
BufHandle,
Dictionary,
Function,
Integer,
Object,
Expand Down Expand Up @@ -105,16 +104,23 @@ impl Buffer {
opts: &BufAttachOpts,
) -> Result<()> {
let mut err = nvim::Error::new();
let opts = Dictionary::from(opts);

#[cfg(not(feature = "neovim-nightly"))]
let opts = oxi_types::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 Expand Up @@ -252,8 +258,21 @@ impl Buffer {
/// [1]: https://neovim.io/doc/user/api.html#nvim_buf_delete()
pub fn delete(self, opts: &BufDeleteOpts) -> Result<()> {
let mut err = nvim::Error::new();
let opts = Dictionary::from(opts);
unsafe { nvim_buf_delete(self.0, opts.non_owning(), &mut err) };

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

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

choose!(err, ())
}

Expand Down Expand Up @@ -431,7 +450,8 @@ impl Buffer {
R: RangeBounds<usize>,
{
let mut err = nvim::Error::new();
let opts = Dictionary::from(opts);
#[cfg(not(feature = "neovim-nightly"))]
let opts = oxi_types::Dictionary::from(opts);
let (start, end) = utils::range_to_limits(line_range);
let lines = unsafe {
nvim_buf_get_text(
Expand All @@ -441,7 +461,10 @@ impl Buffer {
start_col.try_into()?,
end,
end_col.try_into()?,
#[cfg(not(feature = "neovim-nightly"))]
opts.non_owning(),
#[cfg(feature = "neovim-nightly")]
opts,
#[cfg(not(feature = "neovim-0-8"))]
// The nvim_buf_get_text() function returns no line if we use an actual lstate here
std::ptr::null_mut(),
Expand Down Expand Up @@ -579,16 +602,22 @@ impl Buffer {
name: char,
line: usize,
col: usize,
opts: &SetMarkOpts,
) -> Result<()> {
let mut err = nvim::Error::new();
let name = nvim::String::from(name);
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
let opts = oxi_types::Dictionary::from(opts);
let mark_was_set = unsafe {
nvim_buf_set_mark(
self.0,
name.non_owning(),
line.try_into()?,
col.try_into()?,
Dictionary::new().non_owning(),
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
opts.non_owning(),
#[cfg(feature = "neovim-nightly")]
opts,
&mut err,
)
};
Expand Down
16 changes: 7 additions & 9 deletions crates/oxi-api/src/extmark.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
use std::ops::RangeBounds;

use oxi_types::{
self as nvim,
conversion::FromObject,
Array,
Dictionary,
Integer,
};
use oxi_types::{self as nvim, conversion::FromObject, Array, Integer};

use crate::choose;
use crate::ffi::extmark::*;
Expand Down Expand Up @@ -121,14 +115,18 @@ impl Buffer {
extmark_id: u32,
opts: &GetExtmarkByIdOpts,
) -> Result<(usize, usize, Option<ExtmarkInfos>)> {
let opts = Dictionary::from(opts);
#[cfg(not(feature = "neovim-nightly"))]
let opts = oxi_types::Dictionary::from(opts);
let mut err = nvim::Error::new();
let tuple = unsafe {
nvim_buf_get_extmark_by_id(
self.0,
ns_id as Integer,
extmark_id as Integer,
#[cfg(not(feature = "neovim-nightly"))]
opts.non_owning(),
#[cfg(feature = "neovim-nightly")]
opts,
&mut err,
)
};
Expand Down Expand Up @@ -169,7 +167,7 @@ impl Buffer {
) -> Result<impl SuperIterator<(u32, usize, usize, Option<ExtmarkInfos>)>>
{
#[cfg(not(feature = "neovim-nightly"))]
let opts = Dictionary::from(opts);
let opts = oxi_types::Dictionary::from(opts);
let mut err = nvim::Error::new();
let extmarks = unsafe {
nvim_buf_get_extmarks(
Expand Down
11 changes: 8 additions & 3 deletions 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 Expand Up @@ -72,7 +73,8 @@ extern "C" {
// https://github.com/neovim/neovim/blob/v0.9.0/src/nvim/api/buffer.c#L1060
pub(crate) fn nvim_buf_delete(
buf: BufHandle,
opts: NonOwning<Dictionary>,
#[cfg(not(feature = "neovim-nightly"))] opts: NonOwning<Dictionary>,
#[cfg(feature = "neovim-nightly")] opts: *const BufDeleteOpts,
err: *mut Error,
);

Expand Down Expand Up @@ -145,7 +147,8 @@ extern "C" {
start_col: Integer,
end_row: Integer,
end_col: Integer,
opts: NonOwning<Dictionary>,
#[cfg(not(feature = "neovim-nightly"))] opts: NonOwning<Dictionary>,
#[cfg(feature = "neovim-nightly")] opts: *const GetTextOpts,
#[cfg(not(feature = "neovim-0-8"))]
lstate: *mut oxi_luajit::ffi::lua_State,
err: *mut Error,
Expand Down Expand Up @@ -198,7 +201,9 @@ extern "C" {
name: NonOwning<String>,
line: Integer,
col: Integer,
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
opts: NonOwning<Dictionary>,
#[cfg(feature = "neovim-nightly")] opts: *const SetMarkOpts,
err: *mut Error,
) -> bool;

Expand Down
3 changes: 2 additions & 1 deletion crates/oxi-api/src/ffi/extmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ extern "C" {
buf: BufHandle,
ns_id: Integer,
id: Integer,
opts: NonOwning<Dictionary>,
#[cfg(not(feature = "neovim-nightly"))] opts: NonOwning<Dictionary>,
#[cfg(feature = "neovim-nightly")] opts: *const GetExtmarkByIdOpts,
err: *mut Error,
) -> Array;

Expand Down
7 changes: 7 additions & 0 deletions crates/oxi-api/src/ffi/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ extern "C" {
// https://github.com/neovim/neovim/blob/v0.9.0/src/nvim/api/vim.c#L1987
pub(crate) fn nvim_get_mark(
name: NonOwning<String>,
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
opts: NonOwning<Dictionary>,
#[cfg(feature = "neovim-nightly")] opts: *const GetMarkOpts,
err: *mut Error,
) -> Array;

Expand Down Expand Up @@ -256,7 +258,9 @@ extern "C" {
// https://github.com/neovim/neovim/blob/v0.9.0/src/nvim/api/vim.c#L952
pub(crate) fn nvim_open_term(
buf: BufHandle,
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
opts: NonOwning<Dictionary>,
#[cfg(feature = "neovim-nightly")] opts: *const OpenTermOpts,
err: *mut Error,
) -> Integer;

Expand Down Expand Up @@ -293,7 +297,10 @@ extern "C" {
item: Integer,
insert: bool,
finish: bool,
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
opts: NonOwning<Dictionary>,
#[cfg(feature = "neovim-nightly")]
opts: *const SelectPopupMenuItemOpts,
err: *mut Error,
);

Expand Down
2 changes: 2 additions & 0 deletions crates/oxi-api/src/ffi/vimscript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ extern "C" {
// https://github.com/neovim/neovim/blob/v0.9.0/src/nvim/api/command.c#L98
pub(crate) fn nvim_parse_cmd(
src: NonOwning<String>,
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
opts: NonOwning<Dictionary>,
#[cfg(feature = "neovim-nightly")] opts: *const ParseCmdOpts,
error: *mut Error,
) -> Dictionary;

Expand Down
27 changes: 24 additions & 3 deletions crates/oxi-api/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,18 @@ pub fn get_mark(
opts: &GetMarkOpts,
) -> Result<(usize, usize, Buffer, String)> {
let name = nvim::String::from(name);
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
let opts = Dictionary::from(opts);
let mut err = nvim::Error::new();
let mark = unsafe {
nvim_get_mark(name.non_owning(), opts.non_owning(), &mut err)
nvim_get_mark(
name.non_owning(),
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
opts.non_owning(),
#[cfg(feature = "neovim-nightly")]
opts,
&mut err,
)
};
choose!(err, {
let mut iter = mark.into_iter();
Expand Down Expand Up @@ -739,10 +747,19 @@ pub fn notify(
///
/// [1]: https://neovim.io/doc/user/api.html#nvim_open_term()
pub fn open_term(buffer: &Buffer, opts: &OpenTermOpts) -> Result<u32> {
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
let opts = Dictionary::from(opts);
let mut err = nvim::Error::new();
let channel_id =
unsafe { nvim_open_term(buffer.0, opts.non_owning(), &mut err) };
let channel_id = unsafe {
nvim_open_term(
buffer.0,
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
opts.non_owning(),
#[cfg(feature = "neovim-nightly")]
opts,
&mut err,
)
};
choose!(
err,
match channel_id {
Expand Down Expand Up @@ -844,14 +861,18 @@ pub fn select_popupmenu_item(
finish: bool,
opts: &SelectPopupMenuItemOpts,
) -> Result<()> {
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
let opts = Dictionary::from(opts);
let mut err = nvim::Error::new();
unsafe {
nvim_select_popupmenu_item(
item.try_into()?,
insert,
finish,
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
opts.non_owning(),
#[cfg(feature = "neovim-nightly")]
opts,
&mut err,
)
};
Expand Down
Loading

0 comments on commit a67571c

Please sign in to comment.