Skip to content

Commit

Permalink
fix: toggle bookmark when have nested list
Browse files Browse the repository at this point in the history
  • Loading branch information
LintaoAmons committed Aug 9, 2024
1 parent 797ac13 commit bca5712
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions lua/bookmarks/domain/bookmark_list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,31 @@ function M.add_bookmark(self, bookmark)
table.insert(self.bookmarks, bookmark)
end

---@param self Bookmarks.BookmarkList
---@param bookmark Bookmarks.Bookmark
function M.remove_bookmark(self, bookmark)
for i, child in ipairs(self.bookmarks) do
if child.id == bookmark.id then
return table.remove(self.bookmarks, i)
end

local cur_type = _get_value_type(child)
if cur_type == _type.BOOKMARK_LIST then
---@cast child Bookmarks.BookmarkList
local ret = M.remove_bookmark(child, bookmark)
if ret ~= nil then
return ret
end
end
end
end

---@param self Bookmarks.BookmarkList
---@param location Bookmarks.Location
---@return Bookmarks.Bookmark?
function M.find_bookmark_by_location(self, location)
-- TODO: self location path
for _, b in ipairs(self.bookmarks) do
for _, b in ipairs(M.get_all_marks(self)) do
local b_type = _get_value_type(b)
if b_type == _type.BOOKMARK then
---@cast b Bookmarks.Bookmark
Expand Down Expand Up @@ -63,30 +82,18 @@ function M.toggle_bookmarks(self, bookmark, projects)
M.add_bookmark(updated_bookmark_list, bookmark)
end
else
table.insert(updated_bookmark_list.bookmarks, bookmark)
M.add_bookmark(updated_bookmark_list, bookmark)
end

return updated_bookmark_list
end

---@param self Bookmarks.BookmarkList
---@param bookmark Bookmarks.Bookmark
function M.remove_bookmark(self, bookmark, projects)
local new_bookmarks = {}
for _, b in ipairs(self.bookmarks) do
if not bookmark_scope.is_same_location(b, bookmark, projects) then
table.insert(new_bookmarks, b)
end
end
self.bookmarks = new_bookmarks
end

---@param self Bookmarks.BookmarkList
---@param bookmark Bookmarks.Bookmark
---@param projects Bookmarks.Project[]
---@return boolean
function M.contains_bookmark(self, bookmark, projects)
for _, b in ipairs(self.bookmarks) do
for _, b in ipairs(M.get_all_marks(self)) do
---@cast b Bookmarks.Bookmark
if bookmark_scope.is_same_location(b, bookmark, projects) then
return true
Expand All @@ -100,7 +107,7 @@ end
---@param id string | number
---@return Bookmarks.Bookmark?
function M.find_bookmark_by_id(self, id)
for _, b in ipairs(self.bookmarks) do
for _, b in ipairs(M.get_all_marks(self)) do
if b.id == id then
---@type Bookmarks.Bookmark
return b
Expand Down

0 comments on commit bca5712

Please sign in to comment.