Skip to content

Commit

Permalink
api: update SetKeymapOpts for nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 14, 2023
1 parent e6f74a2 commit e3a30b2
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 36 deletions.
11 changes: 6 additions & 5 deletions crates/oxi-api/src/opts/buf_attach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl BufAttachOptsBuilder {
where
F: ToFunction<OnBytesArgs, ShouldDetach>,
{
self.0.on_bytes = on_bytes.to_object();
self.0.on_bytes = Object::from_luaref(on_bytes.into_luaref());
self
}

Expand All @@ -124,7 +124,8 @@ impl BufAttachOptsBuilder {
where
F: ToFunction<OnChangedtickArgs, ShouldDetach>,
{
self.0.on_changedtick = on_changedtick.to_object();
self.0.on_changedtick =
Object::from_luaref(on_changedtick.into_luaref());
self
}

Expand All @@ -134,7 +135,7 @@ impl BufAttachOptsBuilder {
where
F: ToFunction<OnDetachArgs, ShouldDetach>,
{
self.0.on_detach = on_detach.to_object();
self.0.on_detach = Object::from_luaref(on_detach.into_luaref());
self
}

Expand All @@ -144,7 +145,7 @@ impl BufAttachOptsBuilder {
where
F: ToFunction<OnLinesArgs, ShouldDetach>,
{
self.0.on_lines = fun.to_object();
self.0.on_lines = Object::from_luaref(fun.into_luaref());
self
}

Expand All @@ -155,7 +156,7 @@ impl BufAttachOptsBuilder {
where
F: ToFunction<OnReloadArgs, ShouldDetach>,
{
self.0.on_reload = on_reload.to_object();
self.0.on_reload = Object::from_luaref(on_reload.into_luaref());
self
}

Expand Down
10 changes: 5 additions & 5 deletions crates/oxi-api/src/opts/decoration_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl DecorationProviderOptsBuilder {
where
F: ToFunction<OnBufArgs, ()>,
{
self.0.on_buf = fun.to_object();
self.0.on_buf = Object::from_luaref(fun.into_luaref());
self
}

Expand All @@ -109,7 +109,7 @@ impl DecorationProviderOptsBuilder {
where
F: ToFunction<OnEndArgs, ()>,
{
self.0.on_end = fun.to_object();
self.0.on_end = Object::from_luaref(fun.into_luaref());
self
}

Expand All @@ -118,7 +118,7 @@ impl DecorationProviderOptsBuilder {
where
F: ToFunction<OnLineArgs, ()>,
{
self.0.on_line = fun.to_object();
self.0.on_line = Object::from_luaref(fun.into_luaref());
self
}

Expand All @@ -127,7 +127,7 @@ impl DecorationProviderOptsBuilder {
where
F: ToFunction<OnStartArgs, DontSkipRedrawCycle>,
{
self.0.on_start = fun.to_object();
self.0.on_start = Object::from_luaref(fun.into_luaref());
self
}

Expand All @@ -136,7 +136,7 @@ impl DecorationProviderOptsBuilder {
where
F: ToFunction<OnWinArgs, DontSkipOnLines>,
{
self.0.on_win = fun.to_object();
self.0.on_win = Object::from_luaref(fun.into_luaref());
self
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxi-api/src/opts/open_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl OpenTermOptsBuilder {
where
F: ToFunction<OnInputArgs, ()>,
{
self.0.on_input = fun.to_object();
self.0.on_input = Object::from_luaref(fun.into_luaref());
self
}

Expand Down
139 changes: 120 additions & 19 deletions crates/oxi-api/src/opts/set_keymap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use oxi_types::{self as nvim, Object};
use oxi_types as nvim;
#[cfg(not(feature = "neovim-nightly"))]
use oxi_types::Object;
#[cfg(feature = "neovim-nightly")]
use oxi_types::{Boolean, LuaRef};

use crate::ToFunction;

Expand All @@ -25,15 +29,34 @@ pub struct SetKeymapOpts {
#[derive(Clone, Debug, Default, PartialEq)]
#[repr(C)]
pub struct SetKeymapOpts {
noremap: Object,
nowait: Object,
silent: Object,
script: Object,
expr: Object,
unique: Object,
callback: Object,
desc: Object,
replace_keycodes: Object,
mask: u64,

/// 7th in the mask.
noremap: Boolean,

/// 6th in the mask.
nowait: Boolean,

/// 4th in the mask.
silent: Boolean,

/// 3rd in the mask.
script: Boolean,

/// 2nd in the mask.
expr: Boolean,

/// 5th in the mask.
unique: Boolean,

/// 8th in the mask.
callback: LuaRef,

/// 1st in the mask.
desc: nvim::String,

/// 9th in the mask.
replace_keycodes: Boolean,
}

impl SetKeymapOpts {
Expand All @@ -54,28 +77,66 @@ impl SetKeymapOptsBuilder {
where
F: ToFunction<(), ()>,
{
self.0.callback = fun.to_object();
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
}

/// A description for the keymap.
#[inline]
pub fn desc(&mut self, desc: &str) -> &mut Self {
self.0.desc = nvim::String::from(desc).into();
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;
}

self
}

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

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

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

Expand All @@ -93,30 +162,62 @@ impl SetKeymapOptsBuilder {
/// [replace_termcodes()](crate::replace_termcodes)).
#[inline]
pub fn replace_keycodes(&mut self, replace_keycodes: bool) -> &mut Self {
self.0.replace_keycodes = replace_keycodes.into();
#[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
}

/// 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 {
self.0.script = script.into();
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.script = script.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.script = script;
self.0.mask |= 0b1001;
}
self
}

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

Expand Down
12 changes: 6 additions & 6 deletions crates/oxi-api/src/trait_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::iter::FusedIterator;

use oxi_luajit::{Poppable, Pushable};
use oxi_types::{Array, Function, Object};
use oxi_types::{Array, Function, LuaRef, Object};

/// A super trait of most common traits implemented on iterators.
pub trait SuperIterator<I>:
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<S: Into<String>> StringOrListOfStrings for Vec<S> {

/// A trait implemented by closures and [`Function`]s.
pub trait ToFunction<A, R> {
fn to_object(self) -> Object;
fn into_luaref(self) -> LuaRef;
}

impl<A, R, F> ToFunction<A, R> for F
Expand All @@ -71,15 +71,15 @@ where
F: FnMut(A) -> crate::Result<R> + 'static,
{
#[inline]
fn to_object(self) -> Object {
Function::from_fn_mut(self).into()
fn into_luaref(self) -> LuaRef {
Function::from_fn_mut(self).lua_ref()
}
}

impl<A, R> ToFunction<A, R> for Function<A, R> {
#[inline]
fn to_object(self) -> Object {
self.into()
fn into_luaref(self) -> LuaRef {
self.lua_ref()
}
}

Expand Down

0 comments on commit e3a30b2

Please sign in to comment.