From 45e2ad23bd2c9f33590e4b5bce971a44a693ffe1 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Sun, 10 Dec 2023 22:52:22 +0100 Subject: [PATCH] refactor: use `OptsBuilder` in `CreateAutocmdOpts` on nightly --- crates/oxi-api/src/opts/create_autocmd.rs | 176 ++++++++++------------ 1 file changed, 79 insertions(+), 97 deletions(-) diff --git a/crates/oxi-api/src/opts/create_autocmd.rs b/crates/oxi-api/src/opts/create_autocmd.rs index 5b02bffe..cad073fd 100644 --- a/crates/oxi-api/src/opts/create_autocmd.rs +++ b/crates/oxi-api/src/opts/create_autocmd.rs @@ -1,6 +1,4 @@ -use oxi_types::{self as nvim, Array, Function, Object}; -#[cfg(feature = "neovim-nightly")] -use oxi_types::{Boolean, BufHandle, String as NvimString}; +use oxi_types as types; use crate::types::AutocmdCallbackArgs; use crate::Buffer; @@ -8,54 +6,88 @@ use crate::StringOrInt; pub type ShouldDeleteAutocmd = bool; -/// Options passed to [`create_autocmd()`](crate::create_autocmd). -#[cfg(not(feature = "neovim-nightly"))] -#[derive(Clone, Debug, Default)] -#[repr(C)] -pub struct CreateAutocmdOpts { - desc: Object, - once: Object, - group: Object, - buffer: Object, - nested: Object, - command: Object, - pattern: Object, - callback: Object, -} - /// Options passed to [`create_autocmd()`](crate::create_autocmd). #[cfg(feature = "neovim-nightly")] -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug, Default, oxi_macros::OptsBuilder)] #[repr(C)] pub struct CreateAutocmdOpts { - /// 1 + #[builder(mask)] mask: u64, - /// 4th in the mask. - buffer: BufHandle, + /// A specific `Buffer` for buffer-local autocommands. + #[builder(argtype = "Buffer", inline = "{0}.0")] + buffer: types::BufHandle, + + /// Callback to execute when the autocommand is triggered. Cannot be used + /// together with `command`. + #[builder( + generics = r#"F: Into>"#, + argtype = "F", + inline = "{0}.into().into()" + )] + callback: types::Object, - /// 8th in the mask. - callback: Object, + /// Vim command to execute when the autocommand is triggered. Cannot be + /// used together with `callback`. + // TODO: fix builder(Into). + #[builder( + generics = "S: Into", + argtype = "S", + inline = "{0}.into()" + )] + command: types::String, - /// 6th in the mask. - command: NvimString, + /// Description of the autocommand. + // TODO: fix builder(Into). + #[builder( + generics = "S: Into", + argtype = "S", + inline = "{0}.into()" + )] + desc: types::String, - /// 1st in the mask. - desc: NvimString, + /// The autocommand group name or id to match against. + #[builder( + generics = "G: StringOrInt", + argtype = "G", + inline = "{0}.to_object()" + )] + group: types::Object, - /// 3rd in the mask. - group: Object, + /// Run nested autocommands. + #[builder(argtype = "bool")] + nested: types::Boolean, - /// 5th in the mask. - nested: Boolean, + /// Only run the autocommand once. + #[builder(argtype = "bool")] + once: types::Boolean, - /// 2nd in the mask. - once: Boolean, + /// Patterns to match against. + #[builder( + generics = "'a, I: IntoIterator", + method = "patterns", + argtype = "I", + inline = "types::Array::from_iter({0}).into()" + )] + pattern: types::Object, +} - /// 7th in the mask. - pattern: Object, +/// Options passed to [`create_autocmd()`](crate::create_autocmd). +#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))] +#[derive(Clone, Debug, Default)] +#[repr(C)] +pub struct CreateAutocmdOpts { + desc: types::Object, + once: types::Object, + group: types::Object, + buffer: types::Object, + nested: types::Object, + command: types::Object, + pattern: types::Object, + callback: types::Object, } +#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))] impl CreateAutocmdOpts { #[inline(always)] pub fn builder() -> CreateAutocmdOptsBuilder { @@ -63,22 +95,16 @@ impl CreateAutocmdOpts { } } +#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))] #[derive(Clone, Default)] pub struct CreateAutocmdOptsBuilder(CreateAutocmdOpts); +#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))] impl CreateAutocmdOptsBuilder { /// A specific `Buffer` for buffer-local autocommands. #[inline] pub fn buffer(&mut self, buffer: Buffer) -> &mut Self { - #[cfg(not(feature = "neovim-nightly"))] - { - self.0.buffer = buffer.into(); - } - #[cfg(feature = "neovim-nightly")] - { - self.0.buffer = buffer.0; - self.0.mask |= 0b10001; - } + self.0.buffer = buffer.into(); self } @@ -87,13 +113,9 @@ impl CreateAutocmdOptsBuilder { #[inline] pub fn callback(&mut self, callback: F) -> &mut Self where - F: Into>, + F: Into>, { self.0.callback = callback.into().into(); - #[cfg(feature = "neovim-nightly")] - { - self.0.mask |= 0b100000001; - } self } @@ -102,17 +124,9 @@ impl CreateAutocmdOptsBuilder { #[inline] pub fn command(&mut self, command: S) -> &mut Self where - S: Into, + S: Into, { - #[cfg(not(feature = "neovim-nightly"))] - { - self.0.command = command.into().into(); - } - #[cfg(feature = "neovim-nightly")] - { - self.0.command = command.into(); - self.0.mask |= 0b1000001; - } + self.0.command = command.into().into(); self } @@ -120,17 +134,9 @@ impl CreateAutocmdOptsBuilder { #[inline] pub fn desc(&mut self, desc: S) -> &mut Self where - S: Into, + S: Into, { - #[cfg(not(feature = "neovim-nightly"))] - { - self.0.desc = desc.into().into(); - } - #[cfg(feature = "neovim-nightly")] - { - self.0.desc = desc.into(); - self.0.mask |= 0b11; - } + self.0.desc = desc.into().into(); self } @@ -141,40 +147,20 @@ impl CreateAutocmdOptsBuilder { Grp: StringOrInt, { self.0.group = group.to_object(); - #[cfg(feature = "neovim-nightly")] - { - self.0.mask |= 0b1001; - } self } /// Run nested autocommands. #[inline] pub fn nested(&mut self, nested: bool) -> &mut Self { - #[cfg(not(feature = "neovim-nightly"))] - { - self.0.nested = nested.into(); - } - #[cfg(feature = "neovim-nightly")] - { - self.0.nested = nested; - self.0.mask |= 0b100001; - } + self.0.nested = nested.into(); self } /// Only run the autocommand once. #[inline] pub fn once(&mut self, once: bool) -> &mut Self { - #[cfg(not(feature = "neovim-nightly"))] - { - self.0.once = once.into(); - } - #[cfg(feature = "neovim-nightly")] - { - self.0.once = once; - self.0.mask |= 0b101; - } + self.0.once = once.into(); self } @@ -184,11 +170,7 @@ impl CreateAutocmdOptsBuilder { where I: IntoIterator, { - self.0.pattern = Array::from_iter(patterns).into(); - #[cfg(feature = "neovim-nightly")] - { - self.0.mask |= 0b10000001; - } + self.0.pattern = types::Array::from_iter(patterns).into(); self }