diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 62230687e40..5518f31b43e 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -208,8 +208,8 @@ function Filters:prepare(project) local explorer = require("nvim-tree.core").get_explorer() if explorer then - for _, node in pairs(explorer.marks:list()) do - status.bookmarks[node.absolute_path] = node.type + for key, node in pairs(explorer.marks.marks) do + status.bookmarks[key] = node end end diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index c940f999983..930b4c85797 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -22,14 +22,6 @@ local Marks = Class:extend() ---@class (exact) MarksArgs ---@field explorer Explorer ----@protected ----@param args MarksArgs -function Marks:new(args) - self.explorer = args.explorer - - self.marks = {} -end - ---Clear all marks and reload if watchers disabled ---@private function Marks:clear_reload() @@ -46,22 +38,6 @@ function Marks:clear() self.explorer.renderer:draw() end ----@public ----@param node Node -function Marks:toggle(node) - if node.absolute_path == nil then - return - end - - if self:get(node) then - self.marks[node.absolute_path] = nil - else - self.marks[node.absolute_path] = node - end - - self.explorer.renderer:draw() -end - ---Return node if marked ---@public ---@param node Node @@ -243,6 +219,57 @@ function Marks:navigate_next() self:navigate(false) end + +local function save_bookmarks(marks) + local storepath = vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json" + local file = io.open(storepath, "w") + if file then + local data = {} + for path, _ in pairs(marks) do + table.insert(data, path) + end + file:write(vim.fn.json_encode(data)) + file:close() + end +end + +local function load_bookmarks() + local storepath = vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json" + local file = io.open(storepath, "r") + if file then + local content = file:read("*all") + file:close() + if content and content ~= "" then + local data = vim.fn.json_decode(content) + local marks = {} + for _, path in ipairs(data) do + marks[path] = true -- or reconstruct node if needed + end + return marks + end + end + return {} +end + +function Marks:new(args) + self.explorer = args.explorer + self.marks = load_bookmarks() or {} +end + +function Marks:toggle(node) + if node.absolute_path == nil then + return + end + + if self:get(node) then + self.marks[node.absolute_path] = nil + else + self.marks[node.absolute_path] = node + end + + save_bookmarks(self.marks) + self.explorer.renderer:draw() +end ---Prompts for selection of a marked node, sorted by absolute paths. ---A folder will be focused, a file will be opened. ---@public