[Perf] use print instead of raise error directly (#61)

This commit is contained in:
Mist 2024-04-02 20:04:37 +08:00 committed by GitHub
parent efeb8d89e2
commit 2572f664cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View file

@ -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))

View file

@ -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