removed 'state.indexes' + fixed vim mode bookmark deletion

This commit is contained in:
Diogo Duarte
2024-04-26 15:14:57 +01:00
parent 60bfe6f160
commit c52de13eb0

View File

@@ -25,31 +25,26 @@ local _send_notification = ya.sync(
) )
local _get_real_index = ya.sync(function(state, idx) local _get_real_index = ya.sync(function(state, idx)
for key, value in pairs(state.indexes) do for key, value in pairs(state.bookmarks) do
if value == idx then if value.on == SUPPORTED_KEYS[idx].on then
return key return key
end end
end end
return nil return nil
end) end)
local _load_state = ya.sync(function(state) local _load_state = ya.sync(function(state)
ps.sub_remote("bookmarks", function(body) ps.sub_remote("bookmarks", function(body)
if not state.bookmarks and body then if not state.bookmarks and body then
state.indexes = {}
state.bookmarks = {} state.bookmarks = {}
for key, value in pairs(body.indexes) do for _, value in pairs(body) do
state.indexes[tonumber(key)] = value table.insert(state.bookmarks, value)
end
for key, value in pairs(body.bookmarks) do
state.bookmarks[tonumber(key)] = value
end end
end end
end) end)
end) end)
local _save_state = ya.sync(function(state, bookmarks, indexes) local _save_state = ya.sync(function(state, bookmarks)
if not bookmarks then if not bookmarks then
ps.pub_static(10, "bookmarks", nil) ps.pub_static(10, "bookmarks", nil)
return return
@@ -57,22 +52,16 @@ local _save_state = ya.sync(function(state, bookmarks, indexes)
local save_state = {} local save_state = {}
if state.persist == "all" then if state.persist == "all" then
save_state = { bookmarks = bookmarks, indexes = indexes } save_state = bookmarks
else -- VIM mode else -- VIM mode
local save_bookmarks = {}
local save_indexes = {}
local idx = 1 local idx = 1
for key, value in pairs(bookmarks) do for _, value in pairs(bookmarks) do
-- Only save bookmarks in upper case keys -- Only save bookmarks in upper case keys
if string.match(value.on, "%u") then if string.match(value.on, "%u") then
save_bookmarks[idx] = value save_state[idx] = value
local real_index = _get_real_index(key)
save_indexes[real_index] = idx
idx = idx + 1 idx = idx + 1
end end
end end
save_state = { bookmarks = save_bookmarks, indexes = save_indexes }
end end
ps.pub_static(10, "bookmarks", save_state) ps.pub_static(10, "bookmarks", save_state)
@@ -103,11 +92,10 @@ local save_bookmark = ya.sync(function(state, idx)
local folder = Folder:by_kind(Folder.CURRENT) local folder = Folder:by_kind(Folder.CURRENT)
state.bookmarks = state.bookmarks or {} state.bookmarks = state.bookmarks or {}
state.indexes = state.indexes or {}
local _idx = state.indexes[idx] local _idx = _get_real_index(idx)
if _idx == nil then if not _idx then
_idx = #state.bookmarks + 1 _idx = #state.bookmarks + 1
state.indexes[idx] = _idx
end end
state.bookmarks[_idx] = { state.bookmarks[_idx] = {
@@ -117,7 +105,7 @@ local save_bookmark = ya.sync(function(state, idx)
} }
if state.persist then if state.persist then
_save_state(state.bookmarks, state.indexes) _save_state(state.bookmarks)
end end
if state.notify and state.notify.enable then if state.notify and state.notify.enable then
@@ -153,23 +141,17 @@ local delete_bookmark = ya.sync(function(state, idx)
end end
table.remove(state.bookmarks, idx) table.remove(state.bookmarks, idx)
-- remove the indexes entry for the bookmark
local real_index = _get_real_index(idx)
if real_index then
table.remove(state.indexes, real_index)
end
if state.persist then if state.persist then
_save_state(state.bookmarks, state.indexes) _save_state(state.bookmarks)
end end
end) end)
local delete_all_bookmarks = ya.sync(function(state) local delete_all_bookmarks = ya.sync(function(state)
state.bookmarks = nil state.bookmarks = nil
state.indexes = nil
if state.persist then if state.persist then
_save_state(nil, nil) _save_state(nil)
end end
if state.notify and state.notify.enable then if state.notify and state.notify.enable then