Skip to content

Commit 8b6f37b

Browse files
committed
refactor: derive OptsBuilder for CreateCommandOpts on nightly
1 parent 45e2ad2 commit 8b6f37b

File tree

1 file changed

+83
-135
lines changed

1 file changed

+83
-135
lines changed

crates/oxi-api/src/opts/create_command.rs

Lines changed: 83 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
#[cfg(feature = "neovim-nightly")]
2-
use oxi_types::Boolean;
3-
use oxi_types::{
4-
self as nvim,
5-
conversion::ToObject,
6-
Function,
7-
Integer,
8-
Object,
9-
};
1+
use oxi_types as types;
2+
use types::conversion::ToObject;
103

114
use crate::types::{
125
CommandAddr,
@@ -17,71 +10,90 @@ use crate::types::{
1710
};
1811
use crate::Buffer;
1912

20-
/// Options passed to
21-
/// [`Buffer::create_user_command()`](crate::Buffer::create_user_command).
22-
#[cfg(not(feature = "neovim-nightly"))]
23-
#[derive(Clone, Debug, Default)]
24-
#[repr(C)]
25-
pub struct CreateCommandOpts {
26-
bar: Object,
27-
addr: Object,
28-
bang: Object,
29-
desc: Object,
30-
count: Object,
31-
force: Object,
32-
nargs: Object,
33-
range: Object,
34-
preview: Object,
35-
complete: Object,
36-
register_: Object,
37-
keepscript: Object,
38-
}
39-
4013
/// Options passed to [`create_user_command`](crate::create_user_command) and
4114
/// [`Buffer::create_user_command()`](crate::Buffer::create_user_command).
4215
#[cfg(feature = "neovim-nightly")]
43-
#[derive(Clone, Debug, Default)]
16+
#[derive(Clone, Debug, Default, oxi_macros::OptsBuilder)]
4417
#[repr(C)]
4518
pub struct CreateCommandOpts {
19+
#[builder(mask)]
4620
mask: u64,
4721

48-
/// 2nd in the mask.
49-
addr: Object,
50-
51-
/// 3rd in the mask.
52-
bang: Boolean,
22+
#[builder(argtype = "CommandAddr", inline = "{0}.to_object().unwrap()")]
23+
addr: types::Object,
5324

54-
/// 1st in the mask.
55-
bar: Boolean,
25+
#[builder(argtype = "bool")]
26+
bang: types::Boolean,
5627

57-
/// 10th in the mask.
58-
complete: Object,
28+
#[builder(argtype = "bool")]
29+
bar: types::Boolean,
5930

60-
/// 5th in the mask.
61-
count: Object,
31+
#[builder(
32+
argtype = "CommandComplete",
33+
inline = "{0}.to_object().unwrap()"
34+
)]
35+
complete: types::Object,
6236

63-
/// 4th in the mask.
64-
desc: Object,
37+
// TODO: fix `builder(Into)`.
38+
#[builder(
39+
generics = "C: Into<types::Integer>",
40+
argtype = "C",
41+
inline = "{0}.into().into()"
42+
)]
43+
count: types::Object,
6544

66-
/// 6th in the mask.
67-
force: Boolean,
68-
69-
/// 12th in the mask.
70-
keepscript: Boolean,
71-
72-
/// 7th in the mask.
73-
nargs: Object,
74-
75-
/// 9th in the mask.
76-
preview: Object,
77-
78-
/// 8th in the mask.
79-
range: Object,
45+
/// Description for the command.
46+
#[builder(
47+
generics = "C: Into<types::String>",
48+
argtype = "C",
49+
inline = "{0}.into().into()"
50+
)]
51+
desc: types::Object,
52+
53+
#[builder(argtype = "bool")]
54+
force: types::Boolean,
55+
56+
#[builder(argtype = "bool")]
57+
keepscript: types::Boolean,
58+
59+
#[builder(argtype = "CommandNArgs", inline = "{0}.to_object().unwrap()")]
60+
nargs: types::Object,
61+
62+
#[builder(
63+
generics = r#"F: Into<types::Function<(CommandArgs, Option<u32>, Option<Buffer>), u8>>"#,
64+
argtype = "F",
65+
inline = "{0}.into().into()"
66+
)]
67+
preview: types::Object,
68+
69+
#[builder(argtype = "CommandRange", inline = "{0}.to_object().unwrap()")]
70+
range: types::Object,
71+
72+
#[builder(method = "register", argtype = "bool")]
73+
register_: types::Boolean,
74+
}
8075

81-
/// 11th in the mask.
82-
register_: Boolean,
76+
/// Options passed to
77+
/// [`Buffer::create_user_command()`](crate::Buffer::create_user_command).
78+
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
79+
#[derive(Clone, Debug, Default)]
80+
#[repr(C)]
81+
pub struct CreateCommandOpts {
82+
bar: types::Object,
83+
addr: types::Object,
84+
bang: types::Object,
85+
desc: types::Object,
86+
count: types::Object,
87+
force: types::Object,
88+
nargs: types::Object,
89+
range: types::Object,
90+
preview: types::Object,
91+
complete: types::Object,
92+
register_: types::Object,
93+
keepscript: types::Object,
8394
}
8495

96+
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
8597
impl CreateCommandOpts {
8698
#[inline(always)]
8799
/// Creates a new [`CreateCommandOptsBuilder`].
@@ -90,151 +102,87 @@ impl CreateCommandOpts {
90102
}
91103
}
92104

