nvim_api/opts/
decoration_provider.rs

1use derive_builder::Builder;
2use nvim_types::{Dictionary, Object};
3
4use crate::ToFunction;
5use crate::{Buffer, Window};
6
7// NOTE: docs say a third argument of changedtick is passed. I don't see it.
8/// Arguments passed to the function registered to
9/// [`on_buf`](DecorationProviderOptsBuilder::on_buf).
10pub type OnBufArgs = (
11    String, // the string literal "buf"
12    Buffer, // buffer
13);
14
15/// Arguments passed to the function registered to
16/// [`on_end`](DecorationProviderOptsBuilder::on_end).
17pub type OnEndArgs = (
18    String, // the string literal "end"
19    u32,    // changedtick
20);
21
22/// Arguments passed to the function registered to
23/// [`on_line`](DecorationProviderOptsBuilder::on_line).
24pub type OnLineArgs = (
25    String, // the string literal "win"
26    Window, // window
27    Buffer, // buffer
28    usize,  // row
29);
30
31/// Arguments passed to the function registered to
32/// [`on_start`](DecorationProviderOptsBuilder::on_start).
33pub type OnStartArgs = (
34    String, // the string literal "start"
35    u32,    // changedtick
36    u32, /* `type`, undocumented? (https://github.com/neovim/neovim/blob/master/src/nvim/decoration_provider.c#L68) */
37);
38
39/// Arguments passed to the function registered to
40/// [`on_win`](DecorationProviderOptsBuilder::on_win).
41pub type OnWinArgs = (
42    String, // the string literal "win"
43    Window, // window
44    Buffer, // buffer
45    u32,    // topline
46    u32,    // botline guess
47);
48
49/// The `on_start` callback can return `false` to disable the provider until
50/// the next redraw.
51pub type DontSkipRedrawCycle = bool;
52
53/// The `on_win` callback can return `false` to skip the `on_line` callback for
54/// that window.
55pub type DontSkipOnLines = bool;
56
57/// Options passed to
58/// [`nvim_oxi::api::set_decoration_provider`](crate::set_decoration_provider).
59#[derive(Clone, Debug, Default, Builder)]
60#[builder(default, build_fn(private, name = "fallible_build"))]
61pub struct DecorationProviderOpts {
62    #[builder(setter(custom))]
63    on_buf: Object,
64
65    #[builder(setter(custom))]
66    on_end: Object,
67
68    #[builder(setter(custom))]
69    on_line: Object,
70
71    #[builder(setter(custom))]
72    on_start: Object,
73
74    #[builder(setter(custom))]
75    on_win: Object,
76}
77
78impl DecorationProviderOpts {
79    #[inline(always)]
80    /// Creates a new [`DecorationProviderOptsBuilder`].
81    pub fn builder() -> DecorationProviderOptsBuilder {
82        DecorationProviderOptsBuilder::default()
83    }
84}
85
86impl DecorationProviderOptsBuilder {
87    pub fn on_buf<F>(&mut self, fun: F) -> &mut Self
88    where
89        F: ToFunction<OnBufArgs, ()>,
90    {
91        self.on_buf = Some(fun.to_object());
92        self
93    }
94
95    pub fn on_end<F>(&mut self, fun: F) -> &mut Self
96    where
97        F: ToFunction<OnEndArgs, ()>,
98    {
99        self.on_end = Some(fun.to_object());
100        self
101    }
102
103    pub fn on_line<F>(&mut self, fun: F) -> &mut Self
104    where
105        F: ToFunction<OnLineArgs, ()>,
106    {
107        self.on_line = Some(fun.to_object());
108        self
109    }
110
111    pub fn on_start<F>(&mut self, fun: F) -> &mut Self
112    where
113        F: ToFunction<OnStartArgs, DontSkipRedrawCycle>,
114    {
115        self.on_start = Some(fun.to_object());
116        self
117    }
118
119    pub fn on_win<F>(&mut self, fun: F) -> &mut Self
120    where
121        F: ToFunction<OnWinArgs, DontSkipOnLines>,
122    {
123        self.on_win = Some(fun.to_object());
124        self
125    }
126
127    pub fn build(&mut self) -> DecorationProviderOpts {
128        self.fallible_build().expect("never fails, all fields have defaults")
129    }
130}
131
132impl From<&DecorationProviderOpts> for Dictionary {
133    fn from(opts: &DecorationProviderOpts) -> Self {
134        Self::from_iter([
135            ("on_buf", opts.on_buf.clone()),
136            ("on_end", opts.on_end.clone()),
137            ("on_line", opts.on_line.clone()),
138            ("on_start", opts.on_start.clone()),
139            ("on_win", opts.on_win.clone()),
140        ])
141    }
142}