diff --git a/symlinks/config/nvim/lua/esensar/lua_utils.lua b/symlinks/config/nvim/lua/esensar/lua_utils.lua index 89a1b76..19833c7 100644 --- a/symlinks/config/nvim/lua/esensar/lua_utils.lua +++ b/symlinks/config/nvim/lua/esensar/lua_utils.lua @@ -7,6 +7,16 @@ function M.eval_lua_buffer(bufnr) return M.eval_lua_code(table.concat(buffer_content, "\n")) end +function M.is_valid_lua_code(code_string) + local res = loadstring(code_string) + return type(res) == "function", res +end + +function M.is_valid_lua_code_buffer(bufnr) + local buffer_content = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + return M.is_valid_lua_code(table.concat(buffer_content, "\n")) +end + function M.eval_lua_code(code_string) -- Taken from https://github.com/bfredl/nvim-luadev/blob/master/lua/luadev.lua local block = coroutine.create(loadstring(code_string)) diff --git a/symlinks/config/nvim/plugin/luapopup.lua b/symlinks/config/nvim/plugin/luapopup.lua index b4757f5..80dfcce 100644 --- a/symlinks/config/nvim/plugin/luapopup.lua +++ b/symlinks/config/nvim/plugin/luapopup.lua @@ -19,6 +19,13 @@ local eval_functions = { [popup_type.lua] = lua_utils.eval_lua_buffer, [popup_type.fennel] = lua_utils.eval_fennel_buffer, } +local validate_functions = { + [popup_type.lua] = lua_utils.is_valid_lua_code_buffer, + [popup_type.fennel] = function(_) + -- No validation for fennel + return true + end, +} local function get_last_win(type) if type == popup_type.lua then @@ -80,9 +87,14 @@ local function popup_and_run(type) -- map keys win:map("n", "", function(_) - hidden = true - win:hide() - run() + local valid, message = validate_functions[type](win.bufnr) + if not valid then + vim.notify("Parsing code failed: \n" .. vim.inspect(message), vim.log.levels.ERROR) + else + hidden = true + win:hide() + run() + end end, { noremap = true }) win:map("n", "", function(_) cancel()