105+
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
93106
#[derive(Clone, Default)]
94107
pub struct CreateCommandOptsBuilder(CreateCommandOpts);
95108

109+
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
96110
impl CreateCommandOptsBuilder {
97111
#[inline]
98112
pub fn addr(&mut self, addr: CommandAddr) -> &mut Self {
99113
self.0.addr = addr.to_object().unwrap();
100-
#[cfg(feature = "neovim-nightly")]
101-
{
102-
self.0.mask |= 0b101;
103-
}
104114
self
105115
}
106116

107117
#[inline]
108118
pub fn bang(&mut self, bang: bool) -> &mut Self {
109-
#[cfg(not(feature = "neovim-nightly"))]
110-
{
111-
self.0.bang = bang.into();
112-
}
113-
#[cfg(feature = "neovim-nightly")]
114-
{
115-
self.0.bang = bang;
116-
self.0.mask |= 0b1001;
117-
}
119+
self.0.bang = bang.into();
118120
self
119121
}
120122

121123
#[inline]
122124
pub fn bar(&mut self, bar: bool) -> &mut Self {
123-
#[cfg(not(feature = "neovim-nightly"))]
124-
{
125-
self.0.bar = bar.into();
126-
}
127-
#[cfg(feature = "neovim-nightly")]
128-
{
129-
self.0.bar = bar;
130-
self.0.mask |= 0b11;
131-
}
125+
self.0.bar = bar.into();
132126
self
133127
}
134128

135129
#[inline]
136130
pub fn complete(&mut self, complete: CommandComplete) -> &mut Self {
137131
self.0.complete = complete.to_object().unwrap();
138-
#[cfg(feature = "neovim-nightly")]
139-
{
140-
self.0.mask |= 0b10000000001;
141-
}
142132
self
143133
}
144134

145135
#[inline]
146-
pub fn count(&mut self, count: impl Into<Integer>) -> &mut Self {
136+
pub fn count(&mut self, count: impl Into<types::Integer>) -> &mut Self {
147137
self.0.count = count.into().into();
148-
#[cfg(feature = "neovim-nightly")]
149-
{
150-
self.0.mask |= 0b100001;
151-
}
152138
self
153139
}
154140

155141
/// Description for the command.
156142
#[inline]
157-
pub fn desc(&mut self, desc: impl Into<nvim::String>) -> &mut Self {
143+
pub fn desc<S: Into<types::String>>(&mut self, desc: S) -> &mut Self {
158144
self.0.desc = desc.into().into();
159-
#[cfg(feature = "neovim-nightly")]
160-
{
161-
self.0.mask |= 0b10001;
162-
}
163145
self
164146
}
165147

166148
#[inline]
167149
pub fn force(&mut self, force: bool) -> &mut Self {
168-
#[cfg(not(feature = "neovim-nightly"))]
169-
{
170-
self.0.force = force.into();
171-
}
172-
#[cfg(feature = "neovim-nightly")]
173-
{
174-
self.0.force = force;
175-
self.0.mask |= 0b1000001;
176-
}
150+
self.0.force = force.into();
177151
self
178152
}
179153

180154
#[inline]
181155
pub fn keepscript(&mut self, keepscript: bool) -> &mut Self {
182-
#[cfg(not(feature = "neovim-nightly"))]
183-
{
184-
self.0.keepscript = keepscript.into();
185-
}
186-
#[cfg(feature = "neovim-nightly")]
187-
{
188-
self.0.keepscript = keepscript;
189-
self.0.mask |= 0b1000000000001;
190-
}
156+
self.0.keepscript = keepscript.into();
191157
self
192158
}
193159

194160
#[inline]
195161
pub fn nargs(&mut self, nargs: CommandNArgs) -> &mut Self {
196162
self.0.nargs = nargs.to_object().unwrap();
197-
#[cfg(feature = "neovim-nightly")]
198-
{
199-
self.0.mask |= 0b10000001;
200-
}
201163
self
202164
}
203165

204166
#[inline]
205167
pub fn preview<F>(&mut self, fun: F) -> &mut Self
206168
where
207-
F: Into<Function<(CommandArgs, Option<u32>, Option<Buffer>), u8>>,
169+
F: Into<
170+
types::Function<(CommandArgs, Option<u32>, Option<Buffer>), u8>,
171+
>,
208172
{
209173
self.0.preview = fun.into().into();
210-
#[cfg(feature = "neovim-nightly")]
211-
{
212-
self.0.mask |= 0b1000000001;
213-
}
214174
self
215175
}
216176

217177
#[inline]
218178
pub fn range(&mut self, range: CommandRange) -> &mut Self {
219179
self.0.range = range.to_object().unwrap();
220-
#[cfg(feature = "neovim-nightly")]
221-
{
222-
self.0.mask |= 0b100000001;
223-
}
224180
self
225181
}
226182

227183
#[inline]
228184
pub fn register(&mut self, register: bool) -> &mut Self {
229-
#[cfg(not(feature = "neovim-nightly"))]
230-
{
231-
self.0.register_ = register.into();
232-
}
233-
#[cfg(feature = "neovim-nightly")]
234-
{
235-
self.0.register_ = register;
236-
self.0.mask |= 0b100000000001;
237-
}
185+
self.0.register_ = register.into();
238186
self
239187
}
240188

0 commit comments

Comments
 (0)