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#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
59#[derive(Clone, Debug, Default)]
60#[repr(C)]
61pub struct DecorationProviderOpts {
62    on_buf: Object,
63    on_end: Object,
64    on_win: Object,
65    on_line: Object,
66    on_start: Object,
67    _on_hl_def: Object,
68    _on_spell_nav: Object,
69}
70
71/// Options passed to
72/// [`set_decoration_provider()`](crate::set_decoration_provider).
73#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
74#[derive(Clone, Debug, Default)]
75#[repr(C)]
76pub struct DecorationProviderOpts {
77    on_start: Object,
78    on_buf: Object,
79    on_win: Object,
80    on_line: Object,
81    on_end: Object,
82    _on_hl_def: Object,
83    _on_spell_nav: Object,
84}
85
86impl DecorationProviderOpts {
87    #[inline(always)]
88    /// Creates a new [`DecorationProviderOptsBuilder`].
89    pub fn builder() -> DecorationProviderOptsBuilder {
90        DecorationProviderOptsBuilder::default()
91    }
92}
93
94#[derive(Clone, Default)]
95pub struct DecorationProviderOptsBuilder(DecorationProviderOpts);
96
97impl DecorationProviderOptsBuilder {
98    #[inline]
99    pub fn on_buf<F>(&mut self, fun: F) -> &mut Self
100    where
101        F: ToFunction<OnBufArgs, ()>,
102    {
103        self.0.on_buf = Object::from_luaref(fun.into_luaref());
104        self
105    }
106
107    #[inline]
108    pub fn on_end<F>(&mut self, fun: F) -> &mut Self
109    where
110        F: ToFunction<OnEndArgs, ()>,
111    {
112        self.0.on_end = Object::from_luaref(fun.into_luaref());
113        self
114    }
115
116    #[inline]
117    pub fn on_line<F>(&mut self, fun: F) -> &mut Self
118    where
119        F: ToFunction<OnLineArgs, ()>,
120    {
121        self.0.on_line = Object::from_luaref(fun.into_luaref());
122        self
123    }
124
125    #[inline]
126    pub fn on_start<F>(&mut self, fun: F) -> &mut Self
127    where
128        F: ToFunction<OnStartArgs, DontSkipRedrawCycle>,
129    {
130        self.0.on_start = Object::from_luaref(fun.into_luaref());
131        self
132    }
133
134    #[inline]
135    pub fn on_win<F>(&mut self, fun: F) -> &mut Self
136    where
137        F: ToFunction<OnWinArgs, DontSkipOnLines>,
138    {
139        self.0.on_win = Object::from_luaref(fun.into_luaref());
140        self
141    }
142
143    #[inline]
144    pub fn build(&mut self) -> DecorationProviderOpts {
145        std::mem::take(&mut self.0)
146    }
147}