feat(ui): implement ui_client event handlers

This commit is contained in:
hlpr98
2022-03-12 21:08:29 +01:00
committed by bfredl
parent aa35d15a0d
commit 794d2744f3
8 changed files with 187 additions and 4 deletions

62
src/nvim/generators/gen_api_ui_events.lua Normal file → Executable file
View File

@@ -3,15 +3,16 @@ local mpack = require('mpack')
local nvimdir = arg[1]
package.path = nvimdir .. '/?.lua;' .. package.path
assert(#arg == 7)
assert(#arg == 8)
local input = io.open(arg[2], 'rb')
local proto_output = io.open(arg[3], 'wb')
local call_output = io.open(arg[4], 'wb')
local remote_output = io.open(arg[5], 'wb')
local bridge_output = io.open(arg[6], 'wb')
local metadata_output = io.open(arg[7], 'wb')
local redraw_output = io.open(arg[8], 'wb')
local c_grammar = require('generators.c_grammar')
c_grammar = require('generators.c_grammar')
local events = c_grammar.grammar:match(input:read('*all'))
local function write_signature(output, ev, prefix, notype)
@@ -50,6 +51,35 @@ local function write_arglist(output, ev, need_copy)
end
end
function extract_and_write_arglist(output, ev)
local hlattrs_args_count = 0
for j = 1, #ev.parameters do
local param = ev.parameters[j]
local kind = param[1]
output:write(' '..kind..' arg_'..j..' = ')
if kind == 'HlAttrs' then
-- The first HlAttrs argument is rgb_attrs and second is cterm_attrs
output:write('redraw_dict2hlattrs(args.items['..(j-1)..'].data.dictionary, '..(hlattrs_args_count == 0 and 'true' or 'false')..');\n')
hlattrs_args_count = hlattrs_args_count + 1
elseif kind == 'Object' then
output:write('args.items['..(j-1)..'];\n')
else
output:write('args.items['..(j-1)..'].data.'..string.lower(kind)..';\n')
end
end
end
function call_ui_event_method(output, ev)
output:write(' ui_call_'..ev.name..'(')
for j = 1, #ev.parameters do
output:write('arg_'..j)
if j ~= #ev.parameters then
output:write(', ')
end
end
output:write(');\n')
end
for i = 1, #events do
local ev = events[i]
assert(ev.return_type == 'void')
@@ -160,12 +190,38 @@ for i = 1, #events do
call_output:write(";\n")
call_output:write("}\n\n")
end
if (not ev.remote_only) and (not ev.noexport) and (not ev.client_impl) then
redraw_output:write('void ui_redraw_event_'..ev.name..'(Array args)\n{\n')
extract_and_write_arglist(redraw_output, ev)
call_ui_event_method(redraw_output, ev)
redraw_output:write('}\n\n')
end
end
-- Generate the map_init method for redraw handlers
redraw_output:write([[
void redraw_methods_table_init(void)
{
]])
for i = 1, #events do
local fn = events[i]
if (not fn.noexport) and ((not fn.remote_only) or fn.client_impl) then
redraw_output:write(' add_redraw_event_handler('..
'(String) {.data = "'..fn.name..'", '..
'.size = sizeof("'..fn.name..'") - 1}, '..
'(ApiRedrawWrapper) ui_redraw_event_'..fn.name..');\n')
end
end
redraw_output:write('\n}\n\n')
proto_output:close()
call_output:close()
remote_output:close()
bridge_output:close()
redraw_output:close()
-- don't expose internal attributes like "impl_name" in public metadata
local exported_attributes = {'name', 'parameters',