nvim_api/opts/
create_command.rs

1use derive_builder::Builder;
2use nvim_types::{
3    self as nvim,
4    conversion::ToObject,
5    Integer,
6    NonOwning,
7    Object,
8};
9
10use crate::types::{CommandAddr, CommandComplete, CommandNArgs, CommandRange};
11
12/// Options passed to `Buffer::create_user_command`.
13#[derive(Clone, Debug, Default, Builder)]
14#[builder(default, build_fn(private, name = "fallible_build"))]
15pub struct CreateCommandOpts {
16    #[builder(setter(custom))]
17    addr: Object,
18
19    #[builder(setter(strip_option))]
20    bang: Option<bool>,
21
22    #[builder(setter(strip_option))]
23    bar: Option<bool>,
24
25    #[builder(setter(custom))]
26    complete: Object,
27
28    #[builder(setter(into, strip_option))]
29    count: Option<Integer>,
30
31    #[builder(setter(custom))]
32    desc: Object,
33
34    #[builder(setter(strip_option))]
35    /// Whether to override any previous definitions. Defaults to `true`.
36    force: Option<bool>,
37
38    #[builder(setter(strip_option))]
39    keepscript: Option<bool>,
40
41    #[builder(setter(custom))]
42    nargs: Object,
43
44    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
45    #[cfg_attr(
46        docsrs,
47        doc(cfg(any(feature = "neovim-0-8", feature = "neovim-nightly")))
48    )]
49    #[builder(setter(custom))]
50    preview: Object,
51
52    #[builder(setter(custom))]
53    range: Object,
54
55    #[builder(setter(strip_option))]
56    register: Option<bool>,
57}
58
59impl CreateCommandOpts {
60    #[inline(always)]
61    /// Creates a new [`CreateCommandOptsBuilder`].
62    pub fn builder() -> CreateCommandOptsBuilder {
63        CreateCommandOptsBuilder::default()
64    }
65}
66
67macro_rules! object_setter {
68    ($name:ident, $args:ident) => {
69        pub fn $name(&mut self, $name: $args) -> &mut Self {
70            self.$name = Some($name.to_object().unwrap());
71            self
72        }
73    };
74}
75
76impl CreateCommandOptsBuilder {
77    object_setter!(addr, CommandAddr);
78    object_setter!(complete, CommandComplete);
79    object_setter!(nargs, CommandNArgs);
80    object_setter!(range, CommandRange);
81
82    /// Description for the command.
83    pub fn desc(&mut self, desc: impl Into<nvim::String>) -> &mut Self {
84        self.desc = Some(desc.into().into());
85        self
86    }
87
88    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
89    #[cfg_attr(
90        docsrs,
91        doc(cfg(any(feature = "neovim-0-8", feature = "neovim-nightly")))
92    )]
93    pub fn preview<F>(&mut self, fun: F) -> &mut Self
94    where
95        F: Into<
96            nvim_types::Function<
97                (
98                    crate::types::CommandArgs,
99                    Option<u32>,
100                    Option<crate::Buffer>,
101                ),
102                u8,
103            >,
104        >,
105    {
106        self.preview = Some(fun.into().into());
107        self
108    }
109
110    pub fn build(&mut self) -> CreateCommandOpts {
111        self.fallible_build().expect("never fails, all fields have defaults")
112    }
113}
114
115// To see the generated key dicts you need to build Neovim and look in
116// `/build/src/nvim/auto/keysets_defs.generated.h`.
117#[derive(Default)]
118#[allow(non_camel_case_types)]
119#[repr(C)]
120pub(crate) struct KeyDict_user_command<'a> {
121    bar: Object,
122    addr: NonOwning<'a, Object>,
123    bang: Object,
124    desc: NonOwning<'a, Object>,
125    count: Object,
126    force: Object,
127    nargs: NonOwning<'a, Object>,
128    range: NonOwning<'a, Object>,
129    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
130    preview: NonOwning<'a, Object>,
131    complete: NonOwning<'a, Object>,
132    register_: Object,
133    keepscript: Object,
134}
135
136impl<'a> From<&'a CreateCommandOpts> for KeyDict_user_command<'a> {
137    fn from(opts: &'a CreateCommandOpts) -> Self {
138        Self {
139            bar: opts.bar.into(),
140            addr: opts.addr.non_owning(),
141            bang: opts.bang.into(),
142            desc: opts.desc.non_owning(),
143            count: opts.count.into(),
144            force: opts.force.into(),
145            nargs: opts.nargs.non_owning(),
146            range: opts.range.non_owning(),
147            #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
148            preview: opts.preview.non_owning(),
149            complete: opts.complete.non_owning(),
150            register_: opts.register.into(),
151            keepscript: opts.keepscript.into(),
152        }
153    }
154}