Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Delete Macro! #3

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lua/nvim-macros/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,47 @@ function M.save_macro(register)
print("Macro saved as " .. name)
end

function M.delete_macro()
local macros = handle_json_file("r")
if not macros or not macros.macros or #macros.macros == 0 then
print_error("No macros found.")
return
end

local choices = {}
local name_to_index_map = {}
for index, macro in ipairs(macros.macros) do
if macro.name then
local display_text = macro.name .. " | " .. string.sub(macro.content, 1, 150)
table.insert(choices, display_text)
name_to_index_map[display_text] = index
end
end

if next(choices) == nil then
print_error("No valid macros to select for deletion.")
return
end

vim.ui.select(choices, { prompt = "Select a macro to delete:" }, function(choice)
if not choice then
print_error("No macro selected for deletion.")
return
end

local macro_index = name_to_index_map[choice]
if not macro_index then
print_error("Selected macro is not valid.")
return
end

-- Remove the selected macro from the list
table.remove(macros.macros, macro_index)
handle_json_file("w", macros) -- Write the updated list back to the JSON file
print("Macro deleted: " .. choice:match("^[^|]+"))
end)
end

-- Select and yank macro from JSON file (Yanks raw or escaped termcodes)
function M.select_and_yank_macro()
local macros = handle_json_file("r")
Expand Down
4 changes: 4 additions & 0 deletions plugin/nvim-macros.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ end, { nargs = "*" })
vim.api.nvim_create_user_command("MacroSelect", function()
require("nvim-macros").select_and_yank_macro()
end, {})

vim.api.nvim_create_user_command("MacroDelete", function()
require("nvim-macros").delete_macro()
end, {})