nvim_oxi_api/opts/
create_autocmd.rs

1use crate::types::AutocmdCallbackArgs;
2use crate::Buffer;
3use crate::StringOrInt;
4
5pub type ShouldDeleteAutocmd = bool;
6
7/// Options passed to [`create_autocmd()`](crate::create_autocmd).
8#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
9#[derive(Clone, Debug, Default, macros::OptsBuilder)]
10#[repr(C)]
11pub struct CreateAutocmdOpts {
12    #[builder(mask)]
13    mask: u64,
14
15    /// A specific `Buffer` for buffer-local autocommands.
16    #[builder(argtype = "Buffer", inline = "{0}.0")]
17    buffer: types::BufHandle,
18
19    /// Callback to execute when the autocommand is triggered. Cannot be used
20    /// together with `command`.
21    #[builder(
22        generics = r#"F: Into<types::Function<AutocmdCallbackArgs, ShouldDeleteAutocmd>>"#,
23        argtype = "F",
24        inline = "{0}.into().into()"
25    )]
26    callback: types::Object,
27
28    /// Vim command to execute when the autocommand is triggered. Cannot be
29    /// used together with `callback`.
30    // TODO: fix builder(Into).
31    #[builder(
32        generics = "S: Into<types::String>",
33        argtype = "S",
34        inline = "{0}.into()"
35    )]
36    command: types::String,
37
38    /// Description of the autocommand.
39    // TODO: fix builder(Into).
40    #[builder(
41        generics = "S: Into<types::String>",
42        argtype = "S",
43        inline = "{0}.into()"
44    )]
45    desc: types::String,
46
47    /// The autocommand group name or id to match against.
48    #[builder(
49        generics = "G: StringOrInt",
50        argtype = "G",
51        inline = "{0}.to_object()"
52    )]
53    group: types::Object,
54
55    /// Run nested autocommands.
56    #[builder(argtype = "bool")]
57    nested: types::Boolean,
58
59    /// Only run the autocommand once.
60    #[builder(argtype = "bool")]
61    once: types::Boolean,
62
63    /// Patterns to match against.
64    #[builder(
65        generics = "'a, I: IntoIterator<Item = &'a str>",
66        method = "patterns",
67        argtype = "I",
68        inline = "types::Array::from_iter({0}).into()"
69    )]
70    pattern: types::Object,
71}
72
73/// Options passed to [`create_autocmd()`](crate::create_autocmd).
74#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
75#[derive(Clone, Debug, Default)]
76#[repr(C)]
77pub struct CreateAutocmdOpts {
78    desc: types::Object,
79    once: types::Object,
80    group: types::Object,
81    buffer: types::Object,
82    nested: types::Object,
83    command: types::Object,
84    pattern: types::Object,
85    callback: types::Object,
86}
87
88#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
89impl CreateAutocmdOpts {
90    #[inline(always)]
91    pub fn builder() -> CreateAutocmdOptsBuilder {
92        CreateAutocmdOptsBuilder::default()
93    }
94}
95
96#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
97#[derive(Clone, Default)]
98pub struct CreateAutocmdOptsBuilder(CreateAutocmdOpts);
99
100#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
101impl CreateAutocmdOptsBuilder {
102    /// A specific `Buffer` for buffer-local autocommands.
103    #[inline]
104    pub fn buffer(&mut self, buffer: Buffer) -> &mut Self {
105        self.0.buffer = buffer.into();
106        self
107    }
108
109    /// Callback to execute when the autocommand is triggered. Cannot be used
110    /// together with `command`.
111    #[inline]
112    pub fn callback<F>(&mut self, callback: F) -> &mut Self
113    where
114        F: Into<types::Function<AutocmdCallbackArgs, ShouldDeleteAutocmd>>,
115    {
116        self.0.callback = callback.into().into();
117        self
118    }
119
120    /// Vim command to execute when the autocommand is triggered. Cannot be
121    /// used together with `callback`.
122    #[inline]
123    pub fn command<S>(&mut self, command: S) -> &mut Self
124    where
125        S: Into<types::String>,
126    {
127        self.0.command = command.into().into();
128        self
129    }
130
131    /// Description of the autocommand.
132    #[inline]
133    pub fn desc<S>(&mut self, desc: S) -> &mut Self
134    where
135        S: Into<types::String>,
136    {
137        self.0.desc = desc.into().into();
138        self
139    }
140
141    /// The autocommand group name or id to match against.
142    #[inline]
143    pub fn group<Grp>(&mut self, group: Grp) -> &mut Self
144    where
145        Grp: StringOrInt,
146    {
147        self.0.group = group.to_object();
148        self
149    }
150
151    /// Run nested autocommands.
152    #[inline]
153    pub fn nested(&mut self, nested: bool) -> &mut Self {
154        self.0.nested = nested.into();
155        self
156    }
157
158    /// Only run the autocommand once.
159    #[inline]
160    pub fn once(&mut self, once: bool) -> &mut Self {
161        self.0.once = once.into();
162        self
163    }
164
165    /// Patterns to match against.
166    #[inline]
167    pub fn patterns<'a, I>(&mut self, patterns: I) -> &mut Self
168    where
169        I: IntoIterator<Item = &'a str>,
170    {
171        self.0.pattern = types::Array::from_iter(patterns).into();
172        self
173    }
174
175    #[inline]
176    pub fn build(&mut self) -> CreateAutocmdOpts {
177        std::mem::take(&mut self.0)
178    }
179}