nvim_api/types/
command_args.rs

1use nvim_types::{
2    conversion::{self, FromObject},
3    serde::Deserializer,
4    Object,
5};
6use serde::Deserialize;
7
8use crate::serde_utils as utils;
9
10/// Arguments passed to functions executed by commands. See
11/// [`Buffer::create_user_command`](crate::Buffer::create_user_command) to
12/// create a buffer-local command or
13/// [`create_user_command`](crate::create_user_command) to create a global one.
14#[non_exhaustive]
15#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
16pub struct CommandArgs {
17    /// The arguments passed to the command, if any.
18    #[serde(deserialize_with = "utils::empty_string_is_none")]
19    pub args: Option<String>,
20
21    /// Whether the command was executed with a `!` modifier.
22    pub bang: bool,
23
24    /// The count supplied by `<count>`, if any.
25    #[serde(deserialize_with = "utils::minus_one_is_none")]
26    pub count: Option<u32>,
27
28    /// The arguments passed to the command split by unescaped whitespace.
29    pub fargs: Vec<String>,
30
31    /// The starting line of the command range.
32    pub line1: usize,
33
34    /// The final line of the command range.
35    pub line2: usize,
36
37    /// Command modifiers, if any.
38    #[serde(deserialize_with = "utils::empty_string_is_none")]
39    pub mods: Option<String>,
40
41    /// The number of items in the command range.
42    pub range: u8,
43
44    /// The optional register, if specified.
45    #[serde(rename = "reg", deserialize_with = "utils::empty_string_is_none")]
46    pub register: Option<String>,
47
48    /// Command modifiers in a more structured format.
49    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
50    #[cfg_attr(
51        docsrs,
52        doc(cfg(any(feature = "neovim-0-8", feature = "neovim-nightly")))
53    )]
54    pub smods: super::CommandModifiers,
55}
56
57impl FromObject for CommandArgs {
58    fn from_object(obj: Object) -> Result<Self, conversion::Error> {
59        Self::deserialize(Deserializer::new(obj)).map_err(Into::into)
60    }
61}
62
63impl luajit_bindings::Poppable for CommandArgs {
64    unsafe fn pop(
65        lstate: *mut luajit_bindings::ffi::lua_State,
66    ) -> Result<Self, luajit_bindings::Error> {
67        let obj = Object::pop(lstate)?;
68
69        Self::from_object(obj)
70            .map_err(luajit_bindings::Error::pop_error_from_err::<Self, _>)
71    }
72}