Skip to content

Commit

Permalink
refactor: derive OptsBuilder for ExecAutocmdsOpts on nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Dec 10, 2023
1 parent 8b6f37b commit 33d3f3a
Showing 1 changed file with 50 additions and 58 deletions.
108 changes: 50 additions & 58 deletions crates/oxi-api/src/opts/exec_autocmds.rs
Original file line number Diff line number Diff line change
@@ -1,80 +1,88 @@
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 {
/// <modeline><patterns><buffer><group><data>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<types::Object>",
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 {
ExecAutocmdsOptsBuilder::default()
}
}

#[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<Object>) -> &mut Self {
pub fn data(&mut self, any: impl Into<types::Object>) -> &mut Self {
self.0.data = any.into();
#[cfg(feature = "neovim-nightly")]
{
self.0.mask |= 0b11;
}
self
}

Expand All @@ -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
}

Expand All @@ -115,10 +111,6 @@ impl ExecAutocmdsOptsBuilder {
Patterns: StringOrListOfStrings,
{
self.0.patterns = patterns.to_object();
#[cfg(feature = "neovim-nightly")]
{
self.0.mask |= 0b10001;
}
self
}

Expand Down

0 comments on commit 33d3f3a

Please sign in to comment.