use derive_builder::Builder;
use nvim_types::{Dictionary, Object};
use crate::ToFunction;
use crate::{Buffer, Window};
pub type OnBufArgs = (
String, Buffer, );
pub type OnEndArgs = (
String, u32, );
pub type OnLineArgs = (
String, Window, Buffer, usize, );
pub type OnStartArgs = (
String, u32, u32,
);
pub type OnWinArgs = (
String, Window, Buffer, u32, u32, );
pub type DontSkipRedrawCycle = bool;
pub type DontSkipOnLines = bool;
#[derive(Clone, Debug, Default, Builder)]
#[builder(default, build_fn(private, name = "fallible_build"))]
pub struct DecorationProviderOpts {
#[builder(setter(custom))]
on_buf: Object,
#[builder(setter(custom))]
on_end: Object,
#[builder(setter(custom))]
on_line: Object,
#[builder(setter(custom))]
on_start: Object,
#[builder(setter(custom))]
on_win: Object,
}
impl DecorationProviderOpts {
#[inline(always)]
pub fn builder() -> DecorationProviderOptsBuilder {
DecorationProviderOptsBuilder::default()
}
}
impl DecorationProviderOptsBuilder {
pub fn on_buf<F>(&mut self, fun: F) -> &mut Self
where
F: ToFunction<OnBufArgs, ()>,
{
self.on_buf = Some(fun.to_object());
self
}
pub fn on_end<F>(&mut self, fun: F) -> &mut Self
where
F: ToFunction<OnEndArgs, ()>,
{
self.on_end = Some(fun.to_object());
self
}
pub fn on_line<F>(&mut self, fun: F) -> &mut Self
where
F: ToFunction<OnLineArgs, ()>,
{
self.on_line = Some(fun.to_object());
self
}
pub fn on_start<F>(&mut self, fun: F) -> &mut Self
where
F: ToFunction<OnStartArgs, DontSkipRedrawCycle>,
{
self.on_start = Some(fun.to_object());
self
}
pub fn on_win<F>(&mut self, fun: F) -> &mut Self
where
F: ToFunction<OnWinArgs, DontSkipOnLines>,
{
self.on_win = Some(fun.to_object());
self
}
pub fn build(&mut self) -> DecorationProviderOpts {
self.fallible_build().expect("never fails, all fields have defaults")
}
}
impl From<&DecorationProviderOpts> for Dictionary {
fn from(opts: &DecorationProviderOpts) -> Self {
Self::from_iter([
("on_buf", opts.on_buf.clone()),
("on_end", opts.on_end.clone()),
("on_line", opts.on_line.clone()),
("on_start", opts.on_start.clone()),
("on_win", opts.on_win.clone()),
])
}
}