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
use common::Index;
use lsp::Lsp;
use mlua::prelude::*;
use nvim_macros::api;
#[macro_use]
mod macros;
pub mod api;
use api::Api;
pub mod common;
pub mod lsp;
api! {
#[prefixed]
Vim("https://neovim.io/doc/user/lua.html" # "lua-stdlib"){
api: Api,
#[link = "https://neovim.io/doc/user/lsp.html#LSP"]
lsp: Lsp,
notify(value: &str, level: LogLevel, opts: Option<LuaTable>) {
#[derive(Debug, Clone, Copy, ToLua)]
pub enum LogLevel {
Trace = 0,
Debug = 1,
Info = 2,
Warn = 3,
Error = 4,
}
}
g: Variables,
b: IndexedVariables,
w: IndexedVariables,
t: IndexedVariables,
v: Variables,
env: Env,
opt: Opt,
opt_local: Opt,
opt_global: Opt
}
}
impl<'lua> Vim<'lua> {
pub fn new(lua: &'lua Lua) -> Self {
Self::from_lua(lua.globals().get("vim").unwrap(), lua).unwrap()
}
pub fn lua(&self) -> &'lua Lua {
self.lua
}
}
impl<'lua> From<&'lua Lua> for Vim<'lua> {
fn from(lua: &'lua Lua) -> Self {
Self::new(lua)
}
}
api! {
Variables("https://neovim.io/doc/user/lua.html"#"lua-vim-variables")
IndexedVariables("https://neovim.io/doc/user/lua.html"#"lua-vim-variables")
Env("https://neovim.io/doc/user/lua.html"#"vim.env")
Opt("https://neovim.io/doc/user/lua.html"#"vim.opt")
}
impl<'lua> Variables<'lua> {
pub fn get<V: FromLua<'lua>>(&self, name: impl AsRef<str>) -> LuaResult<V> {
self.this.get(name.as_ref())
}
pub fn set(&self, name: impl AsRef<str>, value: impl ToLua<'lua>) {
self.this.set(name.as_ref(), value).unwrap()
}
pub fn unset(&self, name: impl AsRef<str>) {
self.this.set(name.as_ref(), LuaValue::Nil).unwrap()
}
}
impl<'lua> IndexedVariables<'lua> {
pub fn get<V: FromLua<'lua>>(&self, index: Index, name: impl AsRef<str>) -> LuaResult<V> {
if let Index::Index(index) = index {
self.this.get::<_, LuaTable>(index)?.get(name.as_ref())
} else {
self.this.get(name.as_ref())
}
}
pub fn set(
&self,
index: Index,
name: impl AsRef<str>,
value: impl ToLua<'lua>,
) -> LuaResult<()> {
if let Index::Index(index) = index {
self.this
.get::<_, LuaTable>(index)?
.set(name.as_ref(), value)
.unwrap()
} else {
self.this.set(name.as_ref(), value).unwrap()
}
Ok(())
}
pub fn unset(&self, index: Index, name: impl AsRef<str>) -> LuaResult<()> {
if let Index::Index(index) = index {
self.this
.get::<_, LuaTable>(index)?
.set(name.as_ref(), LuaValue::Nil)
.unwrap()
} else {
self.this.set(name.as_ref(), LuaValue::Nil).unwrap()
}
Ok(())
}
}
impl Env<'_> {
pub fn get(&self, name: impl AsRef<str>) -> Option<String> {
self.this.get(name.as_ref()).unwrap()
}
pub fn set(&self, name: impl AsRef<str>, value: impl AsRef<str>) {
self.this.set(name.as_ref(), value.as_ref()).unwrap()
}
pub fn unset(&self, name: impl AsRef<str>) {
self.this.set(name.as_ref(), LuaValue::Nil).unwrap()
}
}
impl<'lua> Opt<'lua> {
pub fn get<V: FromLua<'lua>>(&self, name: impl AsRef<str>) -> LuaResult<V> {
self.this
.get::<_, LuaTable>(name.as_ref())?
.call_method("get", ())
}
pub fn set(&self, name: impl AsRef<str>, value: impl ToLua<'lua>) -> LuaResult<()> {
self.this.set(name.as_ref(), value)
}
pub fn append(&self, name: impl AsRef<str>, value: impl ToLua<'lua>) -> LuaResult<()> {
self.this
.get::<_, LuaTable>(name.as_ref())?
.call_method("append", value)
}
pub fn prepend(&self, name: impl AsRef<str>, value: impl ToLua<'lua>) -> LuaResult<()> {
self.this
.get::<_, LuaTable>(name.as_ref())?
.call_method("prepend", value)
}
pub fn remove(&self, name: impl AsRef<str>, value: impl ToLua<'lua>) -> LuaResult<()> {
self.this
.get::<_, LuaTable>(name.as_ref())?
.call_method("remove", value)
}
}