nvim_api/opts/
clear_autocmds.rs

1use derive_builder::Builder;
2use nvim_types::{Array, NonOwning, Object};
3
4use crate::Buffer;
5use crate::StringOrInt;
6
7/// Options passed to [`api::clear_autocmds`](crate::clear_autocmds).
8#[derive(Clone, Debug, Default, Builder)]
9#[builder(default, build_fn(private, name = "fallible_build"))]
10pub struct ClearAutocmdsOpts {
11    /// Only clear the autocommands local to a specific `Buffer`. Cannot be
12    /// used together with [`patterns`](ClearAutocmdsOptsBuilder::patterns).
13    #[builder(setter(into, strip_option))]
14    buffer: Option<Buffer>,
15
16    #[builder(setter(custom))]
17    events: Object,
18
19    #[builder(setter(custom))]
20    group: Object,
21
22    #[builder(setter(custom))]
23    patterns: Object,
24}
25
26impl ClearAutocmdsOpts {
27    /// Creates a new [`ClearAutocmdsOptsBuilder`].
28    #[inline(always)]
29    pub fn builder() -> ClearAutocmdsOptsBuilder {
30        ClearAutocmdsOptsBuilder::default()
31    }
32}
33
34impl ClearAutocmdsOptsBuilder {
35    /// Clear all the autocommands triggered by one or more of the specified
36    /// events.
37    pub fn events<'a, I>(&mut self, iter: I) -> &mut Self
38    where
39        I: IntoIterator<Item = &'a str>,
40    {
41        self.events = Some(Array::from_iter(iter).into());
42        self
43    }
44
45    /// Only clear the autocommands matching specific patterns. For example, if
46    /// you have `"*.py"` as a pattern for a particular autocommand, you must
47    /// pass that exact pattern to clear it. Cannot be used together with
48    /// [`buffer`](ClearAutocmdsOptsBuilder::buffer).
49    pub fn patterns<'a, I>(&mut self, iter: I) -> &mut Self
50    where
51        I: IntoIterator<Item = &'a str>,
52    {
53        self.patterns = Some(Array::from_iter(iter).into());
54        self
55    }
56
57    /// Only clear the autocommands belonging to a specific augroup. The
58    /// augroup can be specified by both id and name.
59    pub fn group<Grp>(&mut self, group: Grp) -> &mut Self
60    where
61        Grp: StringOrInt,
62    {
63        self.group = Some(group.to_object());
64        self
65    }
66
67    pub fn build(&mut self) -> ClearAutocmdsOpts {
68        self.fallible_build().expect("never fails, all fields have defaults")
69    }
70}
71
72#[derive(Default)]
73#[allow(non_camel_case_types)]
74#[repr(C)]
75pub(crate) struct KeyDict_clear_autocmds<'a> {
76    event: NonOwning<'a, Object>,
77    group: NonOwning<'a, Object>,
78    buffer: Object,
79    pattern: NonOwning<'a, Object>,
80}
81
82impl<'a> From<&'a ClearAutocmdsOpts> for KeyDict_clear_autocmds<'a> {
83    fn from(opts: &'a ClearAutocmdsOpts) -> Self {
84        Self {
85            event: opts.events.non_owning(),
86            group: opts.group.non_owning(),
87            buffer: opts.buffer.as_ref().into(),
88            pattern: opts.patterns.non_owning(),
89        }
90    }
91}