aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbfredl <bjorn.linse@gmail.com>2022-03-15 00:23:14 +0100
committerbfredl <bjorn.linse@gmail.com>2022-03-15 19:55:34 +0100
commitf01d203b70f426c1538813b3bacb4483e914ab44 (patch)
treec2762f7768f631992e5eb436e71a4b9f49f9270d
parentc0b4d931e12910f67cc3eade664247ea2d2bb913 (diff)
downloadrneovim-f01d203b70f426c1538813b3bacb4483e914ab44.tar.gz
rneovim-f01d203b70f426c1538813b3bacb4483e914ab44.tar.bz2
rneovim-f01d203b70f426c1538813b3bacb4483e914ab44.zip
refactor(ui): make auto-generated ui client handlers typesafe
-rwxr-xr-xsrc/nvim/generators/gen_api_ui_events.lua26
-rw-r--r--src/nvim/ui_client.c15
2 files changed, 31 insertions, 10 deletions
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
diff --git a/src/nvim/ui_client.c b/src/nvim/ui_client.c
index 3914a4e199..4fad3e0709 100644
--- a/src/nvim/ui_client.c
+++ b/src/nvim/ui_client.c
@@ -112,7 +112,14 @@ static HlAttrs ui_client_dict2hlattrs(Dictionary d, bool rgb)
void ui_client_event_grid_resize(Array args)
{
- // TODO: typesafe!
+ if (args.size < 3
+ || args.items[0].type != kObjectTypeInteger
+ || args.items[1].type != kObjectTypeInteger
+ || args.items[2].type != kObjectTypeInteger) {
+ ELOG("Error handling ui event 'grid_resize'");
+ return;
+ }
+
Integer grid = args.items[0].data.integer;
Integer width = args.items[1].data.integer;
Integer height = args.items[2].data.integer;
@@ -189,7 +196,7 @@ void ui_client_event_grid_line(Array args)
if (j >= buf_size) {
goto error; // _YIKES_
}
- STRCPY(buf_char[j], schar);
+ STRLCPY(buf_char[j], schar, sizeof(schar_T));
buf_attr[j++] = cur_attr;
}
}
@@ -199,9 +206,9 @@ void ui_client_event_grid_line(Array args)
clear_attr = cur_attr;
ui_call_raw_line(grid, row, startcol, endcol, clearcol, clear_attr, lineflags,
- buf_char, buf_attr);
+ (const schar_T *)buf_char, (const sattr_T *)buf_attr);
return;
error:
- ELOG("malformatted 'grid_line' event");
+ ELOG("Error handling ui event 'grid_line'");
}