From 8b6f37b7f9771a7478555cf49353f267e3810280 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Sun, 10 Dec 2023 23:07:21 +0100 Subject: [PATCH] refactor: derive `OptsBuilder` for `CreateCommandOpts` on nightly --- crates/oxi-api/src/opts/create_command.rs | 218 ++++++++-------------- 1 file changed, 83 insertions(+), 135 deletions(-) diff --git a/crates/oxi-api/src/opts/create_command.rs b/crates/oxi-api/src/opts/create_command.rs index 991789df..2b4d555f 100644 --- a/crates/oxi-api/src/opts/create_command.rs +++ b/crates/oxi-api/src/opts/create_command.rs @@ -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, @@ -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", + 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", + 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, Option), 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`]. @@ -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) -> &mut Self { + pub fn count(&mut self, count: impl Into) -> &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) -> &mut Self { + pub fn desc>(&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(&mut self, fun: F) -> &mut Self where - F: Into, Option), u8>>, + F: Into< + types::Function<(CommandArgs, Option, Option), 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 }