Cleanup of global functions and values & save bookmarks before dirChange
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
local bookmarks = {}
|
||||
local bookmarksFilePath = ".bookmarks"
|
||||
local M = {}
|
||||
local H = {
|
||||
bookmarks = {},
|
||||
}
|
||||
|
||||
function M.setup()
|
||||
local augroup = vim.api.nvim_create_augroup("bookmark.nvim", {clear=true})
|
||||
@@ -8,14 +10,22 @@ function M.setup()
|
||||
{"DirChanged"},
|
||||
{
|
||||
pattern = {"global"},
|
||||
callback = M.loadBookmarks,
|
||||
callback = H.loadBookmarks,
|
||||
group = augroup,
|
||||
}
|
||||
)
|
||||
vim.api.nvim_create_autocmd(
|
||||
{"DirChangedPre"},
|
||||
{
|
||||
pattern = {"global"},
|
||||
callback = H.saveBookmarks,
|
||||
group = augroup,
|
||||
}
|
||||
)
|
||||
vim.api.nvim_create_autocmd(
|
||||
{"SessionWritePost"},
|
||||
{
|
||||
callback = M.saveBookmarks,
|
||||
callback = H.saveBookmarks,
|
||||
group = augroup,
|
||||
}
|
||||
)
|
||||
@@ -23,15 +33,15 @@ function M.setup()
|
||||
{"VimLeavePre"},
|
||||
{
|
||||
pattern = {"*"},
|
||||
callback = M.saveBookmarks,
|
||||
callback = H.saveBookmarks,
|
||||
group = augroup,
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
function M.loadBookmarks()
|
||||
function H.loadBookmarks()
|
||||
--clear current bookmarks
|
||||
bookmarks = {}
|
||||
H.bookmarks = {}
|
||||
bookmarksFilePath = ".bookmarks"
|
||||
local path = vim.fs.find(".bookmarks", {upward = true, type="file"})[1]
|
||||
if not path then
|
||||
@@ -43,18 +53,18 @@ function M.loadBookmarks()
|
||||
local fileContent = file:read("*a")
|
||||
file:close()
|
||||
if fileContent and fileContent ~= "" then
|
||||
bookmarks = vim.json.decode(fileContent)
|
||||
H.bookmarks = vim.json.decode(fileContent)
|
||||
end
|
||||
else
|
||||
vim.notify("Failed to load Bookmarks")
|
||||
end
|
||||
end
|
||||
|
||||
function M.saveBookmarks()
|
||||
if #bookmarks > 0 or vim.fn.filereadable(bookmarksFilePath) == 1 then
|
||||
function H.saveBookmarks()
|
||||
if #H.bookmarks > 0 or vim.fn.filereadable(bookmarksFilePath) == 1 then
|
||||
local file = io.open(bookmarksFilePath, "w")
|
||||
if file then
|
||||
file:write(vim.json.encode(bookmarks))
|
||||
file:write(vim.json.encode(H.bookmarks))
|
||||
file:flush()
|
||||
file:close()
|
||||
else
|
||||
@@ -68,15 +78,15 @@ function M.addBookmark()
|
||||
if input and input ~= "" then
|
||||
local file = vim.api.nvim_buf_get_name(0)
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
table.insert(bookmarks, {name = input, file = file, cursor = cursor})
|
||||
table.insert(H.bookmarks, {name = input, file = file, cursor = cursor})
|
||||
vim.notify("Bookmark \"" .. input .. "\" added")
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function M.selectBookmark(action, on_choice)
|
||||
if bookmarks and #bookmarks > 0 then
|
||||
vim.ui.select(bookmarks, {prompt = action .." Bookmark", format_item = function(item) return item.name end}, on_choice)
|
||||
if H.bookmarks and #H.bookmarks > 0 then
|
||||
vim.ui.select(H.bookmarks, {prompt = action .." Bookmark", format_item = function(item) return item.name end}, on_choice)
|
||||
else
|
||||
vim.notify("No bookmarks set or loaded")
|
||||
end
|
||||
@@ -85,7 +95,7 @@ end
|
||||
function M.deleteBookmark()
|
||||
M.selectBookmark("Delete", function(bookmark, idx)
|
||||
if idx then
|
||||
table.remove(bookmarks, idx)
|
||||
table.remove(H.bookmarks, idx)
|
||||
vim.notify("Deleted bookmark \"".. bookmark.name .. "\"")
|
||||
end
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user