From 2572f664ccb047c69c2df2694044d0a4eab93b47 Mon Sep 17 00:00:00 2001 From: Mist Date: Tue, 2 Apr 2024 20:04:37 +0800 Subject: [PATCH] [Perf] use print instead of raise error directly (#61) --- lua/codesnap/init.lua | 10 +++++++--- plugin/codesnap.lua | 13 ++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lua/codesnap/init.lua b/lua/codesnap/init.lua index 828c3b4..cc051b2 100644 --- a/lua/codesnap/init.lua +++ b/lua/codesnap/init.lua @@ -27,11 +27,12 @@ local function get_config(specify_extension) local extension = specify_extension or get_extension() if string_utils.is_str_empty(code) then - error("Please select code which you want to take snapshot first") + error("No code is selected", 0) + return end if string_utils.is_str_empty(extension) then - error("Cannot detect current filetype") + error("Cannot detect current filetype", 0) end return table_utils.merge({ @@ -52,7 +53,10 @@ end function main.save_snapshot(extension) if string_utils.is_str_empty(static.config.save_path) then - error("Cannot find save_path from config") + error( + "If you want to save snapshot in somewhere, you should config the save_path before, refer: https://github.com/mistricky/codesnap.nvim?tab=readme-ov-file#save-the-snapshot", + 0 + ) end require("generator").save_snapshot(get_config(extension)) diff --git a/plugin/codesnap.lua b/plugin/codesnap.lua index 53da7d3..ab6ed18 100644 --- a/plugin/codesnap.lua +++ b/plugin/codesnap.lua @@ -1,10 +1,21 @@ local codesnap = require("codesnap") +-- The func param is a function that come from rust side, the function +-- may raise exception to user side, the run_generator_function is used to +-- handle these function and print it friendly +local function run_generator_function(func) + xpcall(func, function(err) + print(err) + end) +end + local function take_snapshot(take_snapshot_function) return function(detail) local args = detail.fargs - take_snapshot_function(args[1]) + run_generator_function(function() + take_snapshot_function(args[1]) + end) end end