💅 remove Option from all api functions

This commit is contained in:
noib3 2022-10-02 01:48:23 +02:00
parent ceb753aef3
commit d67a61dbae
No known key found for this signature in database
GPG key ID: 7AF92216C504A017
13 changed files with 103 additions and 90 deletions

View file

@ -2,7 +2,7 @@ use thiserror::Error as ThisError;
pub(crate) type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Eq, PartialEq, ThisError)]
#[derive(Clone, Debug, Eq, PartialEq, ThisError)]
pub enum Error {
#[error("Couldn't create async handle")]
CouldntCreateAsyncHandle,

View file

@ -22,11 +22,8 @@ pub fn clear_autocmds(opts: &ClearAutocmdsOpts) -> Result<()> {
/// an existing augroup set the
/// [`clear`](super::opts::CreateAugroupOptsBuilder::clear) field of `opts` to
/// `false`.
pub fn create_augroup(
name: &str,
opts: Option<&CreateAugroupOpts>,
) -> Result<u32> {
let opts = opts.map(KeyDict_create_augroup::from).unwrap_or_default();
pub fn create_augroup(name: &str, opts: &CreateAugroupOpts) -> Result<u32> {
let opts = KeyDict_create_augroup::from(opts);
let name = nvim::String::from(name);
let mut err = nvim::Error::new();
let id = unsafe {
@ -111,9 +108,9 @@ where
/// events are provided, it will find all the autocommands that match any
/// combination of them.
pub fn get_autocmds(
opts: Option<&GetAutocmdsOpts>,
opts: &GetAutocmdsOpts,
) -> Result<impl SuperIterator<AutocmdInfos>> {
let opts = opts.map(KeyDict_get_autocmds::from).unwrap_or_default();
let opts = KeyDict_get_autocmds::from(opts);
let mut err = nvim::Error::new();
let infos = unsafe { nvim_get_autocmds(&opts, &mut err) };
err.into_err_or_else(|| {

View file

@ -144,12 +144,12 @@ impl Buffer {
&mut self,
name: &str,
command: Cmd,
opts: Option<&CreateCommandOpts>,
opts: &CreateCommandOpts,
) -> Result<()>
where
Cmd: StringOrFunction<CommandArgs, ()>,
{
let opts = opts.map(KeyDict_user_command::from).unwrap_or_default();
let opts = KeyDict_user_command::from(opts);
let mut err = nvim::Error::new();
let name = nvim::String::from(name);
let command = command.to_obj();
@ -226,9 +226,9 @@ impl Buffer {
///
/// Deletes the buffer (not allowed while
/// [`textlock`](https://neovim.io/doc/user/eval.html#textlock) is active).
pub fn delete(self, opts: Option<&BufDeleteOpts>) -> Result<()> {
pub fn delete(self, opts: &BufDeleteOpts) -> Result<()> {
let mut err = nvim::Error::new();
let opts = opts.map(Dictionary::from).unwrap_or_default();
let opts = Dictionary::from(opts);
unsafe { nvim_buf_delete(self.0, opts.non_owning(), &mut err) };
err.into_err_or_else(|| ())
}
@ -243,10 +243,10 @@ impl Buffer {
/// Binding to [`nvim_buf_get_commands`](https://neovim.io/doc/user/api.html#nvim_buf_get_commands()).
pub fn get_commands(
&self,
opts: Option<&GetCommandsOpts>,
opts: &GetCommandsOpts,
) -> Result<impl SuperIterator<CommandInfos>> {
let mut err = nvim::Error::new();
let opts = opts.map(KeyDict_get_commands::from).unwrap_or_default();
let opts = KeyDict_get_commands::from(opts);
let cmds = unsafe { nvim_buf_get_commands(self.0, &opts, &mut err) };
err.into_err_or_else(|| {
cmds.into_iter()
@ -366,10 +366,10 @@ impl Buffer {
start_col: usize,
end_row: usize,
end_col: usize,
opts: Option<&GetTextOpts>,
opts: &GetTextOpts,
) -> Result<impl SuperIterator<nvim::String>> {
let mut err = nvim::Error::new();
let opts = opts.map(Dictionary::from).unwrap_or_default();
let opts = Dictionary::from(opts);
let lines = unsafe {
nvim_buf_get_text(
LUA_INTERNAL_CALL,
@ -433,12 +433,12 @@ impl Buffer {
mode: Mode,
lhs: &str,
rhs: &str,
opts: Option<&SetKeymapOpts>,
opts: &SetKeymapOpts,
) -> Result<()> {
let mode = nvim::String::from(mode);
let lhs = nvim::String::from(lhs);
let rhs = nvim::String::from(rhs);
let opts = opts.map(KeyDict_keymap::from).unwrap_or_default();
let opts = KeyDict_keymap::from(opts);
let mut err = nvim::Error::new();
unsafe {
nvim_buf_set_keymap(

View file

@ -100,9 +100,9 @@ impl Buffer {
&self,
ns_id: u32,
extmark_id: u32,
opts: Option<&GetExtmarkByIdOpts>,
opts: &GetExtmarkByIdOpts,
) -> Result<(usize, usize, Option<ExtmarkInfos>)> {
let opts = opts.map(Dictionary::from).unwrap_or_default();
let opts = Dictionary::from(opts);
let mut err = nvim::Error::new();
let tuple = unsafe {
nvim_buf_get_extmark_by_id(
@ -141,10 +141,10 @@ impl Buffer {
ns_id: u32,
start: ExtmarkPosition,
end: ExtmarkPosition,
opts: Option<&GetExtmarksOpts>,
opts: &GetExtmarksOpts,
) -> Result<impl SuperIterator<(u32, usize, usize, Option<ExtmarkInfos>)>>
{
let opts = opts.map(Dictionary::from).unwrap_or_default();
let opts = Dictionary::from(opts);
let mut err = nvim::Error::new();
let extmarks = unsafe {
nvim_buf_get_extmarks(

View file

@ -44,14 +44,14 @@ pub fn create_buf(is_listed: bool, is_scratch: bool) -> Result<Buffer> {
pub fn create_user_command<Cmd>(
name: &str,
command: Cmd,
opts: Option<&CreateCommandOpts>,
opts: &CreateCommandOpts,
) -> Result<()>
where
Cmd: StringOrFunction<CommandArgs, ()>,
{
let name = nvim::String::from(name);
let command = command.to_obj();
let opts = opts.map(KeyDict_user_command::from).unwrap_or_default();
let opts = KeyDict_user_command::from(opts);
let mut err = nvim::Error::new();
unsafe {
nvim_create_user_command(
@ -176,10 +176,10 @@ pub fn err_writeln(str: &str) {
/// Evaluates a string to be displayed in the statusline.
pub fn eval_statusline(
str: &str,
opts: Option<&EvalStatuslineOpts>,
opts: &EvalStatuslineOpts,
) -> Result<StatuslineInfos> {
let str = nvim::String::from(str);
let opts = opts.map(KeyDict_eval_statusline::from).unwrap_or_default();
let opts = KeyDict_eval_statusline::from(opts);
let mut err = nvim::Error::new();
let dict =
unsafe { nvim_eval_statusline(str.non_owning(), &opts, &mut err) };
@ -242,9 +242,9 @@ pub fn get_color_map() -> impl SuperIterator<(String, u32)> {
/// Returns an iterator over the infos of the global ex commands. Only
/// user-defined commands are returned, not builtin ones.
pub fn get_commands(
opts: Option<&GetCommandsOpts>,
opts: &GetCommandsOpts,
) -> Result<impl SuperIterator<CommandInfos>> {
let opts = opts.map(KeyDict_get_commands::from).unwrap_or_default();
let opts = KeyDict_get_commands::from(opts);
let mut err = nvim::Error::new();
let cmds = unsafe { nvim_get_commands(&opts, &mut err) };
err.into_err_or_else(|| {
@ -255,8 +255,8 @@ pub fn get_commands(
/// Binding to [`nvim_get_context`](https://neovim.io/doc/user/api.html#nvim_get_context()).
///
/// Returns a snapshot of the current editor state.
pub fn get_context(opts: Option<&GetContextOpts>) -> Result<EditorContext> {
let opts = opts.map(KeyDict_context::from).unwrap_or_default();
pub fn get_context(opts: &GetContextOpts) -> Result<EditorContext> {
let opts = KeyDict_context::from(opts);
let mut err = nvim::Error::new();
let ctx = unsafe { nvim_get_context(&opts, &mut err) };
err.into_err_or_flatten(|| Ok(EditorContext::from_obj(ctx.into())?))
@ -336,10 +336,10 @@ pub fn get_keymap(mode: Mode) -> impl SuperIterator<KeymapInfos> {
/// of the named mark. Marks are (1,0)-indexed.
pub fn get_mark(
name: char,
opts: Option<&GetMarkOpts>,
opts: &GetMarkOpts,
) -> Result<(usize, usize, Buffer, String)> {
let name = nvim::String::from(name);
let opts = opts.map(Dictionary::from).unwrap_or_default();
let opts = Dictionary::from(opts);
let mut err = nvim::Error::new();
let mark = unsafe {
nvim_get_mark(name.non_owning(), opts.non_owning(), &mut err)
@ -394,15 +394,12 @@ pub fn get_option_info(name: &str) -> Result<OptionInfos> {
///
/// To get a buffer-local orr window-local option for a specific buffer of
/// window consider using [`Buffer::get_option`] or [`Window::get_option`] instead.
pub fn get_option_value<Opt>(
name: &str,
opts: Option<&OptionValueOpts>,
) -> Result<Opt>
pub fn get_option_value<Opt>(name: &str, opts: &OptionValueOpts) -> Result<Opt>
where
Opt: FromObject,
{
let name = nvim::String::from(name);
let opts = opts.map(KeyDict_option::from).unwrap_or_default();
let opts = KeyDict_option::from(opts);
let mut err = nvim::Error::new();
let obj =
unsafe { nvim_get_option_value(name.non_owning(), &opts, &mut err) };
@ -589,10 +586,10 @@ pub fn load_context(ctx: EditorContext) {
pub fn notify(
msg: &str,
log_level: LogLevel,
opts: Option<&NotifyOpts>,
opts: &NotifyOpts,
) -> Result<()> {
let msg = nvim::String::from(msg);
let opts = opts.map(Dictionary::from).unwrap_or_default();
let opts = Dictionary::from(opts);
let mut err = nvim::Error::new();
let _ = unsafe {
nvim_notify(
@ -610,8 +607,8 @@ pub fn notify(
/// Opens a terminal instance in a buffer. Returns the id of a channel that can
/// be used to send data to the instance via
/// [`nvim_oxi::api::chan_send`](chan_send).
pub fn open_term(buffer: &Buffer, opts: Option<&OpenTermOpts>) -> Result<u32> {
let opts = opts.map(Dictionary::from).unwrap_or_default();
pub fn open_term(buffer: &Buffer, opts: &OpenTermOpts) -> Result<u32> {
let opts = Dictionary::from(opts);
let mut err = nvim::Error::new();
let channel_id =
unsafe { nvim_open_term(buffer.0, opts.non_owning(), &mut err) };
@ -701,9 +698,9 @@ pub fn select_popupmenu_item(
item: usize,
insert: bool,
finish: bool,
opts: Option<&SelectPopupMenuItemOpts>,
opts: &SelectPopupMenuItemOpts,
) -> Result<()> {
let opts = opts.map(Dictionary::from).unwrap_or_default();
let opts = Dictionary::from(opts);
let mut err = nvim::Error::new();
unsafe {
nvim_select_popupmenu_item(
@ -772,13 +769,9 @@ pub fn set_current_win(win: &Window) -> Result<()> {
/// Binding to [`nvim_set_hl`](https://neovim.io/doc/user/api.html#nvim_set_hl()).
///
/// Sets a highlight group.
pub fn set_hl(
ns_id: u32,
name: &str,
opts: Option<&SetHighlightOpts>,
) -> Result<()> {
pub fn set_hl(ns_id: u32, name: &str, opts: &SetHighlightOpts) -> Result<()> {
let name = nvim::String::from(name);
let opts = opts.map(KeyDict_highlight::from).unwrap_or_default();
let opts = KeyDict_highlight::from(opts);
let mut err = nvim::Error::new();
unsafe {
nvim_set_hl(ns_id as Integer, name.non_owning(), &opts, &mut err)
@ -794,12 +787,12 @@ pub fn set_keymap(
mode: Mode,
lhs: &str,
rhs: &str,
opts: Option<&SetKeymapOpts>,
opts: &SetKeymapOpts,
) -> Result<()> {
let mode = nvim::String::from(mode);
let lhs = nvim::String::from(lhs);
let rhs = nvim::String::from(rhs);
let opts = opts.map(KeyDict_keymap::from).unwrap_or_default();
let opts = KeyDict_keymap::from(opts);
let mut err = nvim::Error::new();
unsafe {
nvim_set_keymap(
@ -843,13 +836,13 @@ where
pub fn set_option_value<Opt>(
name: &str,
value: Opt,
opts: Option<&OptionValueOpts>,
opts: &OptionValueOpts,
) -> Result<()>
where
Opt: ToObject,
{
let name = nvim::String::from(name);
let opts = opts.map(KeyDict_option::from).unwrap_or_default();
let opts = KeyDict_option::from(opts);
let mut err = nvim::Error::new();
unsafe {
nvim_set_option_value(

View file

@ -62,10 +62,9 @@ where
)]
pub fn cmd(
infos: &CmdInfos,
opts: Option<&super::opts::CmdOpts>,
opts: &super::opts::CmdOpts,
) -> Result<Option<String>> {
let opts =
opts.map(super::opts::KeyDict_cmd_opts::from).unwrap_or_default();
let opts = super::opts::KeyDict_cmd_opts::from(opts);
let mut err = nvim::Error::new();
let output = unsafe {
nvim_cmd(LUA_INTERNAL_CALL, &infos.into(), &opts.into(), &mut err)
@ -129,10 +128,10 @@ pub fn exec(src: &str, output: bool) -> Result<Option<String>> {
)]
pub fn parse_cmd(
src: &str,
opts: Option<&super::opts::ParseCmdOpts>,
opts: &super::opts::ParseCmdOpts,
) -> Result<CmdInfos> {
let src = nvim::String::from(src);
let opts = opts.map(nvim::Dictionary::from).unwrap_or_default();
let opts = nvim::Dictionary::from(opts);
let mut err = nvim::Error::new();
let dict = unsafe {
nvim_parse_cmd(src.non_owning(), opts.non_owning(), &mut err)

View file

@ -41,7 +41,6 @@ pub mod mlua {
/// Returns a static reference to a
/// [`mlua::Lua`](https://docs.rs/mlua/latest/mlua/struct.Lua.html) object
/// to be able to interact with other Lua plugins.
#[doc(inline)]
pub fn lua() -> &'static mlua::Lua {
unsafe {
crate::lua::with_state(|lstate| {

View file

@ -17,10 +17,10 @@ fn api() -> oxi::Result<Dictionary> {
Ok(())
};
api::create_user_command("Greetings", greetings, Some(&opts))?;
api::create_user_command("Greetings", greetings, &opts)?;
// Remaps `hi` to `hello` in insert mode.
api::set_keymap(Mode::Insert, "hi", "hello", None)?;
api::set_keymap(Mode::Insert, "hi", "hello", &Default::default())?;
// Creates two functions `{open,close}_window` to open and close a
// floating window.

View file

@ -45,11 +45,10 @@ fn clear_autocmds_buffer_n_patterns() {
#[oxi::test]
fn create_augroup() {
let opts = CreateAugroupOpts::builder().build();
let id = api::create_augroup("Foo", Some(&opts))
.expect("create_augroup failed");
let id = api::create_augroup("Foo", &opts).expect("create_augroup failed");
let opts = CreateAugroupOpts::builder().clear(false).build();
let got = api::create_augroup("Foo", Some(&opts));
let got = api::create_augroup("Foo", &opts);
assert_eq!(Ok(id), got);
}
@ -114,19 +113,22 @@ fn exec_autocmds() {
#[oxi::test]
fn get_autocmds() {
let autocmds = api::get_autocmds(None).expect("couldn't get autocmds");
let autocmds =
api::get_autocmds(&Default::default()).expect("couldn't get autocmds");
assert_lt!(0, autocmds.collect::<Vec<_>>().len());
}
#[oxi::test]
fn set_del_augroup_by_id() {
let id = api::create_augroup("Foo", None).expect("create_augroup failed");
let id = api::create_augroup("Foo", &Default::default())
.expect("create_augroup failed");
assert_eq!(Ok(()), api::del_augroup_by_id(id));
}
#[oxi::test]
fn set_del_augroup_by_name() {
let _ = api::create_augroup("Foo", None).expect("create_augroup failed");
let _ = api::create_augroup("Foo", &Default::default())
.expect("create_augroup failed");
assert_eq!(Ok(()), api::del_augroup_by_name("Foo"));
}

View file

@ -32,15 +32,22 @@ fn buf_call() {
fn buf_create_del_user_command() {
let mut buf = Buffer::current();
let res = buf.create_user_command("Foo", ":", None);
let res = buf.create_user_command("Foo", ":", &Default::default());
assert_eq!(Ok(()), res);
api::command("Foo").unwrap();
let res = buf.create_user_command("Bar", |_args| Ok(()), None);
let res =
buf.create_user_command("Bar", |_args| Ok(()), &Default::default());
assert_eq!(Ok(()), res);
api::command("Bar").unwrap();
assert_eq!(2, buf.get_commands(None).unwrap().collect::<Vec<_>>().len());
assert_eq!(
2,
buf.get_commands(&Default::default())
.unwrap()
.collect::<Vec<_>>()
.len()
);
assert_eq!(Ok(()), buf.del_user_command("Foo"));
assert_eq!(Ok(()), buf.del_user_command("Bar"));
@ -62,7 +69,7 @@ fn loaded_n_valid() {
#[oxi::test]
fn new_buf_delete() {
let buf = api::create_buf(true, false).unwrap();
assert_eq!(Ok(()), buf.delete(None));
assert_eq!(Ok(()), buf.delete(&Default::default()));
}
#[oxi::test]
@ -75,7 +82,7 @@ fn buf_set_get_del_keymap() {
.expr(true)
.build();
let res = buf.set_keymap(Mode::Insert, "a", "", Some(&opts));
let res = buf.set_keymap(Mode::Insert, "a", "", &opts);
assert_eq!(Ok(()), res);
let keymaps = buf.get_keymap(Mode::Insert).unwrap().collect::<Vec<_>>();
@ -89,7 +96,12 @@ fn buf_set_get_del_keymap() {
fn buf_set_get_del_nvo_keymap() {
let mut buf = Buffer::current();
let res = buf.set_keymap(Mode::NormalVisualOperator, "a", "b", None);
let res = buf.set_keymap(
Mode::NormalVisualOperator,
"a",
"b",
&Default::default(),
);
assert_eq!(Ok(()), res);
let keymaps = buf
@ -140,7 +152,7 @@ fn set_get_del_text() {
assert_eq!(Ok(()), buf.set_text(0, 0, 0, 0, ["foo", "bar", "baz"]));
assert_eq!(
vec!["foo", "bar", "baz"],
buf.get_text(0, 0, 2, 3, None)
buf.get_text(0, 0, 2, 3, &Default::default())
.unwrap()
.flat_map(TryFrom::try_from)
.collect::<Vec<String>>()
@ -151,7 +163,7 @@ fn set_get_del_text() {
assert_eq!(
1,
buf.get_text(0, 0, 0, 1, None)
buf.get_text(0, 0, 0, 1, &Default::default())
.unwrap()
.map(String::try_from)
.collect::<Result<Vec<_>, _>>()

View file

@ -48,7 +48,7 @@ fn get_extmarks() {
let opts = GetExtmarksOpts::builder().details(true).build();
let res = buf
.get_extmarks(ns_id, start, end, Some(&opts))
.get_extmarks(ns_id, start, end, &opts)
.map(|iter| iter.collect::<Vec<_>>());
assert!(res.is_ok(), "{res:?}");
@ -155,7 +155,7 @@ fn set_get_del_extmark() {
let extmark_id = res.unwrap();
let opts = GetExtmarkByIdOpts::builder().details(true).build();
let got = buf.get_extmark_by_id(ns_id, extmark_id, Some(&opts));
let got = buf.get_extmark_by_id(ns_id, extmark_id, &opts);
assert!(got.is_ok(), "{got:?}");
let (row, col, infos) = got.unwrap();

View file

@ -10,15 +10,22 @@ fn chan_send_fail() {
#[oxi::test]
fn create_del_user_command() {
let res = api::create_user_command("Foo", ":", None);
let res = api::create_user_command("Foo", ":", &Default::default());
assert_eq!(Ok(()), res);
api::command("Foo").unwrap();
let res = api::create_user_command("Bar", |_args| Ok(()), None);
let res =
api::create_user_command("Bar", |_args| Ok(()), &Default::default());
assert_eq!(Ok(()), res);
api::command("Bar").unwrap();
assert_eq!(2, api::get_commands(None).unwrap().collect::<Vec<_>>().len());
assert_eq!(
2,
api::get_commands(&Default::default())
.unwrap()
.collect::<Vec<_>>()
.len()
);
assert_eq!(Ok(()), api::del_user_command("Foo"));
assert_eq!(Ok(()), api::del_user_command("Bar"));
@ -27,16 +34,17 @@ fn create_del_user_command() {
#[oxi::test]
fn user_command_with_count() {
let opts = CreateCommandOpts::builder().count(32).build();
api::create_user_command("Foo", "echo 'foo'", Some(&opts)).unwrap();
api::create_user_command("Foo", "echo 'foo'", &opts).unwrap();
let res = api::get_commands(None).map(|cmds| cmds.collect::<Vec<_>>());
let res = api::get_commands(&Default::default())
.map(|cmds| cmds.collect::<Vec<_>>());
assert!(res.is_ok(), "{res:?}");
}
#[oxi::test]
fn eval_statusline() {
let opts = EvalStatuslineOpts::builder().highlights(true).build();
let res = api::eval_statusline("foo", Some(&opts));
let res = api::eval_statusline("foo", &opts);
assert_eq!(Ok("foo".into()), res.map(|infos| infos.str));
}
@ -57,7 +65,7 @@ fn get_colors() {
#[oxi::test]
fn get_context() {
let res = api::get_context(None);
let res = api::get_context(&Default::default());
assert!(res.is_ok());
}
@ -90,7 +98,7 @@ fn get_option_info() {
#[oxi::test]
fn hl_underline() {
let opts = SetHighlightOpts::builder().underline(true).build();
api::set_hl(0, "MatchParen", Some(&opts)).unwrap();
api::set_hl(0, "MatchParen", &opts).unwrap();
let infos = api::get_hl_by_name("MatchParen", true).unwrap();
assert_eq!(Some(true), infos.underline);
@ -116,7 +124,7 @@ fn set_get_del_keymap() {
.expr(true)
.build();
let res = api::set_keymap(Mode::Insert, "a", "", Some(&opts));
let res = api::set_keymap(Mode::Insert, "a", "", &opts);
assert_eq!(Ok(()), res);
let keymaps = api::get_keymap(Mode::Insert).collect::<Vec<_>>();
@ -133,7 +141,10 @@ fn set_get_del_mark() {
let res = buf.set_mark('A', 1, 0);
assert_eq!(Ok(()), res);
assert_eq!((1, 0, buf, "".into()), api::get_mark('A', None).unwrap());
assert_eq!(
(1, 0, buf, "".into()),
api::get_mark('A', &Default::default()).unwrap()
);
let res = api::del_mark('A');
assert_eq!(Ok(()), res);

View file

@ -13,7 +13,7 @@ fn call_function() {
fn cmd_basic() {
let opts = CmdOpts::builder().output(true).build();
let infos = CmdInfos::builder().cmd("echo 'foo'").build();
assert_eq!(Ok(None), api::cmd(&infos, Some(&opts)));
assert_eq!(Ok(None), api::cmd(&infos, &opts));
}
#[cfg(feature = "nightly")]
@ -21,7 +21,7 @@ fn cmd_basic() {
fn cmd_no_output() {
let opts = CmdOpts::builder().output(false).build();
let infos = CmdInfos::builder().cmd("echo 'foo'").build();
assert_eq!(Ok(None), api::cmd(&infos, Some(&opts)));
assert_eq!(Ok(None), api::cmd(&infos, &opts));
}
#[oxi::test]
@ -58,7 +58,7 @@ fn exec() {
fn parse_cmd_basic() {
let opts = ParseCmdOpts::builder().build();
let res = api::parse_cmd("echo 'foo'", Some(&opts));
let res = api::parse_cmd("echo 'foo'", &opts);
assert!(res.is_ok(), "{res:?}");
let infos = res.unwrap();