diff --git a/lua/bookmarks/actions.lua b/lua/bookmarks/actions.lua index 97c9c22..8907e16 100644 --- a/lua/bookmarks/actions.lua +++ b/lua/bookmarks/actions.lua @@ -203,6 +203,11 @@ function M.loadBookmarks() end function M.saveBookmarks() + if utils.count_bookmarks(config.cache.data) == 0 then + utils.delete_file(config.save_file) + return + end + local data = vim.json.encode(config.cache) if config.marks ~= data then utils.write_file(config.save_file, data) diff --git a/lua/bookmarks/util.lua b/lua/bookmarks/util.lua index faa566e..5758ecd 100644 --- a/lua/bookmarks/util.lua +++ b/lua/bookmarks/util.lua @@ -118,6 +118,30 @@ M.read_file = function(path, callback) end) end +M.delete_file = function(path) + if M.path_exists(path) then + uv.fs_unlink(path, function(err) + assert(not err, err) + end) + end +end + +local function count_keys(t) + local count = 0 + for _ in pairs(t) do + count = count + 1 + end + return count +end + +M.count_bookmarks = function(cache) + local result = 0 + for _, bookmarks in pairs(cache) do + result = result + count_keys(bookmarks) + end + return result +end + function M.warn(...) vim.notify(string.format(...), vim.log.levels.WARN) end