Skip to content

Commit

Permalink
refactor: derive OptsBuilder for CreateCommandOpts on nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Dec 10, 2023
1 parent 45e2ad2 commit 8b6f37b
Showing 1 changed file with 83 additions and 135 deletions.
218 changes: 83 additions & 135 deletions crates/oxi-api/src/opts/create_command.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
#[cfg(feature = "neovim-nightly")]
use oxi_types::Boolean;
use oxi_types::{
self as nvim,
conversion::ToObject,
Function,
Integer,
Object,
};
use oxi_types as types;
use types::conversion::ToObject;

use crate::types::{
CommandAddr,
Expand All @@ -17,71 +10,90 @@ use crate::types::{
};
use crate::Buffer;

/// Options passed to
/// [`Buffer::create_user_command()`](crate::Buffer::create_user_command).
#[cfg(not(feature = "neovim-nightly"))]
#[derive(Clone, Debug, Default)]
#[repr(C)]
pub struct CreateCommandOpts {
bar: Object,
addr: Object,
bang: Object,
desc: Object,
count: Object,
force: Object,
nargs: Object,
range: Object,
preview: Object,
complete: Object,
register_: Object,
keepscript: Object,
}

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

/// 2nd in the mask.
addr: Object,

/// 3rd in the mask.
bang: Boolean,
#[builder(argtype = "CommandAddr", inline = "{0}.to_object().unwrap()")]
addr: types::Object,

/// 1st in the mask.
bar: Boolean,
#[builder(argtype = "bool")]
bang: types::Boolean,

/// 10th in the mask.
complete: Object,
#[builder(argtype = "bool")]
bar: types::Boolean,

/// 5th in the mask.
count: Object,
#[builder(
argtype = "CommandComplete",
inline = "{0}.to_object().unwrap()"
)]
complete: types::Object,

/// 4th in the mask.
desc: Object,
// TODO: fix `builder(Into)`.
#[builder(
generics = "C: Into<types::Integer>",
argtype = "C",
inline = "{0}.into().into()"
)]
count: types::Object,

/// 6th in the mask.
force: Boolean,

/// 12th in the mask.
keepscript: Boolean,

/// 7th in the mask.
nargs: Object,

/// 9th in the mask.
preview: Object,

/// 8th in the mask.
range: Object,
/// Description for the command.
#[builder(
generics = "C: Into<types::String>",
argtype = "C",
inline = "{0}.into().into()"
)]
desc: types::Object,

#[builder(argtype = "bool")]
force: types::Boolean,

#[builder(argtype = "bool")]
keepscript: types::Boolean,

#[builder(argtype = "CommandNArgs", inline = "{0}.to_object().unwrap()")]
nargs: types::Object,

#[builder(
generics = r#"F: Into<types::Function<(CommandArgs, Option<u32>, Option<Buffer>), u8>>"#,
argtype = "F",
inline = "{0}.into().into()"
)]
preview: types::Object,

#[builder(argtype = "CommandRange", inline = "{0}.to_object().unwrap()")]
range: types::Object,

#[builder(method = "register", argtype = "bool")]
register_: types::Boolean,
}

/// 11th in the mask.
register_: Boolean,
/// Options passed to
/// [`Buffer::create_user_command()`](crate::Buffer::create_user_command).
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
#[derive(Clone, Debug, Default)]
#[repr(C)]
pub struct CreateCommandOpts {
bar: types::Object,
addr: types::Object,
bang: types::Object,
desc: types::Object,
count: types::Object,
force: types::Object,
nargs: types::Object,
range: types::Object,
preview: types::Object,
complete: types::Object,
register_: types::Object,
keepscript: types::Object,
}

#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
impl CreateCommandOpts {
#[inline(always)]
/// Creates a new [`CreateCommandOptsBuilder`].
Expand All @@ -90,151 +102,87 @@ impl CreateCommandOpts {
}
}

#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
#[derive(Clone, Default)]
pub struct CreateCommandOptsBuilder(CreateCommandOpts);

#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
impl CreateCommandOptsBuilder {
#[inline]
pub fn addr(&mut self, addr: CommandAddr) -> &mut Self {
self.0.addr = addr.to_object().unwrap();
#[cfg(feature = "neovim-nightly")]
{
self.0.mask |= 0b101;
}
self
}

#[inline]
pub fn bang(&mut self, bang: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.bang = bang.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.bang = bang;
self.0.mask |= 0b1001;
}
self.0.bang = bang.into();
self
}

#[inline]
pub fn bar(&mut self, bar: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.bar = bar.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.bar = bar;
self.0.mask |= 0b11;
}
self.0.bar = bar.into();
self
}

#[inline]
pub fn complete(&mut self, complete: CommandComplete) -> &mut Self {
self.0.complete = complete.to_object().unwrap();
#[cfg(feature = "neovim-nightly")]
{
self.0.mask |= 0b10000000001;
}
self
}

#[inline]
pub fn count(&mut self, count: impl Into<Integer>) -> &mut Self {
pub fn count(&mut self, count: impl Into<types::Integer>) -> &mut Self {
self.0.count = count.into().into();
#[cfg(feature = "neovim-nightly")]
{
self.0.mask |= 0b100001;
}
self
}

/// Description for the command.
#[inline]
pub fn desc(&mut self, desc: impl Into<nvim::String>) -> &mut Self {
pub fn desc<S: Into<types::String>>(&mut self, desc: S) -> &mut Self {
self.0.desc = desc.into().into();
#[cfg(feature = "neovim-nightly")]
{
self.0.mask |= 0b10001;
}
self
}

#[inline]
pub fn force(&mut self, force: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.force = force.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.force = force;
self.0.mask |= 0b1000001;
}
self.0.force = force.into();
self
}

#[inline]
pub fn keepscript(&mut self, keepscript: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.keepscript = keepscript.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.keepscript = keepscript;
self.0.mask |= 0b1000000000001;
}
self.0.keepscript = keepscript.into();
self
}

#[inline]
pub fn nargs(&mut self, nargs: CommandNArgs) -> &mut Self {
self.0.nargs = nargs.to_object().unwrap();
#[cfg(feature = "neovim-nightly")]
{
self.0.mask |= 0b10000001;
}
self
}

#[inline]
pub fn preview<F>(&mut self, fun: F) -> &mut Self
where
F: Into<Function<(CommandArgs, Option<u32>, Option<Buffer>), u8>>,
F: Into<
types::Function<(CommandArgs, Option<u32>, Option<Buffer>), u8>,
>,
{
self.0.preview = fun.into().into();
#[cfg(feature = "neovim-nightly")]
{
self.0.mask |= 0b1000000001;
}
self
}

#[inline]
pub fn range(&mut self, range: CommandRange) -> &mut Self {
self.0.range = range.to_object().unwrap();
#[cfg(feature = "neovim-nightly")]
{
self.0.mask |= 0b100000001;
}
self
}

#[inline]
pub fn register(&mut self, register: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.register_ = register.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.register_ = register;
self.0.mask |= 0b100000000001;
}
self.0.register_ = register.into();
self
}

Expand Down

0 comments on commit 8b6f37b

Please sign in to comment.