Skip to content

Commit

Permalink
refactor: derive OptsBuilder for SetKeymapOpts on nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Dec 10, 2023
1 parent bc91273 commit 5d3d7f9
Showing 1 changed file with 66 additions and 115 deletions.
181 changes: 66 additions & 115 deletions crates/oxi-api/src/opts/set_keymap.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,89 @@
use oxi_types as nvim;
use oxi_types as types;
#[cfg(not(feature = "neovim-nightly"))]
use oxi_types::Object;
#[cfg(feature = "neovim-nightly")]
use oxi_types::{Boolean, LuaRef};

use crate::ToFunction;

/// Options passed to [`Buffer::set_keymap()`](crate::Buffer::set_keymap)
/// and [`set_keymap()`](crate::set_keymap).
#[cfg(not(feature = "neovim-nightly"))]
#[derive(Clone, Debug, Default, PartialEq)]
#[repr(C)]
pub struct SetKeymapOpts {
desc: Object,
expr: Object,
script: Object,
silent: Object,
unique: Object,
nowait: Object,
noremap: Object,
callback: Object,
replace_keycodes: Object,
}

/// Options passed to [`Buffer::set_keymap()`](crate::Buffer::set_keymap)
/// and [`set_keymap()`](crate::set_keymap).
#[cfg(feature = "neovim-nightly")]
#[derive(Clone, Debug, Default, PartialEq)]
#[derive(Clone, Debug, Default, PartialEq, oxi_macros::OptsBuilder)]
#[repr(C)]
pub struct SetKeymapOpts {
#[builder(mask)]
mask: u64,

/// 7th in the mask.
/// Whether the right-hand side of the mapping shouldn't be remappable.
#[builder(argtype = "bool")]
noremap: Boolean,

/// 6th in the mask.
/// For buffer-local mappings, whether Neovim should wait for more
/// characters to be typed if there's a global mapping that could also
/// match. See `:h map-nowait` for more details.
#[builder(argtype = "bool")]
nowait: Boolean,

/// 4th in the mask.
/// Whether the keymap should be silent.
#[builder(argtype = "bool")]
silent: Boolean,

/// 3rd in the mask.
/// Whether to remap characters in the right-hand side by expanding the
/// `<sid>` script tag.
#[builder(argtype = "bool")]
script: Boolean,

/// 2nd in the mask.
/// Whether the keymap argument is an expression.
#[builder(argtype = "bool")]
expr: Boolean,

/// 5th in the mask.
/// If `true` setting the keymap fill fail if another keymap with the same
/// left-hand side already exists.
#[builder(argtype = "bool")]
unique: Boolean,

/// 8th in the mask.
/// A function to call when the mapping is executed.
#[builder(
generics = "F: ToFunction<(), ()>",
argtype = "F",
inline = "{0}.into_luaref()"
)]
callback: LuaRef,

/// 1st in the mask.
desc: nvim::String,
/// A description for the keymap.
#[builder(
generics = "D: Into<types::String>",
argtype = "D",
inline = "{0}.into()"
)]
desc: types::String,

/// 9th in the mask.
/// When [`expr`](SetKeymapOptsBuilder::expr) is `true`, this option can be
/// used to replace the keycodes in the resulting string (see
/// [replace_termcodes()](crate::replace_termcodes)).
#[builder(argtype = "bool")]
replace_keycodes: Boolean,
}

/// Options passed to [`Buffer::set_keymap()`](crate::Buffer::set_keymap)
/// and [`set_keymap()`](crate::set_keymap).
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
#[derive(Clone, Debug, Default, PartialEq)]
#[repr(C)]
pub struct SetKeymapOpts {
desc: Object,
expr: Object,
script: Object,
silent: Object,
unique: Object,
nowait: Object,
noremap: Object,
callback: Object,
replace_keycodes: Object,
}

#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
impl SetKeymapOpts {
#[inline(always)]
/// Creates a new [`SetKeymapOptsBuilder`].
Expand All @@ -67,9 +92,11 @@ impl SetKeymapOpts {
}
}

#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
#[derive(Clone, Default)]
pub struct SetKeymapOptsBuilder(SetKeymapOpts);

#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
impl SetKeymapOptsBuilder {
/// A function to call when the mapping is executed.
#[inline]
Expand All @@ -78,65 +105,29 @@ impl SetKeymapOptsBuilder {
F: ToFunction<(), ()>,
{
let callback = fun.into_luaref();

#[cfg(not(feature = "neovim-nightly"))]
{
self.0.callback = Object::from_luaref(callback);
}
#[cfg(feature = "neovim-nightly")]
{
self.0.callback = callback;
self.0.mask |= 0b100000001;
}

self.0.callback = Object::from_luaref(callback);
self
}

/// A description for the keymap.
#[inline]
pub fn desc(&mut self, desc: &str) -> &mut Self {
let desc = nvim::String::from(desc);

#[cfg(not(feature = "neovim-nightly"))]
{
self.0.desc = desc.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.desc = desc;
self.0.mask |= 0b11;
}

let desc = types::String::from(desc);
self.0.desc = desc.into();
self
}

/// Whether the keymap argument is an expression.
#[inline]
pub fn expr(&mut self, expr: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.expr = expr.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.expr = expr;
self.0.mask |= 0b101;
}
self.0.expr = expr.into();
self
}

/// Whether the right-hand side of the mapping shouldn't be remappable.
#[inline]
pub fn noremap(&mut self, noremap: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.noremap = noremap.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.noremap = noremap;
self.0.mask |= 0b10000001;
}
self.0.noremap = noremap.into();
self
}

Expand All @@ -145,15 +136,7 @@ impl SetKeymapOptsBuilder {
/// match. See `:h map-nowait` for more details.
#[inline]
pub fn nowait(&mut self, nowait: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.nowait = nowait.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.nowait = nowait;
self.0.mask |= 0b1000001;
}
self.0.nowait = nowait.into();
self
}

Expand All @@ -162,62 +145,30 @@ impl SetKeymapOptsBuilder {
/// [replace_termcodes()](crate::replace_termcodes)).
#[inline]
pub fn replace_keycodes(&mut self, replace_keycodes: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.replace_keycodes = replace_keycodes.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.replace_keycodes = replace_keycodes;
self.0.mask |= 0b1000000001;
}
self.0.replace_keycodes = replace_keycodes.into();
self
}

/// Whether to remap characters in the right-hand side by expanding the
/// `<sid>` script tag.
#[inline]
pub fn script(&mut self, script: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.script = script.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.script = script;
self.0.mask |= 0b1001;
}
self.0.script = script.into();
self
}

/// Whether the keymap should be silent.
#[inline]
pub fn silent(&mut self, silent: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.silent = silent.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.silent = silent;
self.0.mask |= 0b10001;
}
self.0.silent = silent.into();
self
}

/// If `true` setting the keymap fill fail if another keymap with the same
/// left-hand side already exists.
#[inline]
pub fn unique(&mut self, unique: bool) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.unique = unique.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.unique = unique;
self.0.mask |= 0b100001;
}
self.0.unique = unique.into();
self
}

Expand Down

0 comments on commit 5d3d7f9

Please sign in to comment.