-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv.lua
96 lines (84 loc) · 1.61 KB
/
conv.lua
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
-- base converter
if vim.fn.has('nvim') == 1 then
local function id(...)
return ...
end
return {
empty_dict = vim.empty_dict,
islist = vim.tbl_islist,
vim = id,
lua = id,
call = vim.call,
}
end
local M = {}
local empty_dict_mt = {}
function M.empty_dict()
return setmetatable({}, empty_dict_mt)
end
function M.islist(tbl)
if getmetatable(tbl) == empty_dict_mt then
return false
end
local max = 0
for k, v in pairs(tbl) do
if type(k) ~= 'number' then
return false
end
max = max < k and k or max
end
return #tbl == max
end
function M.vim(value)
if type(value) == 'table' then
local newtable = {}
for k, v in pairs(value) do
newtable[k] = M.vim(v)
end
if M.islist(newtable) then
return vim.list(newtable)
else
return vim.dict(newtable)
end
return newtable
end
return value
end
function M.lua(value)
local t = vim.type(value)
if t == 'dict' then
local tbl = {}
for k, v in value() do
tbl[k] = M.lua(v)
end
return tbl
end
if t == 'list' then
local tbl = {}
local count = 1
for v in value() do
tbl[count] = M.lua(v)
count = count + 1
end
return tbl
end
if t == 'table' then
local tbl = {}
for k, v in pairs(value) do
tbl[k] = M.lua(v)
end
return tbl
end
return value
end
-- various functions
local unpack = unpack or table.unpack
function M.call(fn, ...)
local tbl = {...}
for k, v in pairs(tbl) do
tbl[k] = M.vim(v)
end
return M.lua(vim.call(fn, unpack(tbl)))
end
-- M.call('map', {'foo', 'bar'}, print)
return M