nvim_oxi_api/opts/
decoration_provider.rs

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