Improve checking if marks exist

This commit is contained in:
Brandon Conway 2021-03-16 17:52:48 -07:00
parent a6faacee32
commit 80ce51f88d

View File

@ -31,6 +31,25 @@ local function get_buf_name(id)
return "" return ""
end end
local function mark_exists(buf_name)
local marks = harpoon.get_mark_config().marks
for idx = 1, #marks do
if marks[idx] == buf_name then
return true
end
end
return false
end
local function validate_buf_name(buf_name)
if buf_name == "" or buf_name == nil then
error("Couldn't find a valid file name to mark, sorry.")
return
end
end
M.get_index_of = function(item) M.get_index_of = function(item)
if item == nil then if item == nil then
error("You have provided a nil value to Harpoon, please provide a string rep of the file or the file idx.") error("You have provided a nil value to Harpoon, please provide a string rep of the file or the file idx.")
@ -73,10 +92,7 @@ M.add_file = function(file_name_or_buf_id)
return return
end end
if buf_name == "" or buf_name == nil then validate_buf_name(buf_name)
error("Couldn't find a valid file name to mark, sorry.")
return
end
local config = harpoon.get_mark_config() local config = harpoon.get_mark_config()
for idx = 1, M.get_length() do for idx = 1, M.get_length() do
@ -178,16 +194,15 @@ M.set_mark_list = function(new_list)
end end
M.toggle_file = function(file_name_or_buf_id) M.toggle_file = function(file_name_or_buf_id)
local mark_count_before = #harpoon.get_mark_config().marks local buf_name = get_buf_name(file_name_or_buf_id)
M.add_file(file_name_or_buf_id) validate_buf_name(buf_name)
local mark_count_after = #harpoon.get_mark_config().marks if (mark_exists(buf_name)) then
M.rm_file(buf_name)
if (mark_count_before == mark_count_after) then
M.rm_file(file_name_or_buf_id)
print("Mark removed") print("Mark removed")
else else
M.add_file(buf_name)
print("Mark Added") print("Mark Added")
end end
end end