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 bookmarksFilePath = ".bookmarks"
|
||||||
local M = {}
|
local M = {}
|
||||||
|
local H = {
|
||||||
|
bookmarks = {},
|
||||||
|
}
|
||||||
|
|
||||||
function M.setup()
|
function M.setup()
|
||||||
local augroup = vim.api.nvim_create_augroup("bookmark.nvim", {clear=true})
|
local augroup = vim.api.nvim_create_augroup("bookmark.nvim", {clear=true})
|
||||||
@@ -8,14 +10,22 @@ function M.setup()
|
|||||||
{"DirChanged"},
|
{"DirChanged"},
|
||||||
{
|
{
|
||||||
pattern = {"global"},
|
pattern = {"global"},
|
||||||
callback = M.loadBookmarks,
|
callback = H.loadBookmarks,
|
||||||
|
group = augroup,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
vim.api.nvim_create_autocmd(
|
||||||
|
{"DirChangedPre"},
|
||||||
|
{
|
||||||
|
pattern = {"global"},
|
||||||
|
callback = H.saveBookmarks,
|
||||||
group = augroup,
|
group = augroup,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
vim.api.nvim_create_autocmd(
|
vim.api.nvim_create_autocmd(
|
||||||
{"SessionWritePost"},
|
{"SessionWritePost"},
|
||||||
{
|
{
|
||||||
callback = M.saveBookmarks,
|
callback = H.saveBookmarks,
|
||||||
group = augroup,
|
group = augroup,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -23,15 +33,15 @@ function M.setup()
|
|||||||
{"VimLeavePre"},
|
{"VimLeavePre"},
|
||||||
{
|
{
|
||||||
pattern = {"*"},
|
pattern = {"*"},
|
||||||
callback = M.saveBookmarks,
|
callback = H.saveBookmarks,
|
||||||
group = augroup,
|
group = augroup,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.loadBookmarks()
|
function H.loadBookmarks()
|
||||||
--clear current bookmarks
|
--clear current bookmarks
|
||||||
bookmarks = {}
|
H.bookmarks = {}
|
||||||
bookmarksFilePath = ".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
|
||||||
@@ -43,18 +53,18 @@ function M.loadBookmarks()
|
|||||||
local fileContent = file:read("*a")
|
local fileContent = file:read("*a")
|
||||||
file:close()
|
file:close()
|
||||||
if fileContent and fileContent ~= "" then
|
if fileContent and fileContent ~= "" then
|
||||||
bookmarks = vim.json.decode(fileContent)
|
H.bookmarks = vim.json.decode(fileContent)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
vim.notify("Failed to load Bookmarks")
|
vim.notify("Failed to load Bookmarks")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.saveBookmarks()
|
function H.saveBookmarks()
|
||||||
if #bookmarks > 0 or vim.fn.filereadable(bookmarksFilePath) == 1 then
|
if #H.bookmarks > 0 or vim.fn.filereadable(bookmarksFilePath) == 1 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(H.bookmarks))
|
||||||
file:flush()
|
file:flush()
|
||||||
file:close()
|
file:close()
|
||||||
else
|
else
|
||||||
@@ -68,15 +78,15 @@ function M.addBookmark()
|
|||||||
if input and input ~= "" then
|
if input and input ~= "" then
|
||||||
local file = vim.api.nvim_buf_get_name(0)
|
local file = vim.api.nvim_buf_get_name(0)
|
||||||
local cursor = vim.api.nvim_win_get_cursor(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")
|
vim.notify("Bookmark \"" .. input .. "\" added")
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.selectBookmark(action, on_choice)
|
function M.selectBookmark(action, on_choice)
|
||||||
if bookmarks and #bookmarks > 0 then
|
if H.bookmarks and #H.bookmarks > 0 then
|
||||||
vim.ui.select(bookmarks, {prompt = action .." Bookmark", format_item = function(item) return item.name end}, on_choice)
|
vim.ui.select(H.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
|
||||||
@@ -85,7 +95,7 @@ end
|
|||||||
function M.deleteBookmark()
|
function M.deleteBookmark()
|
||||||
M.selectBookmark("Delete", function(bookmark, idx)
|
M.selectBookmark("Delete", function(bookmark, idx)
|
||||||
if idx then
|
if idx then
|
||||||
table.remove(bookmarks, idx)
|
table.remove(H.bookmarks, idx)
|
||||||
vim.notify("Deleted bookmark \"".. bookmark.name .. "\"")
|
vim.notify("Deleted bookmark \"".. bookmark.name .. "\"")
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user