Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
name: ci

on:
push:
branches:
- main
pull_request:
branches:
- main
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
# push:
# branches:
# - main
# pull_request:
# branches:
# - main
# schedule:
# - cron: '0 0 * * *'

jobs:
test:
Expand Down
137 changes: 134 additions & 3 deletions crates/api/src/types/command_complete.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
#[cfg(feature = "neovim-0-12")] // on 0.12 and Nightly.
use std::fmt;

#[cfg(not(feature = "neovim-0-12"))] // On 0.11 Only
use serde::Serialize;
#[cfg(feature = "neovim-0-12")] // on 0.12 and Nightly.
use serde::{Deserialize, Serialize, de};
#[cfg(feature = "neovim-0-12")] // on 0.12 and Nightly.
use types::{
Function,
Object,
conversion::{self, FromObject, ToObject},
serde::{Deserializer, Serializer},
};
#[cfg(not(feature = "neovim-0-12"))] // On 0.11 Only
use types::{
Function,
Object,
conversion::{self, ToObject},
serde::Serializer,
};

type CustomListFunc = Function<(String, String, usize), Vec<String>>;

/// See `:h command-complete` for details.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)]
Expand All @@ -14,12 +30,17 @@ pub enum CommandComplete {
Arglist,
Augroup,
Buffer,
Behave,
Breakpoint,
Color,
Command,
Compiler,
Cscope,
#[serde(rename = "custom")]
CustomFromCompleteArg,
#[serde(rename = "customlist")]
CustomlistFromCompleteArg,
DiffBuffer,
Dir,
DirInPath,
Environment,
Event,
Expression,
Expand All @@ -30,6 +51,7 @@ pub enum CommandComplete {
Help,
Highlight,
History,
Keymap,
Locale,
Lua,
Mapclear,
Expand All @@ -38,7 +60,12 @@ pub enum CommandComplete {
Messages,
Option,
Packadd,
#[cfg(feature = "neovim-0-12")] // On 0.12 and Nightly
Retab,
Runtime,
Scriptnames,
Shellcmd,
Shellcmdline,
Sign,
Syntax,
Syntime,
Expand All @@ -48,11 +75,115 @@ pub enum CommandComplete {
Var,

/// See `:h command-completion-customlist` for details.
CustomList(Function<(String, String, usize), Vec<String>>),
CustomList(CustomListFunc),
}

impl ToObject for CommandComplete {
fn to_object(self) -> Result<Object, conversion::Error> {
self.serialize(Serializer::new()).map_err(Into::into)
}
}

#[cfg(feature = "neovim-0-12")] // on 0.12 and Nightly.
impl FromObject for CommandComplete {
fn from_object(obj: Object) -> Result<Self, conversion::Error> {
Self::deserialize(Deserializer::new(obj)).map_err(Into::into)
}
}

#[cfg(feature = "neovim-0-12")] // on 0.12 and Nightly.
impl<'de> de::Deserialize<'de> for CommandComplete {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct CommandCompleteVisitor;

impl de::Visitor<'_> for CommandCompleteVisitor {
type Value = CommandComplete;

fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(
"string or function (see `:help command-completion`)",
)
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
match v {
"arglist" => return Ok(Self::Value::Arglist),
"augroup" => return Ok(Self::Value::Augroup),
"buffer" => return Ok(Self::Value::Buffer),
"breakpoint" => return Ok(Self::Value::Breakpoint),
"color" => return Ok(Self::Value::Color),
"command" => return Ok(Self::Value::Command),
"compiler" => return Ok(Self::Value::Compiler),
"custom" => return Ok(Self::Value::CustomFromCompleteArg),
"customlist" => {
return Ok(Self::Value::CustomlistFromCompleteArg);
},
"diff_buffer" => return Ok(Self::Value::DiffBuffer),
"dir" => return Ok(Self::Value::Dir),
"dir_in_path" => return Ok(Self::Value::DirInPath),
"environment" => return Ok(Self::Value::Environment),
"event" => return Ok(Self::Value::Event),
"expression" => return Ok(Self::Value::Expression),
"file" => return Ok(Self::Value::File),
"file_in_path" => return Ok(Self::Value::FileInPath),
"filetype" => return Ok(Self::Value::Filetype),
"function" => return Ok(Self::Value::Function),
"help" => return Ok(Self::Value::Help),
"highlight" => return Ok(Self::Value::Highlight),
"history" => return Ok(Self::Value::History),
"keymap" => return Ok(Self::Value::Keymap),
"locale" => return Ok(Self::Value::Locale),
"lua" => return Ok(Self::Value::Lua),
"mapclear" => return Ok(Self::Value::Mapclear),
"mapping" => return Ok(Self::Value::Mapping),
"menu" => return Ok(Self::Value::Menu),
"messages" => return Ok(Self::Value::Messages),
"option" => return Ok(Self::Value::Option),
"packadd" => return Ok(Self::Value::Packadd),
#[cfg(feature = "neovim-0-12")] // On 0.12 and Nightly
"retab" => return Ok(Self::Value::Retab),
"runtime" => return Ok(Self::Value::Runtime),
"scriptnames" => return Ok(Self::Value::Scriptnames),
"shellcmd" => return Ok(Self::Value::Shellcmd),
"shellcmdline" => return Ok(Self::Value::Shellcmdline),
"sign" => return Ok(Self::Value::Sign),
"syntax" => return Ok(Self::Value::Syntax),
"syntime" => return Ok(Self::Value::Syntime),
"tag" => return Ok(Self::Value::Tag),
"tag_listfiles" => return Ok(Self::Value::TagListfiles),
"user" => return Ok(Self::Value::User),
"var" => return Ok(Self::Value::Var),

_ => {},
};

Err(E::invalid_value(
de::Unexpected::Str(v),
&"completion type (see `:help command-completion`)",
))
}

fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
where
E: de::Error,
{
let lua_ref = Object::from_luaref(v as i32);
if let Ok(func) = CustomListFunc::from_object(lua_ref) {
return Ok(Self::Value::CustomList(func));
}
Err(E::invalid_value(
de::Unexpected::Float(v as f64),
&"custom_list like completion function",
))
}
}

deserializer.deserialize_str(CommandCompleteVisitor)
}
}
7 changes: 6 additions & 1 deletion crates/api/src/types/command_infos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use types::{
};

use super::{CommandAddr, CommandArgs, CommandNArgs, CommandRange};
#[cfg(feature = "neovim-0-12")] // on 0.12 and Nightly.
use crate::types::CommandComplete;

#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
Expand All @@ -26,7 +28,10 @@ pub struct CommandInfos {
/// Callback triggered by the command.
pub callback: Option<Function<CommandArgs, ()>>,

/// Command complletion strategy.
/// Command completion strategy.
#[cfg(feature = "neovim-0-12")] // on 0.12 and Nightly.
pub complete: Option<CommandComplete>,
#[cfg(not(feature = "neovim-0-12"))] // On 0.11 Only
pub complete: Option<String>,

/// TODO: docs
Expand Down