This commit is contained in:
xiantang 2025-10-09 19:27:28 +04:00 committed by GitHub
commit 37f2e79d94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 60 additions and 4 deletions

View File

@ -530,6 +530,10 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
watcher = false, watcher = false,
}, },
}, },
marks = {
enable_persistence = false,
save_path = nil, -- nil will default to stdpath("data") .. "/nvim-tree-bookmarks.json"
},
}-- END_DEFAULT_OPTS }-- END_DEFAULT_OPTS
local function merge_options(conf) local function merge_options(conf)

View File

@ -208,8 +208,8 @@ function Filters:prepare(project)
local explorer = require("nvim-tree.core").get_explorer() local explorer = require("nvim-tree.core").get_explorer()
if explorer then if explorer then
for _, node in pairs(explorer.marks:list()) do for key, node in pairs(explorer.marks.marks) do
status.bookmarks[node.absolute_path] = node.type status.bookmarks[key] = node
end end
end end

View File

@ -11,6 +11,45 @@ local utils = require("nvim-tree.utils")
local Class = require("nvim-tree.classic") local Class = require("nvim-tree.classic")
local DirectoryNode = require("nvim-tree.node.directory") local DirectoryNode = require("nvim-tree.node.directory")
local function get_save_path(opts)
return opts.marks.save_path or (vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json")
end
local function save_bookmarks(marks, opts)
if not opts.marks.enable_persistence then
return
end
local storepath = get_save_path(opts)
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.json.encode(data))
file:close()
end
end
local function load_bookmarks(opts)
local storepath = get_save_path(opts)
local file = io.open(storepath, "r")
if file then
local content = file:read("*all")
file:close()
if content and content ~= "" then
local data = vim.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
---@class (exact) Marks: Class ---@class (exact) Marks: Class
---@field private explorer Explorer ---@field private explorer Explorer
---@field private marks table<string, Node> by absolute path ---@field private marks table<string, Node> by absolute path
@ -26,8 +65,15 @@ local Marks = Class:extend()
---@param args MarksArgs ---@param args MarksArgs
function Marks:new(args) function Marks:new(args)
self.explorer = args.explorer self.explorer = args.explorer
self.marks = {} self.marks = {}
if self.explorer.opts.marks.enable_persistence then
local ok, loaded_marks = pcall(load_bookmarks, self.explorer.opts)
if ok then
self.marks = loaded_marks
else
notify.warn("Failed to load bookmarks: " .. loaded_marks)
end
end
end end
---Clear all marks and reload if watchers disabled ---Clear all marks and reload if watchers disabled
@ -59,6 +105,12 @@ function Marks:toggle(node)
self.marks[node.absolute_path] = node self.marks[node.absolute_path] = node
end end
if self.explorer.opts.marks.enable_persistence then
local ok, err = pcall(save_bookmarks, self.marks, self.explorer.opts)
if not ok then
notify.warn("Failed to save bookmarks: " .. err)
end
end
self.explorer.renderer:draw() self.explorer.renderer:draw()
end end