nvim_api/opts/
get_autocmds.rs

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