[Feat] add extension as argument (#57)

pull/60/head
Mist 2024-04-02 18:18:37 +08:00 committed by GitHub
parent 8e9b4489b6
commit fefaaa12be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 12 deletions

View File

@ -23,9 +23,9 @@ function main.setup(config)
static.config = table_utils.merge(static.config, config == nil and {} or config) static.config = table_utils.merge(static.config, config == nil and {} or config)
end end
local function get_config() local function get_config(specify_extension)
local code = visual_utils.get_selected_text() local code = visual_utils.get_selected_text()
local extension = get_extension() local extension = specify_extension or get_extension()
if string_utils.is_str_empty(code) then if string_utils.is_str_empty(code) then
error("Please select code which you want to take snapshot first") error("Please select code which you want to take snapshot first")
@ -45,18 +45,18 @@ local function get_config()
}, static.config) }, static.config)
end end
function main.copy_into_clipboard() function main.copy_into_clipboard(extension)
generator.copy_into_clipboard(get_config()) generator.copy_into_clipboard(get_config(extension))
vim.cmd("delmarks <>") vim.cmd("delmarks <>")
vim.notify("Save snapshot into clipboard successfully") vim.notify("Save snapshot into clipboard successfully")
end end
function main.save_snapshot() function main.save_snapshot(extension)
if string_utils.is_str_empty(static.config.save_path) then if string_utils.is_str_empty(static.config.save_path) then
error("Cannot find save_path from config") error("Cannot find save_path from config")
end end
generator.save_snapshot(get_config()) generator.save_snapshot(get_config(extension))
vim.cmd("delmarks <>") vim.cmd("delmarks <>")
vim.notify("Save snapshot in " .. static.config.save_path .. " successfully") vim.notify("Save snapshot in " .. static.config.save_path .. " successfully")
end end

View File

@ -1,9 +1,13 @@
local codesnap = require("codesnap") local codesnap = require("codesnap")
vim.api.nvim_create_user_command("CodeSnap", function() local function take_snapshot(take_snapshot_function)
codesnap.copy_into_clipboard() return function(detail)
end, { nargs = "*", range = "%" }) local args = detail.fargs
vim.api.nvim_create_user_command("CodeSnapSave", function() take_snapshot_function(args[1])
codesnap.save_snapshot() end
end, { nargs = "*", range = "%" }) end
vim.api.nvim_create_user_command("CodeSnap", take_snapshot(codesnap.copy_into_clipboard), { nargs = "*", range = "%" })
vim.api.nvim_create_user_command("CodeSnapSave", take_snapshot(codesnap.save_snapshot), { nargs = "*", range = "%" })