nvim_oxi_api/opts/
create_command.rs

1use types::conversion::ToObject;
2
3use crate::types::{
4    CommandAddr,
5    CommandArgs,
6    CommandComplete,
7    CommandNArgs,
8    CommandRange,
9};
10use crate::Buffer;
11
12/// Options passed to [`create_user_command`](crate::create_user_command) and
13/// [`Buffer::create_user_command()`](crate::Buffer::create_user_command).
14#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
15#[derive(Clone, Debug, Default, macros::OptsBuilder)]
16#[repr(C)]
17pub struct CreateCommandOpts {
18    #[builder(mask)]
19    mask: u64,
20
21    #[builder(argtype = "CommandAddr", inline = "{0}.to_object().unwrap()")]
22    addr: types::Object,
23
24    #[builder(argtype = "bool")]
25    bang: types::Boolean,
26
27    #[builder(argtype = "bool")]
28    bar: types::Boolean,
29
30    #[builder(
31        argtype = "CommandComplete",
32        inline = "{0}.to_object().unwrap()"
33    )]
34    complete: types::Object,
35
36    // TODO: fix `builder(Into)`.
37    #[builder(
38        generics = "C: Into<types::Integer>",
39        argtype = "C",
40        inline = "{0}.into().into()"
41    )]
42    count: types::Object,
43
44    /// Description for the command.
45    #[builder(
46        generics = "C: Into<types::String>",
47        argtype = "C",
48        inline = "{0}.into().into()"
49    )]
50    desc: types::Object,
51
52    #[builder(argtype = "bool")]
53    force: types::Boolean,
54
55    #[builder(argtype = "bool")]
56    keepscript: types::Boolean,
57
58    #[builder(argtype = "CommandNArgs", inline = "{0}.to_object().unwrap()")]
59    nargs: types::Object,
60
61    #[builder(
62        generics = r#"F: Into<types::Function<(CommandArgs, Option<u32>, Option<Buffer>), u8>>"#,
63        argtype = "F",
64        inline = "{0}.into().into()"
65    )]
66    preview: types::Object,
67
68    #[builder(argtype = "CommandRange", inline = "{0}.to_object().unwrap()")]
69    range: types::Object,
70
71    #[builder(method = "register", argtype = "bool")]
72    register_: types::Boolean,
73}
74
75/// Options passed to
76/// [`Buffer::create_user_command()`](crate::Buffer::create_user_command).
77#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
78#[derive(Clone, Debug, Default)]
79#[repr(C)]
80pub struct CreateCommandOpts {
81    bar: types::Object,
82    addr: types::Object,
83    bang: types::Object,
84    desc: types::Object,
85    count: types::Object,
86    force: types::Object,
87    nargs: types::Object,
88    range: types::Object,
89    preview: types::Object,
90    complete: types::Object,
91    register_: types::Object,
92    keepscript: types::Object,
93}
94
95#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
96impl CreateCommandOpts {
97    #[inline(always)]
98    /// Creates a new [`CreateCommandOptsBuilder`].
99    pub fn builder() -> CreateCommandOptsBuilder {
100        CreateCommandOptsBuilder::default()
101    }
102}
103
104#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
105#[derive(Clone, Default)]
106pub struct CreateCommandOptsBuilder(CreateCommandOpts);
107
108#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
109impl CreateCommandOptsBuilder {
110    #[inline]
111    pub fn addr(&mut self, addr: CommandAddr) -> &mut Self {
112        self.0.addr = addr.to_object().unwrap();
113        self
114    }
115
116    #[inline]
117    pub fn bang(&mut self, bang: bool) -> &mut Self {
118        self.0.bang = bang.into();
119        self
120    }
121
122    #[inline]
123    pub fn bar(&mut self, bar: bool) -> &mut Self {
124        self.0.bar = bar.into();
125        self
126    }
127
128    #[inline]
129    pub fn complete(&mut self, complete: CommandComplete) -> &mut Self {
130        self.0.complete = complete.to_object().unwrap();
131        self
132    }
133
134    #[inline]
135    pub fn count(&mut self, count: impl Into<types::Integer>) -> &mut Self {
136        self.0.count = count.into().into();
137        self
138    }
139
140    /// Description for the command.
141    #[inline]
142    pub fn desc<S: Into<types::String>>(&mut self, desc: S) -> &mut Self {
143        self.0.desc = desc.into().into();
144        self
145    }
146
147    #[inline]
148    pub fn force(&mut self, force: bool) -> &mut Self {
149        self.0.force = force.into();
150        self
151    }
152
153    #[inline]
154    pub fn keepscript(&mut self, keepscript: bool) -> &mut Self {
155        self.0.keepscript = keepscript.into();
156        self
157    }
158
159    #[inline]
160    pub fn nargs(&mut self, nargs: CommandNArgs) -> &mut Self {
161        self.0.nargs = nargs.to_object().unwrap();
162        self
163    }
164
165    #[inline]
166    pub fn preview<F>(&mut self, fun: F) -> &mut Self
167    where
168        F: Into<
169            types::Function<(CommandArgs, Option<u32>, Option<Buffer>), u8>,
170        >,
171    {
172        self.0.preview = fun.into().into();
173        self
174    }
175
176    #[inline]
177    pub fn range(&mut self, range: CommandRange) -> &mut Self {
178        self.0.range = range.to_object().unwrap();
179        self
180    }
181
182    #[inline]
183    pub fn register(&mut self, register: bool) -> &mut Self {
184        self.0.register_ = register.into();
185        self
186    }
187
188    #[inline]
189    pub fn build(&mut self) -> CreateCommandOpts {
190        std::mem::take(&mut self.0)
191    }
192}