diff --git a/crates/oxi-api/src/opts/set_keymap.rs b/crates/oxi-api/src/opts/set_keymap.rs index 428fdbc8..f2563482 100644 --- a/crates/oxi-api/src/opts/set_keymap.rs +++ b/crates/oxi-api/src/opts/set_keymap.rs @@ -1,4 +1,4 @@ -use oxi_types as nvim; +use oxi_types as types; #[cfg(not(feature = "neovim-nightly"))] use oxi_types::Object; #[cfg(feature = "neovim-nightly")] @@ -6,59 +6,84 @@ 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 + /// `` 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", + 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`]. @@ -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] @@ -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 } @@ -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 } @@ -162,15 +145,7 @@ 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 } @@ -178,30 +153,14 @@ impl SetKeymapOptsBuilder { /// `` 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 } @@ -209,15 +168,7 @@ impl SetKeymapOptsBuilder { /// 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 }