use derive_builder::Builder;
use nvim_types::{Dictionary, Object};
use crate::Buffer;
use crate::ToFunction;
pub type OnLinesArgs = (
String,
Buffer,
u32,
usize,
usize,
usize,
usize,
Option<usize>,
Option<usize>,
);
pub type OnBytesArgs = (
String,
Buffer,
u32,
usize,
usize,
usize,
usize,
usize,
usize,
usize,
usize,
usize,
);
pub type OnChangedtickArgs = (String, Buffer, u32);
pub type OnDetachArgs = (String, Buffer);
pub type OnReloadArgs = (String, Buffer);
pub type ShouldDetach = bool;
#[derive(Clone, Debug, Default, Builder)]
#[builder(default, build_fn(private, name = "fallible_build"))]
pub struct BufAttachOpts {
#[builder(setter(custom))]
on_bytes: Object,
#[builder(setter(custom))]
on_changedtick: Object,
#[builder(setter(custom))]
on_detach: Object,
#[builder(setter(custom))]
on_lines: Object,
#[builder(setter(custom))]
on_reload: Object,
preview: bool,
utf_sizes: bool,
}
impl BufAttachOpts {
#[inline(always)]
pub fn builder() -> BufAttachOptsBuilder {
BufAttachOptsBuilder::default()
}
}
impl BufAttachOptsBuilder {
pub fn on_bytes<F>(&mut self, fun: F) -> &mut Self
where
F: ToFunction<OnBytesArgs, ShouldDetach>,
{
self.on_bytes = Some(fun.to_object());
self
}
pub fn on_changedtick<F>(&mut self, fun: F) -> &mut Self
where
F: ToFunction<OnChangedtickArgs, ShouldDetach>,
{
self.on_changedtick = Some(fun.to_object());
self
}
pub fn on_detach<F>(&mut self, fun: F) -> &mut Self
where
F: ToFunction<OnDetachArgs, ShouldDetach>,
{
self.on_detach = Some(fun.to_object());
self
}
pub fn on_lines<F>(&mut self, fun: F) -> &mut Self
where
F: ToFunction<OnLinesArgs, ShouldDetach>,
{
self.on_lines = Some(fun.to_object());
self
}
pub fn on_reload<F>(&mut self, fun: F) -> &mut Self
where
F: ToFunction<OnReloadArgs, ShouldDetach>,
{
self.on_reload = Some(fun.to_object());
self
}
pub fn build(&mut self) -> BufAttachOpts {
self.fallible_build().expect("never fails, all fields have defaults")
}
}
impl From<&BufAttachOpts> for Dictionary {
fn from(opts: &BufAttachOpts) -> Self {
Self::from_iter([
("on_bytes", opts.on_bytes.clone()),
("on_changedtick", opts.on_changedtick.clone()),
("on_detach", opts.on_detach.clone()),
("on_lines", opts.on_lines.clone()),
("on_reload", opts.on_reload.clone()),
("preview", opts.preview.into()),
("utf_sizes", opts.utf_sizes.into()),
])
}
}