forked from zgpio/tree.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_api.py
executable file
·93 lines (77 loc) · 3 KB
/
gen_api.py
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from jinja2 import Environment, FileSystemLoader
import msgpack, sys, os, subprocess
import re
class InvalidType(Exception):
pass
class NativeType:
def __init__(self, name, expect_ref = False):
self.name = name
self.expect_ref = expect_ref
REMAP_T = {
'ArrayOf(Integer, 2)': NativeType('std::vector<Integer>', True),
'Boolean': NativeType('bool'),
'String': NativeType('std::string', True),
'void': NativeType('void'),
'Window': NativeType('Window'),
'Buffer': NativeType('Buffer'),
'Tabpage': NativeType('Tabpage'),
'Integer': NativeType('Integer'),
'Object': NativeType('Object', True),
'Array': NativeType('Array', True),
'Dictionary': NativeType('Dictionary', True)
}
def convert_type_to_native(nvim_t, enable_ref_op, const_ref=True, ref=False):
array_of = r'ArrayOf\(\s*(\w+)\s*\)'
# print(nvim_t.__class__)
# print(array_of.__class__)
obj = re.match(array_of, nvim_t)
if obj:
ret = 'std::vector<%s>' % convert_type_to_native(obj.groups()[0], False)
return 'const ' + ret + '&' if enable_ref_op else ret
if nvim_t in REMAP_T:
native_t = REMAP_T[nvim_t]
if const_ref:
return 'const ' + native_t.name + '&' if enable_ref_op and native_t.expect_ref else native_t.name
elif ref:
return native_t.name + '&' if enable_ref_op and native_t.expect_ref else native_t.name
else:
return native_t.name
else:
print("unknown nvim type name: " + str(nvim_t))
raise InvalidType()
#TODO: implement error handler
#return nvim_t
def main():
env = Environment(loader=FileSystemLoader('templates', encoding='utf8'))
api_info = subprocess.check_output(["nvim", '--api-info'])
unpacked_api = msgpack.unpackb(api_info, raw=False)
# generate nvim.hpp
functions = []
for f in unpacked_api['functions']:
if 'deprecated_since' in f and f['deprecated_since']<=3:
continue
d = {}
# if re.match(r'(n?vim_)?(ui.*|(un)?subscribe|.*(de|a)ttach.*)', f['name']):
# print('This is ui function: ' + f['name'])
# continue
d['name'] = f['name']
d['short_name'] = f['name'][5:] if f['name'].startswith('nvim_') else f['name']
try:
d['return'] = convert_type_to_native(f['return_type'], False)
d['args'] = [{'type': convert_type_to_native(arg[0], True),
'name': arg[1]} for arg in f['parameters']]
functions.append(d)
except InvalidType as e:
print("invalid function = " + str(f))
tpl = env.get_template('nvim.hpp')
api = tpl.render({'functions': functions})
with open(os.path.join("./gen", "nvim.hpp"), 'w') as f:
f.write(api)
tplcpp = env.get_template('nvim.cpp')
apicpp = tplcpp.render({'functions': functions})
with open(os.path.join("./gen", "nvim.cpp"), 'w') as f:
f.write(apicpp)
if __name__ == '__main__':
main()