Fix not saving correctly
This commit is contained in:
@@ -32,22 +32,33 @@ end
|
|||||||
function M.loadBookmarks()
|
function M.loadBookmarks()
|
||||||
--clear current bookmarks
|
--clear current bookmarks
|
||||||
bookmarks = {}
|
bookmarks = {}
|
||||||
|
bookmarksFilePath = ".bookmarks"
|
||||||
local path = vim.fs.find(".bookmarks", {upward = true, type="file"})[1]
|
local path = vim.fs.find(".bookmarks", {upward = true, type="file"})[1]
|
||||||
if not path then
|
if not path then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
bookmarksFilePath = path
|
bookmarksFilePath = path
|
||||||
local fileContent = io.open(bookmarksFilePath, "r"):read("*a")
|
local file = io.open(bookmarksFilePath, "r")
|
||||||
|
if file then
|
||||||
|
local fileContent = file:read("*a")
|
||||||
|
file:close()
|
||||||
if fileContent and fileContent ~= "" then
|
if fileContent and fileContent ~= "" then
|
||||||
bookmarks = vim.json.decode(fileContent)
|
bookmarks = vim.json.decode(fileContent)
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
vim.notify("Failed to load Bookmarks")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.saveBookmarks()
|
function M.saveBookmarks()
|
||||||
if #bookmarks > 1 then
|
if #bookmarks > 0 or vim.fn.filereadable(bookmarksFilePath) then
|
||||||
local file = io.open(bookmarksFilePath, "w")
|
local file = io.open(bookmarksFilePath, "w")
|
||||||
if file then
|
if file then
|
||||||
file:write(vim.json.encode(bookmarks))
|
file:write(vim.json.encode(bookmarks))
|
||||||
|
file:flush()
|
||||||
|
file:close()
|
||||||
|
else
|
||||||
|
vim.notify("Failed to load Bookmarks")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -62,16 +73,16 @@ function M.addBookmark()
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.selectBookmark(on_choice)
|
function M.selectBookmark(action, on_choice)
|
||||||
if bookmarks and #bookmarks > 0 then
|
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
|
else
|
||||||
vim.notify("No bookmarks set or loaded")
|
vim.notify("No bookmarks set or loaded")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.deleteBookmark()
|
function M.deleteBookmark()
|
||||||
M.selectBookmark(function(_, idx)
|
M.selectBookmark("Delete", function(_, idx)
|
||||||
if idx then
|
if idx then
|
||||||
table.remove(bookmarks, idx)
|
table.remove(bookmarks, idx)
|
||||||
end
|
end
|
||||||
@@ -79,7 +90,7 @@ function M.deleteBookmark()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.jumpToBookmark()
|
function M.jumpToBookmark()
|
||||||
M.selectBookmark(vim.schedule_wrap(function(item)
|
M.selectBookmark("Jump to",vim.schedule_wrap(function(item)
|
||||||
if item then
|
if item then
|
||||||
vim.cmd("edit " .. item.file)
|
vim.cmd("edit " .. item.file)
|
||||||
vim.api.nvim_win_set_cursor(0, item.cursor)
|
vim.api.nvim_win_set_cursor(0, item.cursor)
|
||||||
|
|||||||
Reference in New Issue
Block a user