Skip to content

Commit

Permalink
refactor: use OptsBuilder in CreateAutocmdOpts on nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Dec 10, 2023
1 parent a67571c commit 45e2ad2
Showing 1 changed file with 79 additions and 97 deletions.
176 changes: 79 additions & 97 deletions crates/oxi-api/src/opts/create_autocmd.rs
Original file line number Diff line number Diff line change
@@ -1,84 +1,110 @@
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;
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 {
/// <callback><pattern><command><nested><buffer><group><once><desc>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<types::Function<AutocmdCallbackArgs, ShouldDeleteAutocmd>>"#,
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<types::String>",
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<types::String>",
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<Item = &'a str>",
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 {
CreateAutocmdOptsBuilder::default()
}
}

#[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
}

Expand All @@ -87,13 +113,9 @@ impl CreateAutocmdOptsBuilder {
#[inline]
pub fn callback<F>(&mut self, callback: F) -> &mut Self
where
F: Into<Function<AutocmdCallbackArgs, ShouldDeleteAutocmd>>,
F: Into<types::Function<AutocmdCallbackArgs, ShouldDeleteAutocmd>>,
{
self.0.callback = callback.into().into();
#[cfg(feature = "neovim-nightly")]
{
self.0.mask |= 0b100000001;
}
self
}

Expand All @@ -102,35 +124,19 @@ impl CreateAutocmdOptsBuilder {
#[inline]
pub fn command<S>(&mut self, command: S) -> &mut Self
where
S: Into<nvim::String>,
S: Into<types::String>,
{
#[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
}

/// Description of the autocommand.
#[inline]
pub fn desc<S>(&mut self, desc: S) -> &mut Self
where
S: Into<nvim::String>,
S: Into<types::String>,
{
#[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
}

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

Expand All @@ -184,11 +170,7 @@ impl CreateAutocmdOptsBuilder {
where
I: IntoIterator<Item = &'a str>,
{
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
}

Expand Down

0 comments on commit 45e2ad2

Please sign in to comment.