From 794d2744f33562326172801ddd729853e7135347 Mon Sep 17 00:00:00 2001 From: hlpr98 Date: Sat, 12 Mar 2022 21:08:29 +0100 Subject: feat(ui): implement ui_client event handlers --- src/nvim/generators/c_grammar.lua | 1 + src/nvim/generators/gen_api_ui_events.lua | 62 +++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) mode change 100644 => 100755 src/nvim/generators/gen_api_ui_events.lua (limited to 'src/nvim/generators') diff --git a/src/nvim/generators/c_grammar.lua b/src/nvim/generators/c_grammar.lua index f35817c466..70a7be86b5 100644 --- a/src/nvim/generators/c_grammar.lua +++ b/src/nvim/generators/c_grammar.lua @@ -49,6 +49,7 @@ local c_proto = Ct( (fill * Cg((P('FUNC_API_REMOTE_IMPL') * Cc(true)), 'remote_impl') ^ -1) * (fill * Cg((P('FUNC_API_BRIDGE_IMPL') * Cc(true)), 'bridge_impl') ^ -1) * (fill * Cg((P('FUNC_API_COMPOSITOR_IMPL') * Cc(true)), 'compositor_impl') ^ -1) * + (fill * Cg((P('FUNC_API_CLIENT_IMPL') * Cc(true)), 'client_impl') ^ -1) * fill * P(';') ) diff --git a/src/nvim/generators/gen_api_ui_events.lua b/src/nvim/generators/gen_api_ui_events.lua old mode 100644 new mode 100755 index 3cb117d8b5..3cb63c3837 --- a/src/nvim/generators/gen_api_ui_events.lua +++ b/src/nvim/generators/gen_api_ui_events.lua @@ -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', -- cgit From ca23f2ed308b46665e5c8e677c4012cfa7453490 Mon Sep 17 00:00:00 2001 From: bfredl Date: Sun, 13 Mar 2022 14:57:57 +0100 Subject: refactor(ui): use "ui_client" instead of "redraw" as general prefix --- src/nvim/generators/gen_api_ui_events.lua | 32 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/nvim/generators') diff --git a/src/nvim/generators/gen_api_ui_events.lua b/src/nvim/generators/gen_api_ui_events.lua index 3cb63c3837..cdf4538e9f 100755 --- a/src/nvim/generators/gen_api_ui_events.lua +++ b/src/nvim/generators/gen_api_ui_events.lua @@ -10,9 +10,9 @@ 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 client_output = io.open(arg[8], 'wb') -c_grammar = require('generators.c_grammar') +local c_grammar = require('generators.c_grammar') local events = c_grammar.grammar:match(input:read('*all')) local function write_signature(output, ev, prefix, notype) @@ -51,7 +51,7 @@ local function write_arglist(output, ev, need_copy) end end -function extract_and_write_arglist(output, ev) +local function extract_and_write_arglist(output, ev) local hlattrs_args_count = 0 for j = 1, #ev.parameters do local param = ev.parameters[j] @@ -59,7 +59,7 @@ function extract_and_write_arglist(output, ev) 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') + output:write('ui_client_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') @@ -69,7 +69,7 @@ function extract_and_write_arglist(output, ev) end end -function call_ui_event_method(output, ev) +local function call_ui_event_method(output, ev) output:write(' ui_call_'..ev.name..'(') for j = 1, #ev.parameters do output:write('arg_'..j) @@ -192,16 +192,16 @@ for i = 1, #events do 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') + client_output:write('void ui_client_event_'..ev.name..'(Array args)\n{\n') + extract_and_write_arglist(client_output, ev) + call_ui_event_method(client_output, ev) + client_output:write('}\n\n') end end --- Generate the map_init method for redraw handlers -redraw_output:write([[ -void redraw_methods_table_init(void) +-- Generate the map_init method for client handlers +client_output:write([[ +void ui_client_methods_table_init(void) { ]]) @@ -209,19 +209,19 @@ 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('.. + client_output:write(' add_ui_client_event_handler('.. '(String) {.data = "'..fn.name..'", '.. '.size = sizeof("'..fn.name..'") - 1}, '.. - '(ApiRedrawWrapper) ui_redraw_event_'..fn.name..');\n') + '(UIClientHandler) ui_client_event_'..fn.name..');\n') end end -redraw_output:write('\n}\n\n') +client_output:write('\n}\n\n') proto_output:close() call_output:close() remote_output:close() -redraw_output:close() +client_output:close() -- don't expose internal attributes like "impl_name" in public metadata local exported_attributes = {'name', 'parameters', -- cgit From f01d203b70f426c1538813b3bacb4483e914ab44 Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 15 Mar 2022 00:23:14 +0100 Subject: refactor(ui): make auto-generated ui client handlers typesafe --- src/nvim/generators/gen_api_ui_events.lua | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'src/nvim/generators') diff --git a/src/nvim/generators/gen_api_ui_events.lua b/src/nvim/generators/gen_api_ui_events.lua index cdf4538e9f..5e70442dce 100755 --- a/src/nvim/generators/gen_api_ui_events.lua +++ b/src/nvim/generators/gen_api_ui_events.lua @@ -51,8 +51,25 @@ local function write_arglist(output, ev, need_copy) end end -local function extract_and_write_arglist(output, ev) +local function call_ui_event_method(output, ev) + output:write('void ui_client_event_'..ev.name..'(Array args)\n{\n') + local hlattrs_args_count = 0 + if #ev.parameters > 0 then + output:write(' if (args.size < '..(#ev.parameters)) + for j = 1, #ev.parameters do + local kind = ev.parameters[j][1] + if kind ~= "Object" then + if kind == 'HlAttrs' then kind = 'Dictionary' end + output:write('\n || args.items['..(j-1)..'].type != kObjectType'..kind..'') + end + end + output:write(') {\n') + output:write(' ELOG("Error handling ui event \''..ev.name..'\'");\n') + output:write(' return;\n') + output:write(' }\n') + end + for j = 1, #ev.parameters do local param = ev.parameters[j] local kind = param[1] @@ -67,9 +84,7 @@ local function extract_and_write_arglist(output, ev) output:write('args.items['..(j-1)..'].data.'..string.lower(kind)..';\n') end end -end -local function call_ui_event_method(output, ev) output:write(' ui_call_'..ev.name..'(') for j = 1, #ev.parameters do output:write('arg_'..j) @@ -78,6 +93,8 @@ local function call_ui_event_method(output, ev) end end output:write(');\n') + + output:write('}\n\n') end for i = 1, #events do @@ -192,10 +209,7 @@ for i = 1, #events do end if (not ev.remote_only) and (not ev.noexport) and (not ev.client_impl) then - client_output:write('void ui_client_event_'..ev.name..'(Array args)\n{\n') - extract_and_write_arglist(client_output, ev) call_ui_event_method(client_output, ev) - client_output:write('}\n\n') end end -- cgit