From 6c0812d92e0c7937b913175b8856064cb45f55c2 Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 1 Aug 2023 23:35:34 +0200 Subject: [PATCH] feat(lua): specific error messages for type checking `opts` params --- runtime/doc/news.txt | 2 ++ src/nvim/generators/gen_api_dispatch.lua | 2 +- src/nvim/lua/converter.c | 15 ++++++++------- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 2d56fc4fc4..d0da35fb90 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -134,6 +134,8 @@ The following new APIs and features were added. • `vim.fn.*` • `vim.api.*` +• Improved messages for type errors in `vim.api.*` calls (including `opts` params) + ============================================================================== CHANGED FEATURES *news-changed* diff --git a/src/nvim/generators/gen_api_dispatch.lua b/src/nvim/generators/gen_api_dispatch.lua index 9c15597fcc..02648e6c99 100644 --- a/src/nvim/generators/gen_api_dispatch.lua +++ b/src/nvim/generators/gen_api_dispatch.lua @@ -630,7 +630,7 @@ local function process_function(fn) local seterr = '' if string.match(param_type, '^KeyDict_') then write_shifted_output(output, string.format([[ - %s %s = { 0 }; nlua_pop_keydict(lstate, &%s, %s_get_field, %s&err);]], param_type, cparam, cparam, param_type, extra)) + %s %s = { 0 }; nlua_pop_keydict(lstate, &%s, %s_get_field, &err_param, &err);]], param_type, cparam, cparam, param_type)) cparam = '&'..cparam errshift = 1 -- free incomplete dict on error else diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index 07c452deed..2df6458198 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -813,7 +813,7 @@ String nlua_pop_String(lua_State *lstate, Error *err) { if (lua_type(lstate, -1) != LUA_TSTRING) { lua_pop(lstate, 1); - api_set_error(err, kErrorTypeValidation, "Expected lua string"); + api_set_error(err, kErrorTypeValidation, "Expected Lua string"); return (String) { .size = 0, .data = NULL }; } String ret; @@ -834,7 +834,7 @@ Integer nlua_pop_Integer(lua_State *lstate, Error *err) { if (lua_type(lstate, -1) != LUA_TNUMBER) { lua_pop(lstate, 1); - api_set_error(err, kErrorTypeValidation, "Expected lua number"); + api_set_error(err, kErrorTypeValidation, "Expected Lua number"); return 0; } const lua_Number n = lua_tonumber(lstate, -1); @@ -871,7 +871,7 @@ static inline LuaTableProps nlua_check_type(lua_State *const lstate, Error *cons { if (lua_type(lstate, -1) != LUA_TTABLE) { if (err) { - api_set_error(err, kErrorTypeValidation, "Expected lua %s", + api_set_error(err, kErrorTypeValidation, "Expected Lua %s", (type == kObjectTypeFloat) ? "number" : "table"); } return (LuaTableProps) { .type = kObjectTypeNil }; @@ -885,7 +885,7 @@ static inline LuaTableProps nlua_check_type(lua_State *const lstate, Error *cons if (table_props.type != type) { if (err) { - api_set_error(err, kErrorTypeValidation, "Expected %s-like lua table", api_typename(type)); + api_set_error(err, kErrorTypeValidation, "Expected %s-like Lua table", api_typename(type)); } } @@ -1169,7 +1169,7 @@ Object nlua_pop_Object(lua_State *const lstate, bool ref, Error *const err) break; case kObjectTypeNil: api_set_error(err, kErrorTypeValidation, - "Cannot convert given lua table"); + "Cannot convert given Lua table"); break; default: abort(); @@ -1287,10 +1287,10 @@ void nlua_init_types(lua_State *const lstate) } // lua specific variant of api_dict_to_keydict -void nlua_pop_keydict(lua_State *L, void *retval, FieldHashfn hashy, Error *err) +void nlua_pop_keydict(lua_State *L, void *retval, FieldHashfn hashy, char **err_opt, Error *err) { if (!lua_istable(L, -1)) { - api_set_error(err, kErrorTypeValidation, "Expected lua table"); + api_set_error(err, kErrorTypeValidation, "Expected Lua table"); lua_pop(L, -1); return; } @@ -1336,6 +1336,7 @@ void nlua_pop_keydict(lua_State *L, void *retval, FieldHashfn hashy, Error *err) abort(); } if (ERROR_SET(err)) { + *err_opt = field->str; break; } }