From 33d3f3a65245c54479bc069f01db642b15e92993 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Sun, 10 Dec 2023 23:15:54 +0100 Subject: [PATCH] refactor: derive `OptsBuilder` for `ExecAutocmdsOpts` on nightly --- crates/oxi-api/src/opts/exec_autocmds.rs | 108 +++++++++++------------ 1 file changed, 50 insertions(+), 58 deletions(-) diff --git a/crates/oxi-api/src/opts/exec_autocmds.rs b/crates/oxi-api/src/opts/exec_autocmds.rs index 9b724175..ea039b90 100644 --- a/crates/oxi-api/src/opts/exec_autocmds.rs +++ b/crates/oxi-api/src/opts/exec_autocmds.rs @@ -1,46 +1,64 @@ -use oxi_types::Object; -#[cfg(feature = "neovim-nightly")] -use oxi_types::{Boolean, BufHandle}; +use oxi_types as types; use crate::Buffer; use crate::{StringOrInt, StringOrListOfStrings}; -/// Options passed to [`exec_autocmds()`](crate::exec_autocmds). -#[cfg(not(feature = "neovim-nightly"))] -#[derive(Clone, Debug, Default)] -#[repr(C)] -pub struct ExecAutocmdsOpts { - data: Object, - group: Object, - buffer: Object, - patterns: Object, - modeline: Object, -} - /// Options passed to [`exec_autocmds()`](crate::exec_autocmds). #[cfg(feature = "neovim-nightly")] -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug, Default, oxi_macros::OptsBuilder)] #[repr(C)] pub struct ExecAutocmdsOpts { - /// 1 + #[builder(mask)] mask: u64, - /// 3rd in the mask. - buffer: BufHandle, + /// A specific [`Buffer`] for buffer-local autocommands. Cannot be used + /// together with [`patterns`](ExecAutocmdsOptsBuilder::patterns). + #[builder(argtype = "Buffer", inline = "{0}.0")] + buffer: types::BufHandle, - /// 2nd in the mask. - group: Object, + /// The autocommand group name or id to match against. + #[builder( + generics = "G: StringOrInt", + argtype = "G", + inline = "{0}.to_object()" + )] + group: types::Object, - /// 5th in the mask. - modeline: Boolean, + /// Whether to process the modeline after the autocommands. + #[builder(argtype = "bool")] + modeline: types::Boolean, - /// 4th in the mask. - patterns: Object, + /// Patterns to match against. Cannot be used together with + /// [`buffer`](ExecAutocmdsOptsBuilder::buffer). + #[builder( + generics = "P: StringOrListOfStrings", + method = "patterns", + argtype = "P", + inline = "{0}.to_object()" + )] + pattern: types::Object, + + #[builder( + generics = "D: Into", + argtype = "D", + inline = "{0}.into()" + )] + data: types::Object, +} - /// 1st in the mask. - data: Object, +/// Options passed to [`exec_autocmds()`](crate::exec_autocmds). +#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))] +#[derive(Clone, Debug, Default)] +#[repr(C)] +pub struct ExecAutocmdsOpts { + data: types::Object, + group: types::Object, + buffer: types::Object, + patterns: types::Object, + modeline: types::Object, } +#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))] impl ExecAutocmdsOpts { #[inline(always)] pub fn builder() -> ExecAutocmdsOptsBuilder { @@ -48,33 +66,23 @@ impl ExecAutocmdsOpts { } } +#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))] #[derive(Clone, Default)] pub struct ExecAutocmdsOptsBuilder(ExecAutocmdsOpts); +#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))] impl ExecAutocmdsOptsBuilder { /// A specific [`Buffer`] for buffer-local autocommands. Cannot be used /// together with [`patterns`](ExecAutocmdsOptsBuilder::patterns). #[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 |= 0b1001; - } + self.0.buffer = buffer.into(); self } #[inline] - pub fn data(&mut self, any: impl Into) -> &mut Self { + pub fn data(&mut self, any: impl Into) -> &mut Self { self.0.data = any.into(); - #[cfg(feature = "neovim-nightly")] - { - self.0.mask |= 0b11; - } self } @@ -85,25 +93,13 @@ impl ExecAutocmdsOptsBuilder { Grp: StringOrInt, { self.0.group = group.to_object(); - #[cfg(feature = "neovim-nightly")] - { - self.0.mask |= 0b101; - } self } /// Whether to process the modeline after the autocommands. #[inline] pub fn modeline(&mut self, modeline: bool) -> &mut Self { - #[cfg(not(feature = "neovim-nightly"))] - { - self.0.modeline = modeline.into(); - } - #[cfg(feature = "neovim-nightly")] - { - self.0.modeline = modeline; - self.0.mask |= 0b100001; - } + self.0.modeline = modeline.into(); self } @@ -115,10 +111,6 @@ impl ExecAutocmdsOptsBuilder { Patterns: StringOrListOfStrings, { self.0.patterns = patterns.to_object(); - #[cfg(feature = "neovim-nightly")] - { - self.0.mask |= 0b10001; - } self }