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).
6///
7/// The `(a, b, c, d, e, f, g, h, i)` tuple represents:
8///
9/// - `a`: the string literal `"lines"`;
10/// - `b`: the [`Buffer`] that triggered the callback;
11/// - `c`: the value of the buffer-local `b:changedtick` variable;
12/// - `d`: first row that changed (0-indexed);
13/// - `e`: last row that was changed;
14/// - `f`: last row in the updated range;
15/// - `g`: byte count of previous contents;
16/// - `h`: deleted UTF-32 codepoints (if
17///   [`utf_sizes`](BufAttachOptsBuilder::utf_sizes) was `true`);
18/// - `i`: deleted UTF-16 codeunits (if
19///   [`utf_sizes`](BufAttachOptsBuilder::utf_sizes) was `true`);
20pub type OnLinesArgs = (
21    String,
22    Buffer,
23    u32,
24    usize,
25    usize,
26    usize,
27    usize,
28    Option<usize>,
29    Option<usize>,
30);
31
32/// Arguments passed to the callback registered to [`on_bytes`](BufAttachOptsBuilder::on_bytes).
33///
34/// The `(a, b, c, d, e, f, g, h, i, j, k, l)`
35/// - `a`: the string literal `"bytes"`;
36/// - `b`: the [`Buffer`] that triggered the callback;
37/// - `c`: the value of the buffer-local `b:changedtick` variable;
38/// - `d`: start row of the changed text (0-indexed);
39/// - `e`: start column of the changed text;
40/// - `f`: byte offset of the changed text (from the start of the buffer);
41/// - `g`: old end row of the changed text (offset from start row);
42/// - `h`: old end column of the changed text (if old end row = 0, offset from start column);
43/// - `i`: old end byte length of the changed text;
44/// - `j`: new end row of the changed text (offset from start row);
45/// - `k`: new end column of the changed text (if new end row = 0, offset from start column);
46/// - `l`: new end byte length of the changed text;
47pub type OnBytesArgs = (
48    String,
49    Buffer,
50    u32,
51    usize,
52    usize,
53    usize,
54    usize,
55    usize,
56    usize,
57    usize,
58    usize,
59    usize,
60);
61
62/// Arguments passed to the callback registered to
63/// [`on_changedtick`](BufAttachOptsBuilder::on_changedtick).
64///
65/// The first tuple element is the string literal `"changedtick"`, the second
66/// is the [`Buffer`] that triggered the callback and the third is current
67/// value of the buffer-local
68/// [`b:changedtick`](https://neovim.io/doc/user/eval.html#b:changedtick)
69/// variable.
70pub type OnChangedtickArgs = (String, Buffer, u32);
71
72/// Arguments passed to the callback registered to
73/// [`on_detach`](BufAttachOptsBuilder::on_detach).
74///
75/// The first tuple element is the string literal `"detach"`, the second is the
76/// [`Buffer`] that triggered the callback.
77pub type OnDetachArgs = (String, Buffer);
78
79/// Arguments passed to the callback registered to
80/// [`on_reload`](BufAttachOptsBuilder::on_reload).
81///
82/// The first tuple element is the string literal `"reload"`, the second is the
83/// [`Buffer`] that triggered the callback.
84pub type OnReloadArgs = (String, Buffer);
85
86/// All the registered callbacks can detach by returning `true`, as described
87/// in `:h api-lua-detach`.
88pub type ShouldDetach = bool;
89
90/// Options passed to [`Buffer::attach`](crate::Buffer::attach).
91#[derive(Clone, Debug, Default, macros::OptsBuilder)]
92#[repr(C)]
93pub struct BufAttachOpts {
94    #[builder(mask)]
95    mask: u64,
96
97    /// Callback invoked on change.
98    #[builder(
99        generics = "F: ToFunction<OnLinesArgs, ShouldDetach>",
100        argtype = "F",
101        inline = "{0}.into_luaref()"
102    )]
103    on_lines: types::LuaRef,
104
105    /// Callback invoked on change. It receives more granular information about
106    /// the change compared to [`on_lines`](BufAttachOptsBuilder::on_lines).
107    #[builder(
108        generics = "F: ToFunction<OnBytesArgs, ShouldDetach>",
109        argtype = "F",
110        inline = "{0}.into_luaref()"
111    )]
112    on_bytes: types::LuaRef,
113
114    /// Callback invoked on changedtick increment without text change.
115    #[builder(
116        generics = "F: ToFunction<OnChangedtickArgs, ShouldDetach>",
117        argtype = "F",
118        inline = "{0}.into_luaref()"
119    )]
120    on_changedtick: types::LuaRef,
121
122    /// Callback invoked on detach.
123    #[builder(
124        generics = "F: ToFunction<OnDetachArgs, ShouldDetach>",
125        argtype = "F",
126        inline = "{0}.into_luaref()"
127    )]
128    on_detach: types::LuaRef,
129
130    /// Callback invoked on reload. The entire buffer content should be
131    /// considered changed.
132    #[builder(
133        generics = "F: ToFunction<OnReloadArgs, ShouldDetach>",
134        argtype = "F",
135        inline = "{0}.into_luaref()"
136    )]
137    on_reload: types::LuaRef,
138
139    /// Whether to include the UTF-32 and UTF-16 sizes of the replaced region
140    /// as the last arguments of the
141    /// [`on_lines`](BufAttachOptsBuilder::on_lines) callback.
142    #[builder(argtype = "bool")]
143    utf_sizes: types::Boolean,
144
145    /// Whether to also attach to command preview (i.e.
146    /// [`inccommand`](https://neovim.io/doc/user/options.html#'inccommand'))
147    /// events.
148    #[builder(argtype = "bool")]
149    preview: types::Boolean,
150}