1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use types::conversion::ToObject;

use crate::types::{
    CommandAddr,
    CommandArgs,
    CommandComplete,
    CommandNArgs,
    CommandRange,
};
use crate::Buffer;

/// Options passed to [`create_user_command`](crate::create_user_command) and
/// [`Buffer::create_user_command()`](crate::Buffer::create_user_command).
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
#[derive(Clone, Debug, Default, macros::OptsBuilder)]
#[repr(C)]
pub struct CreateCommandOpts {
    #[builder(mask)]
    mask: u64,

    #[builder(argtype = "CommandAddr", inline = "{0}.to_object().unwrap()")]
    addr: types::Object,

    #[builder(argtype = "bool")]
    bang: types::Boolean,

    #[builder(argtype = "bool")]
    bar: types::Boolean,

    #[builder(
        argtype = "CommandComplete",
        inline = "{0}.to_object().unwrap()"
    )]
    complete: types::Object,

    // TODO: fix `builder(Into)`.
    #[builder(
        generics = "C: Into<types::Integer>",
        argtype = "C",
        inline = "{0}.into().into()"
    )]
    count: types::Object,

    /// Description for the command.
    #[builder(
        generics = "C: Into<types::String>",
        argtype = "C",
        inline = "{0}.into().into()"
    )]
    desc: types::Object,

    #[builder(argtype = "bool")]
    force: types::Boolean,

    #[builder(argtype = "bool")]
    keepscript: types::Boolean,

    #[builder(argtype = "CommandNArgs", inline = "{0}.to_object().unwrap()")]
    nargs: types::Object,

    #[builder(
        generics = r#"F: Into<types::Function<(CommandArgs, Option<u32>, Option<Buffer>), u8>>"#,
        argtype = "F",
        inline = "{0}.into().into()"
    )]
    preview: types::Object,

    #[builder(argtype = "CommandRange", inline = "{0}.to_object().unwrap()")]
    range: types::Object,

    #[builder(method = "register", argtype = "bool")]
    register_: types::Boolean,
}

/// Options passed to
/// [`Buffer::create_user_command()`](crate::Buffer::create_user_command).
#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
#[derive(Clone, Debug, Default)]
#[repr(C)]
pub struct CreateCommandOpts {
    bar: types::Object,
    addr: types::Object,
    bang: types::Object,
    desc: types::Object,
    count: types::Object,
    force: types::Object,
    nargs: types::Object,
    range: types::Object,
    preview: types::Object,
    complete: types::Object,
    register_: types::Object,
    keepscript: types::Object,
}

#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
impl CreateCommandOpts {
    #[inline(always)]
    /// Creates a new [`CreateCommandOptsBuilder`].
    pub fn builder() -> CreateCommandOptsBuilder {
        CreateCommandOptsBuilder::default()
    }
}

#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
#[derive(Clone, Default)]
pub struct CreateCommandOptsBuilder(CreateCommandOpts);

#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
impl CreateCommandOptsBuilder {
    #[inline]
    pub fn addr(&mut self, addr: CommandAddr) -> &mut Self {
        self.0.addr = addr.to_object().unwrap();
        self
    }

    #[inline]
    pub fn bang(&mut self, bang: bool) -> &mut Self {
        self.0.bang = bang.into();
        self
    }

    #[inline]
    pub fn bar(&mut self, bar: bool) -> &mut Self {
        self.0.bar = bar.into();
        self
    }

    #[inline]
    pub fn complete(&mut self, complete: CommandComplete) -> &mut Self {
        self.0.complete = complete.to_object().unwrap();
        self
    }

    #[inline]
    pub fn count(&mut self, count: impl Into<types::Integer>) -> &mut Self {
        self.0.count = count.into().into();
        self
    }

    /// Description for the command.
    #[inline]
    pub fn desc<S: Into<types::String>>(&mut self, desc: S) -> &mut Self {
        self.0.desc = desc.into().into();
        self
    }

    #[inline]
    pub fn force(&mut self, force: bool) -> &mut Self {
        self.0.force = force.into();
        self
    }

    #[inline]
    pub fn keepscript(&mut self, keepscript: bool) -> &mut Self {
        self.0.keepscript = keepscript.into();
        self
    }

    #[inline]
    pub fn nargs(&mut self, nargs: CommandNArgs) -> &mut Self {
        self.0.nargs = nargs.to_object().unwrap();
        self
    }

    #[inline]
    pub fn preview<F>(&mut self, fun: F) -> &mut Self
    where
        F: Into<
            types::Function<(CommandArgs, Option<u32>, Option<Buffer>), u8>,
        >,
    {
        self.0.preview = fun.into().into();
        self
    }

    #[inline]
    pub fn range(&mut self, range: CommandRange) -> &mut Self {
        self.0.range = range.to_object().unwrap();
        self
    }

    #[inline]
    pub fn register(&mut self, register: bool) -> &mut Self {
        self.0.register_ = register.into();
        self
    }

    #[inline]
    pub fn build(&mut self) -> CreateCommandOpts {
        std::mem::take(&mut self.0)
    }
}