nvim_api/opts/
create_autocmd.rs

1use derive_builder::Builder;
2use nvim_types::{self as nvim, Array, Function, NonOwning, Object};
3
4use crate::types::AutocmdCallbackArgs;
5use crate::Buffer;
6use crate::StringOrInt;
7
8pub type ShouldDeleteAutocmd = bool;
9
10/// Options passed to `crate::create_autocmd`.
11#[derive(Clone, Debug, Default, Builder)]
12#[builder(default, build_fn(private, name = "fallible_build"))]
13pub struct CreateAutocmdOpts {
14    /// A specific `Buffer` for buffer-local autocommands.
15    #[builder(setter(into, strip_option))]
16    buffer: Option<Buffer>,
17
18    /// Description of the autocommand.
19    #[builder(setter(custom))]
20    desc: Object,
21
22    /// Callback to execute when the autocommand is triggered. Cannot be used
23    /// together with `command`.
24    #[builder(setter(custom))]
25    callback: Object,
26
27    /// Vim command to execute when the autocommand is triggered. Cannot be
28    /// used together with `callback`.
29    #[builder(setter(custom))]
30    command: Object,
31
32    /// The autocommand group name or id to match against.
33    #[builder(setter(custom))]
34    group: Object,
35
36    /// Run nested autocommands.
37    #[builder(setter(strip_option))]
38    nested: Option<bool>,
39
40    /// Only run the autocommand once.
41    #[builder(setter(strip_option))]
42    once: Option<bool>,
43
44    /// Patterns to match against.
45    #[builder(setter(custom))]
46    patterns: Object,
47}
48
49impl CreateAutocmdOpts {
50    #[inline(always)]
51    pub fn builder() -> CreateAutocmdOptsBuilder {
52        CreateAutocmdOptsBuilder::default()
53    }
54}
55
56impl CreateAutocmdOptsBuilder {
57    pub fn callback<F>(&mut self, callback: F) -> &mut Self
58    where
59        F: Into<Function<AutocmdCallbackArgs, ShouldDeleteAutocmd>>,
60    {
61        self.callback = Some(callback.into().into());
62        self
63    }
64
65    pub fn command<S>(&mut self, command: S) -> &mut Self
66    where
67        S: Into<nvim::String>,
68    {
69        self.command = Some(command.into().into());
70        self
71    }
72
73    pub fn desc<S>(&mut self, desc: S) -> &mut Self
74    where
75        S: Into<nvim::String>,
76    {
77        self.desc = Some(desc.into().into());
78        self
79    }
80
81    pub fn group<Grp>(&mut self, group: Grp) -> &mut Self
82    where
83        Grp: StringOrInt,
84    {
85        self.group = Some(group.to_object());
86        self
87    }
88
89    pub fn patterns<'a, I>(&mut self, patterns: I) -> &mut Self
90    where
91        I: IntoIterator<Item = &'a str>,
92    {
93        self.patterns = Some(Array::from_iter(patterns).into());
94        self
95    }
96
97    pub fn build(&mut self) -> CreateAutocmdOpts {
98        self.fallible_build().expect("never fails, all fields have defaults")
99    }
100}
101
102#[derive(Default)]
103#[allow(non_camel_case_types)]
104#[repr(C)]
105pub(crate) struct KeyDict_create_autocmd<'a> {
106    desc: NonOwning<'a, Object>,
107    once: Object,
108    group: NonOwning<'a, Object>,
109    buffer: Object,
110    nested: Object,
111    command: NonOwning<'a, Object>,
112    pattern: NonOwning<'a, Object>,
113    callback: NonOwning<'a, Object>,
114}
115
116impl<'a> From<&'a CreateAutocmdOpts> for KeyDict_create_autocmd<'a> {
117    fn from(opts: &'a CreateAutocmdOpts) -> Self {
118        Self {
119            desc: opts.desc.non_owning(),
120            once: opts.once.into(),
121            group: opts.group.non_owning(),
122            buffer: opts.buffer.as_ref().into(),
123            nested: opts.nested.into(),
124            command: opts.command.non_owning(),
125            pattern: opts.patterns.non_owning(),
126            callback: opts.callback.non_owning(),
127        }
128    }
129}