diff --git a/lua/bookmark.lua b/lua/bookmark.lua index 5d772e5..b4b5be4 100644 --- a/lua/bookmark.lua +++ b/lua/bookmark.lua @@ -32,22 +32,33 @@ end function M.loadBookmarks() --clear current bookmarks bookmarks = {} + bookmarksFilePath = ".bookmarks" local path = vim.fs.find(".bookmarks", {upward = true, type="file"})[1] if not path then return end bookmarksFilePath = path - local fileContent = io.open(bookmarksFilePath, "r"):read("*a") - if fileContent and fileContent ~= "" then - bookmarks = vim.json.decode(fileContent) + local file = io.open(bookmarksFilePath, "r") + if file then + local fileContent = file:read("*a") + file:close() + if fileContent and fileContent ~= "" then + bookmarks = vim.json.decode(fileContent) + end + else + vim.notify("Failed to load Bookmarks") end end function M.saveBookmarks() - if #bookmarks > 1 then + if #bookmarks > 0 or vim.fn.filereadable(bookmarksFilePath) then local file = io.open(bookmarksFilePath, "w") if file then file:write(vim.json.encode(bookmarks)) + file:flush() + file:close() + else + vim.notify("Failed to load Bookmarks") end end end @@ -62,16 +73,16 @@ function M.addBookmark() end) end -function M.selectBookmark(on_choice) +function M.selectBookmark(action, on_choice) if bookmarks and #bookmarks > 0 then - vim.ui.select(bookmarks, {prompt = "Select Bookmark", format_item = function(item) return item.name end}, on_choice) + vim.ui.select(bookmarks, {prompt = action .." Bookmark", format_item = function(item) return item.name end}, on_choice) else vim.notify("No bookmarks set or loaded") end end function M.deleteBookmark() - M.selectBookmark(function(_, idx) + M.selectBookmark("Delete", function(_, idx) if idx then table.remove(bookmarks, idx) end @@ -79,7 +90,7 @@ function M.deleteBookmark() end function M.jumpToBookmark() - M.selectBookmark(vim.schedule_wrap(function(item) + M.selectBookmark("Jump to",vim.schedule_wrap(function(item) if item then vim.cmd("edit " .. item.file) vim.api.nvim_win_set_cursor(0, item.cursor)