nvim_oxi_api/opts/buf_attach.rs
1use crate::Buffer;
2use crate::ToFunction;
3
4/// Arguments passed to the callback registered to
5/// [`on_lines`](BufAttachOptsBuilder::on_lines). The `(a, b, c, d, e, f, g, h,
6/// i)` tuple represents:
7///
8/// - `a`: the string literal `"lines"`;
9/// - `b`: the [`Buffer`] that triggered the callback;
10/// - `c`: the value of the buffer-local `b:changedtick` variable;
11/// - `d`: first row that changed (0-indexed);
12/// - `e`: last row that was changed;
13/// - `f`: last row in the updated range;
14/// - `g`: byte count of previous contents;
15/// - `h`: deleted UTF-32 codepoints (if
16/// [`utf_sizes`](BufAttachOptsBuilder::utf_sizes) was `true`);
17/// - `i`: deleted UTF-16 codeunits (if
18/// [`utf_sizes`](BufAttachOptsBuilder::utf_sizes) was `true`);
19pub type OnLinesArgs = (
20 String,
21 Buffer,
22 u32,
23 usize,
24 usize,
25 usize,
26 usize,
27 Option<usize>,
28 Option<usize>,
29);
30
31/// Arguments passed to the callback registered to [`on_bytes`](BufAttachOptsBuilder::on_bytes). The `(a, b, c, d, e, f, g, h, i, j, k, l)`
32/// - `a`: the string literal `"bytes"`;
33/// - `b`: the [`Buffer`] that triggered the callback;
34/// - `c`: the value of the buffer-local `b:changedtick` variable;
35/// - `d`: start row of the changed text (0-indexed);
36/// - `e`: start column of the changed text;
37/// - `f`: byte offset of the changed text from the start of the buffer;
38/// - `g`: number of rows deleted;
39/// - `h`: number of columns deleted;
40/// - `i`: number of bytes deleted;
41/// - `j`: number of rows added;
42/// - `k`: number of columns added;
43/// - `l`: number of bytes added;
44pub type OnBytesArgs = (
45 String,
46 Buffer,
47 u32,
48 usize,
49 usize,
50 usize,
51 usize,
52 usize,
53 usize,
54 usize,
55 usize,
56 usize,
57);
58
59/// Arguments passed to the callback registered to
60/// [`on_changedtick`](BufAttachOptsBuilder::on_changedtick). The first tuple
61/// element is the string literal `"changedtick"`, the second is the [`Buffer`]
62/// that triggered the callback and the third is current value of the
63/// buffer-local
64/// [`b:changedtick`](https://neovim.io/doc/user/eval.html#b:changedtick)
65/// variable.
66pub type OnChangedtickArgs = (String, Buffer, u32);
67
68/// Arguments passed to the callback registered to
69/// [`on_detach`](BufAttachOptsBuilder::on_detach). The first tuple element is
70/// the string literal `"detach"`, the second is the [`Buffer`] that triggered
71/// the callback.
72pub type OnDetachArgs = (String, Buffer);
73
74/// Arguments passed to the callback registered to
75/// [`on_reload`](BufAttachOptsBuilder::on_reload). The first tuple element is
76/// the string literal `"reload"`, the second is the [`Buffer`] that triggered
77/// the callback.
78pub type OnReloadArgs = (String, Buffer);
79
80/// All the registered callbacks can detach by returning `true`, as described
81/// in `:h api-lua-detach`.
82pub type ShouldDetach = bool;
83
84/// Options passed to [`Buffer::attach`](crate::Buffer::attach).
85#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
86#[derive(Clone, Debug, Default)]
87pub struct BufAttachOpts {
88 on_bytes: types::Object,
89 on_changedtick: types::Object,
90 on_detach: types::Object,
91 on_lines: types::Object,
92 on_reload: types::Object,
93 preview: types::Object,
94 utf_sizes: types::Object,
95}
96
97#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
98impl BufAttachOpts {
99 #[inline(always)]
100 /// Creates a new [`BufAttachOptsBuilder`].
101 pub fn builder() -> BufAttachOptsBuilder {
102 BufAttachOptsBuilder::default()
103 }
104}
105
106#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
107#[derive(Clone, Default)]
108pub struct BufAttachOptsBuilder(BufAttachOpts);
109
110#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
111impl BufAttachOptsBuilder {
112 /// Callback invoked on change. It receives more granular information about
113 /// the change compared to [`on_lines`](BufAttachOptsBuilder::on_lines).
114 #[inline]
115 pub fn on_bytes<F>(&mut self, on_bytes: F) -> &mut Self
116 where
117 F: ToFunction<OnBytesArgs, ShouldDetach>,
118 {
119 self.0.on_bytes = types::Object::from_luaref(on_bytes.into_luaref());
120 self
121 }
122
123 /// Callback invoked on changedtick increment without text change.
124 #[inline]
125 pub fn on_changedtick<F>(&mut self, on_changedtick: F) -> &mut Self
126 where
127 F: ToFunction<OnChangedtickArgs, ShouldDetach>,
128 {
129 self.0.on_changedtick =
130 types::Object::from_luaref(on_changedtick.into_luaref());
131 self
132 }
133
134 /// Callback invoked on detach.
135 #[inline]
136 pub fn on_detach<F>(&mut self, on_detach: F) -> &mut Self
137 where
138 F: ToFunction<OnDetachArgs, ShouldDetach>,
139 {
140 self.0.on_detach = types::Object::from_luaref(on_detach.into_luaref());
141 self
142 }
143
144 /// Callback invoked on change.
145 #[inline]
146 pub fn on_lines<F>(&mut self, fun: F) -> &mut Self
147 where
148 F: ToFunction<OnLinesArgs, ShouldDetach>,
149 {
150 self.0.on_lines = types::Object::from_luaref(fun.into_luaref());
151 self
152 }
153
154 /// Callback invoked on reload. The entire buffer content should be
155 /// considered changed.
156 #[inline]
157 pub fn on_reload<F>(&mut self, on_reload: F) -> &mut Self
158 where
159 F: ToFunction<OnReloadArgs, ShouldDetach>,
160 {
161 self.0.on_reload = types::Object::from_luaref(on_reload.into_luaref());
162 self
163 }
164
165 /// Whether to also attach to command preview (i.e.
166 /// [`inccommand`](https://neovim.io/doc/user/options.html#'inccommand'))
167 /// events.
168 #[inline]
169 pub fn preview(&mut self, preview: bool) -> &mut Self {
170 self.0.preview = preview.into();
171 self
172 }
173
174 /// Whether to include the UTF-32 and UTF-16 sizes of the replaced region
175 /// as the last arguments of the
176 /// [`on_lines`](BufAttachOptsBuilder::on_lines) callback.
177 #[inline]
178 pub fn utf_sizes(&mut self, utf_sizes: bool) -> &mut Self {
179 self.0.utf_sizes = utf_sizes.into();
180 self
181 }
182
183 #[inline]
184 pub fn build(&mut self) -> BufAttachOpts {
185 std::mem::take(&mut self.0)
186 }
187}
188
189#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
190impl From<&BufAttachOpts> for types::Dictionary {
191 #[inline]
192 fn from(opts: &BufAttachOpts) -> Self {
193 Self::from_iter([
194 ("on_bytes", opts.on_bytes.clone()),
195 ("on_changedtick", opts.on_changedtick.clone()),
196 ("on_detach", opts.on_detach.clone()),
197 ("on_lines", opts.on_lines.clone()),
198 ("on_reload", opts.on_reload.clone()),
199 ("preview", opts.preview.clone()),
200 ("utf_sizes", opts.utf_sizes.clone()),
201 ])
202 }
203}
204
205/// Options passed to [`Buffer::attach`](crate::Buffer::attach).
206#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
207#[derive(Clone, Debug, Default, macros::OptsBuilder)]
208#[repr(C)]
209pub struct BufAttachOpts {
210 #[builder(mask)]
211 mask: u64,
212
213 /// Callback invoked on change.
214 #[builder(
215 generics = "F: ToFunction<OnLinesArgs, ShouldDetach>",
216 argtype = "F",
217 inline = "{0}.into_luaref()"
218 )]
219 on_lines: types::LuaRef,
220
221 /// Callback invoked on change. It receives more granular information about
222 /// the change compared to [`on_lines`](BufAttachOptsBuilder::on_lines).
223 #[builder(
224 generics = "F: ToFunction<OnBytesArgs, ShouldDetach>",
225 argtype = "F",
226 inline = "{0}.into_luaref()"
227 )]
228 on_bytes: types::LuaRef,
229
230 /// Callback invoked on changedtick increment without text change.
231 #[builder(
232 generics = "F: ToFunction<OnChangedtickArgs, ShouldDetach>",
233 argtype = "F",
234 inline = "{0}.into_luaref()"
235 )]
236 on_changedtick: types::LuaRef,
237
238 /// Callback invoked on detach.
239 #[builder(
240 generics = "F: ToFunction<OnDetachArgs, ShouldDetach>",
241 argtype = "F",
242 inline = "{0}.into_luaref()"
243 )]
244 on_detach: types::LuaRef,
245
246 /// Callback invoked on reload. The entire buffer content should be
247 /// considered changed.
248 #[builder(
249 generics = "F: ToFunction<OnReloadArgs, ShouldDetach>",
250 argtype = "F",
251 inline = "{0}.into_luaref()"
252 )]
253 on_reload: types::LuaRef,
254
255 /// Whether to include the UTF-32 and UTF-16 sizes of the replaced region
256 /// as the last arguments of the
257 /// [`on_lines`](BufAttachOptsBuilder::on_lines) callback.
258 #[builder(argtype = "bool")]
259 utf_sizes: types::Boolean,
260
261 /// Whether to also attach to command preview (i.e.
262 /// [`inccommand`](https://neovim.io/doc/user/options.html#'inccommand'))
263 /// events.
264 #[builder(argtype = "bool")]
265 preview: types::Boolean,
266}