-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen_api.lua
More file actions
90 lines (77 loc) · 1.99 KB
/
gen_api.lua
File metadata and controls
90 lines (77 loc) · 1.99 KB
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
local functions = vim.fn.api_info().functions
table.sort(functions, function(a, b)
return a.name < b.name
end)
local types = {}
local TYPEMAP = {
void = 'nil',
Boolean = 'boolean',
Integer = 'integer',
Float = 'number',
String = 'string',
Array = 'table',
Dictionary = 'table',
LuaRef = 'function',
Object = 'any',
Buffer = 'Buffer',
Window = 'Window',
Tabpage = 'Tabpage',
}
local function new_type(s)
if not types[s] then
local arrayof = s:match('^ArrayOf%((.*)%)$')
if arrayof then
local tp, cnt = arrayof:match('^(%w+), (%d+)$')
if tp then
tp = assert(TYPEMAP[tp], 'unknown type: '..tp)
local tbl = {}
for i = 1, cnt do
table.insert(tbl, ('[%d]: %s'):format(i, tp))
end
types[s] = '{ '..table.concat(tbl, ', ')..' }'
else
types[s] = assert(TYPEMAP[arrayof], 'unknown type: '..arrayof)..'[]'
end
else
types[s] = assert(TYPEMAP[s], 'unknown type: '..s)
end
end
return types[s]
end
local lines = {[[
---@meta
---@class vim.api
vim.api = {}
---@alias Buffer integer
---@alias Tabpage integer
---@alias Window integer
]]}
local function ins(s)
table.insert(lines, s)
end
for _, f in ipairs(functions) do
if f.name:find('^nvim_') then
f.return_type = new_type(f.return_type)
for _, parameter in ipairs(f.parameters) do
parameter[1] = new_type(parameter[1])
end
local p1, p2 = {}, {}
for _, param in ipairs(f.parameters) do
if param[2] == 'end' then param[2] = '_end' end
table.insert(p1, assert(param[2]))
table.insert(p2, ('---@param %s %s'):format(param[2], param[1]))
end
ins('')
if #p2 > 0 then
ins(table.concat(p2, '\n'))
end
if f.return_type ~= 'nil' then
ins(('---@return %s'):format(f.return_type))
end
ins(('function vim.api.%s(%s) end'):format(f.name, table.concat(p1, ', ')))
end
end
ins('')
local f = io.open('./nvim/library/vim.api.lua', 'w+b')
f:write(table.concat(lines, '\n'))
f:flush()